Order::jsonSerialize()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 256
nop 0
dl 0
loc 43
ccs 0
cts 23
cp 0
crap 90
rs 6.5222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Order
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use DateTime;
14
15
/**
16
 * Order
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.2.4
20
 * @since   1.0.0
21
 */
22
class Order extends Message implements \JsonSerializable {
23
	/**
24
	 * ISO 8601 standard Date / time on which the order is announced at ROK.
25
	 * As a rule, this is the current date / time.
26
	 *
27
	 * This field is mandatory and provides protection against so-called
28
	 * replay (playback) attacks
29
	 *
30
	 * @var DateTime
31
	 */
32
	private $timestamp;
33
34
	/**
35
	 * Generated by Merchant. If your webshop wants to use AfterPay, this field must be unique.
36
	 *
37
	 * @var string
38
	 */
39
	private $merchant_order_id;
40
41
	/**
42
	 * Description of the order.
43
	 *
44
	 * @var string|null
45
	 */
46
	private $description;
47
48
	/**
49
	 * The order items.
50
	 *
51
	 * @var OrderItems|null
52
	 */
53
	private $order_items;
54
55
	/**
56
	 * Amount.
57
	 *
58
	 * @var Money
59
	 */
60
	private $amount;
61
62
	/**
63
	 * The shipping address.
64
	 *
65
	 * @var Address|null
66
	 */
67
	private $shipping_detail;
68
69
	/**
70
	 * The billing address.
71
	 *
72
	 * @var Address|null
73
	 */
74
	private $billing_detail;
75
76
	/**
77
	 * The customer information.
78
	 *
79
	 * @var CustomerInformation|null
80
	 */
81
	private $customer_information;
82
83
	/**
84
	 * Language.
85
	 *
86
	 * ISO 639-1 standard. Not Case sensitive.
87
	 *
88
	 * @var string|null
89
	 */
90
	private $language;
91
92
	/**
93
	 * Merchant return URL.
94
	 *
95
	 * The URL to which the consumer's browser will be sent after the payment.
96
	 *
97
	 * @var string
98
	 */
99
	private $merchant_return_url;
100
101
	/**
102
	 * Payment brand.
103
	 *
104
	 * This field is optional and is used to enforce a specific
105
	 * payment method with the consumer instead of the consumer
106
	 * selecting a payment method on the payment method selection
107
	 * page.
108
	 *
109
	 * Valid values are:
110
	 * • IDEAL
111
	 * • AFTERPAY
112
	 * • PAYPAL
113
	 * • MASTERCARD
114
	 * • VISA
115
	 * • BANCONTACT
116
	 * • MAESTRO
117
	 * • V_PAY
118
	 * • CARDS
119
	 *
120
	 * The CARDS value ensures that the consumer can choose
121
	 * between payment methods: MASTERCARD, VISA, BANCONTACT,
122
	 * MAESTRO and V_PAY
123
	 *
124
	 * @var string|null
125
	 */
126
	private $payment_brand;
127
128
	/**
129
	 * Payment brand force.
130
	 *
131
	 * This field should only be delivered if the paymentBrand field (see
132
	 * above) is also specified. This field can be used to send or, after
133
	 * a failed payment, the consumer can or can not select another payment
134
	 * method to still pay the payment.
135
	 *
136
	 * Valid values are:
137
	 * • FORCE_ONCE
138
	 * • FORCE_ALWAYS
139
	 *
140
	 * In the case of FORCE_ONCE, the indicated paymentBrand is only
141
	 * enforced on the first transaction. If this fails, the consumer
142
	 * can still choose another payment method. When FORCE_ALWAYS is
143
	 * chosen, the consumer can not choose another payment method.
144
	 *
145
	 * @var string|null
146
	 */
147
	private $payment_brand_force;
148
149
	/**
150
	 * Construct order.
151
	 *
152
	 * @param string $merchant_order_id    Merchant order ID.
153
	 * @param Money  $amount               Amount.
154
	 * @param string $merchant_return_url  Merchant return URL.
155
	 * @return void
156
	 */
157 5
	public function __construct( $merchant_order_id, $amount, $merchant_return_url ) {
158 5
		$this->set_timestamp( new DateTime() );
159 5
		$this->set_merchant_order_id( $merchant_order_id );
160 3
		$this->set_amount( $amount );
161 3
		$this->set_merchant_return_url( $merchant_return_url );
162 3
	}
163
164
	/**
165
	 * Set timestamp.
166
	 *
167
	 * @param DateTime $timestamp Timestamp.
168
	 * @return void
169
	 */
170 5
	public function set_timestamp( DateTime $timestamp ) {
171 5
		$this->timestamp = $timestamp;
172 5
	}
173
174
	/**
175
	 * Get merchant order ID.
176
	 *
177
	 * @return string
178
	 */
179 1
	public function get_merchant_order_id() {
180 1
		return $this->merchant_order_id;
181
	}
182
183
	/**
184
	 * Set merchant order ID.
185
	 *
186
	 * Generated by Merchant. If you want your webshop to use AfterPay, this field must be unique.
187
	 * If the ID contains more than 24 characters, the extra characters are removed after the 24th character.
188
	 *
189
	 * @param string $merchant_order_id Merchant order ID.
190
	 * @return void
191
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 10`.
192
	 */
193 5
	public function set_merchant_order_id( $merchant_order_id ) {
194 5
		DataHelper::validate_ans( $merchant_order_id, 24, 'Order.merchantOrderId' );
195
196 3
		$this->merchant_order_id = $merchant_order_id;
197 3
	}
198
199
	/**
200
	 * Set amount.
201
	 *
202
	 * @param Money $amount Amount.
203
	 * @return void
204
	 */
205 3
	public function set_amount( Money $amount ) {
206 3
		$this->amount = $amount;
207 3
	}
208
209
	/**
210
	 * Set merchant return URL.
211
	 *
212
	 * The URL to which the consumer's browser will be sent after the payment.
213
	 *
214
	 * @param string $url Merchant return URL.
215
	 * @return void
216
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 1024`.
217
	 */
218 3
	public function set_merchant_return_url( $url ) {
219 3
		DataHelper::validate_an( $url, 1024, 'Order.merchantReturnURL' );
220
221 3
		$this->merchant_return_url = $url;
222 3
	}
223
224
	/**
225
	 * Set description.
226
	 *
227
	 * @param string|null $description Description.
228
	 * @return void
229
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 35`.
230
	 */
231 1
	public function set_description( $description ) {
232 1
		DataHelper::validate_null_or_an( $description, 35, 'Order.description' );
233
234 1
		$this->description = $description;
235 1
	}
236
237
	/**
238
	 * Set language.
239
	 *
240
	 * @param string|null $language Language (ISO 3166-1 alpha-2).
241
	 * @return void
242
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..2`.
243
	 */
244 1
	public function set_language( $language ) {
245 1
		DataHelper::validate_null_or_an( $language, 2, 'Order.language' );
246
247 1
		$this->language = $language;
248 1
	}
249
250
	/**
251
	 * Set payment brand.
252
	 *
253
	 * @param string|null $payment_brand Payment brand.
254
	 * @return void
255
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
256
	 */
257 1
	public function set_payment_brand( $payment_brand ) {
258 1
		DataHelper::validate_null_or_an( $payment_brand, 50, 'Order.paymentBrand' );
259
260 1
		$this->payment_brand = $payment_brand;
261 1
	}
262
263
	/**
264
	 * Set payment brand force.
265
	 *
266
	 * @param string|null $payment_brand_force Payment brand force.
267
	 * @return void
268
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
269
	 */
270 1
	public function set_payment_brand_force( $payment_brand_force ) {
271 1
		DataHelper::validate_null_or_an( $payment_brand_force, 50, 'Order.paymentBrandForce' );
272
273 1
		$this->payment_brand_force = $payment_brand_force;
274 1
	}
275
276
	/**
277
	 * Create and set new order items.
278
	 *
279
	 * @return OrderItems
280
	 */
281 1
	public function new_items() {
282 1
		$this->order_items = new OrderItems();
283
284 1
		return $this->order_items;
285
	}
286
287
	/**
288
	 * Set order items.
289
	 *
290
	 * @param OrderItems|null $order_items Order items.
291
	 * @return void
292
	 */
293
	public function set_order_items( OrderItems $order_items = null ) {
294
		$this->order_items = $order_items;
295
	}
296
297
	/**
298
	 * Set shipping detail.
299
	 *
300
	 * @param Address|null $shipping_detail Shipping address details.
301
	 * @return void
302
	 */
303 1
	public function set_shipping_detail( Address $shipping_detail = null ) {
304 1
		$this->shipping_detail = $shipping_detail;
305 1
	}
306
307
	/**
308
	 * Set billing detail.
309
	 *
310
	 * @param Address|null $billing_detail Billing address details.
311
	 * @return void
312
	 */
313 1
	public function set_billing_detail( Address $billing_detail = null ) {
314 1
		$this->billing_detail = $billing_detail;
315 1
	}
316
317
	/**
318
	 * Set customer information.
319
	 *
320
	 * @param CustomerInformation $customer_information Customer information.
321
	 * @return void
322
	 */
323 1
	public function set_customer_information( CustomerInformation $customer_information ) {
324 1
		$this->customer_information = $customer_information;
325 1
	}
326
327
	/**
328
	 * Get JSON object.
329
	 *
330
	 * @return object
331
	 */
332
	public function jsonSerialize() {
333
		$object = (object) array();
334
335
		$object->timestamp       = $this->timestamp->format( \DATE_ATOM );
336
		$object->merchantOrderId = $this->merchant_order_id;
337
338
		if ( null !== $this->description ) {
339
			$object->description = $this->description;
340
		}
341
342
		if ( null !== $this->order_items ) {
343
			$object->orderItems = $this->order_items;
344
		}
345
346
		$object->amount = $this->amount;
347
348
		if ( null !== $this->shipping_detail ) {
349
			$object->shippingDetail = $this->shipping_detail;
350
		}
351
352
		if ( null !== $this->billing_detail ) {
353
			$object->billingDetail = $this->billing_detail;
354
		}
355
356
		if ( null !== $this->customer_information ) {
357
			$object->customerInformation = $this->customer_information;
358
		}
359
360
		if ( null !== $this->language ) {
361
			$object->language = $this->language;
362
		}
363
364
		$object->merchantReturnURL = $this->merchant_return_url;
365
366
		if ( null !== $this->payment_brand ) {
367
			$object->paymentBrand = $this->payment_brand;
368
		}
369
370
		if ( null !== $this->payment_brand_force ) {
371
			$object->paymentBrandForce = $this->payment_brand_force;
372
		}
373
374
		return $object;
375
	}
376
377
	/**
378
	 * Get signature fields.
379
	 *
380
	 * @param array<string> $fields Fields.
381
	 * @return array<string>
382
	 */
383 2
	public function get_signature_fields( $fields = array() ) {
384 2
		$fields[] = $this->timestamp->format( \DATE_ATOM );
385 2
		$fields[] = $this->merchant_order_id;
386
387 2
		$fields = $this->amount->get_signature_fields( $fields );
388
389 2
		$fields[] = \strval( (string) $this->language );
390 2
		$fields[] = \strval( (string) $this->description );
391 2
		$fields[] = $this->merchant_return_url;
392
393 2
		if ( null !== $this->order_items ) {
394 1
			$fields = $this->order_items->get_signature_fields( $fields );
395
		}
396
397 2
		if ( null !== $this->shipping_detail ) {
398 1
			$fields = $this->shipping_detail->get_signature_fields( $fields );
399
		}
400
401 2
		if ( null !== $this->payment_brand ) {
402 1
			$fields[] = $this->payment_brand;
403
		}
404
405 2
		if ( null !== $this->payment_brand_force ) {
406 1
			$fields[] = $this->payment_brand_force;
407
		}
408
409 2
		if ( null !== $this->customer_information ) {
410 1
			$fields = $this->customer_information->get_signature_fields( $fields );
411
		}
412
413 2
		if ( null !== $this->billing_detail ) {
414 1
			$fields = $this->billing_detail->get_signature_fields( $fields );
415
		}
416
417 2
		return $fields;
418
	}
419
}
420