Failed Conditions
Push — master ( f8b3b2...e22298 )
by Remco
21:14 queued 10:25
created

Order   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 385
Duplicated Lines 0 %

Test Coverage

Coverage 72.16%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 80
c 6
b 0
f 0
dl 0
loc 385
ccs 70
cts 97
cp 0.7216
rs 9.92
wmc 31

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set_timestamp() 0 2 1
A set_billing_detail() 0 2 1
A set_shipping_detail() 0 2 1
A set_payment_brand_force() 0 4 1
A set_merchant_return_url() 0 4 1
C get_json() 0 45 9
A set_payment_brand() 0 4 1
A set_merchant_order_id() 0 4 1
A set_description() 0 4 1
A set_amount() 0 2 1
A new_items() 0 4 1
A set_order_items() 0 2 1
A set_customer_information() 0 2 1
A get_merchant_order_id() 0 2 1
B get_signature_fields() 0 35 7
A set_language() 0 4 1
1
<?php
2
/**
3
 * Order
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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.1.9
20
 * @since   1.0.0
21
 */
22
class Order extends Message {
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
	 */
156 5
	public function __construct( $merchant_order_id, $amount, $merchant_return_url ) {
157 5
		$this->set_timestamp( new DateTime() );
158 5
		$this->set_merchant_order_id( $merchant_order_id );
159 3
		$this->set_amount( $amount );
160 3
		$this->set_merchant_return_url( $merchant_return_url );
161 3
	}
162
163
	/**
164
	 * Set timestamp.
165
	 *
166
	 * @param DateTime $timestamp Timestamp.
167
	 */
168 5
	public function set_timestamp( DateTime $timestamp ) {
169 5
		$this->timestamp = $timestamp;
170 5
	}
171
172
	/**
173
	 * Get merchant order ID.
174
	 *
175
	 * @return string
176
	 */
177 1
	public function get_merchant_order_id() {
178 1
		return $this->merchant_order_id;
179
	}
180
181
	/**
182
	 * Set merchant order ID.
183
	 *
184
	 * Generated by Merchant. If you want your webshop to use AfterPay, this field must be unique.
185
	 * If the ID contains more than 24 characters, the extra characters are removed after the 24th character.
186
	 *
187
	 * @param string $merchant_order_id Merchant order ID.
188
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 10`.
189
	 */
190 5
	public function set_merchant_order_id( $merchant_order_id ) {
191 5
		DataHelper::validate_ans( $merchant_order_id, 24, 'Order.merchantOrderId' );
192
193 3
		$this->merchant_order_id = $merchant_order_id;
194 3
	}
195
196
	/**
197
	 * Set amount.
198
	 *
199
	 * @param Money $amount Amount.
200
	 */
201 3
	public function set_amount( Money $amount ) {
202 3
		$this->amount = $amount;
203 3
	}
204
205
	/**
206
	 * Set merchant return URL.
207
	 *
208
	 * The URL to which the consumer's browser will be sent after the payment.
209
	 *
210
	 * @param string $url Merchant return URL.
211
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 1024`.
212
	 */
213 3
	public function set_merchant_return_url( $url ) {
214 3
		DataHelper::validate_an( $url, 1024, 'Order.merchantReturnURL' );
215
216 3
		$this->merchant_return_url = $url;
217 3
	}
218
219
	/**
220
	 * Set description.
221
	 *
222
	 * @param string|null $description Description.
223
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 35`.
224
	 */
225 1
	public function set_description( $description ) {
226 1
		DataHelper::validate_null_or_an( $description, 35, 'Order.description' );
227
228 1
		$this->description = $description;
229 1
	}
230
231
	/**
232
	 * Set language.
233
	 *
234
	 * @param string|null $language Language (ISO 3166-1 alpha-2).
235
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..2`.
236
	 */
237 1
	public function set_language( $language ) {
238 1
		DataHelper::validate_null_or_an( $language, 2, 'Order.language' );
239
240 1
		$this->language = $language;
241 1
	}
242
243
	/**
244
	 * Set payment brand.
245
	 *
246
	 * @param string|null $payment_brand Payment brand.
247
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
248
	 */
249 1
	public function set_payment_brand( $payment_brand ) {
250 1
		DataHelper::validate_null_or_an( $payment_brand, 50, 'Order.paymentBrand' );
251
252 1
		$this->payment_brand = $payment_brand;
253 1
	}
254
255
	/**
256
	 * Set payment brand force.
257
	 *
258
	 * @param string|null $payment_brand_force Payment brand force.
259
	 * @throws \InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
260
	 */
261 1
	public function set_payment_brand_force( $payment_brand_force ) {
262 1
		DataHelper::validate_null_or_an( $payment_brand_force, 50, 'Order.paymentBrandForce' );
263
264 1
		$this->payment_brand_force = $payment_brand_force;
265 1
	}
266
267
	/**
268
	 * Create and set new order items.
269
	 *
270
	 * @return OrderItems
271
	 */
272 1
	public function new_items() {
273 1
		$this->order_items = new OrderItems();
274
275 1
		return $this->order_items;
276
	}
277
278
	/**
279
	 * Set order items.
280
	 *
281
	 * @param OrderItems|null $order_items Order items.
282
	 */
283
	public function set_order_items( OrderItems $order_items = null ) {
284
		$this->order_items = $order_items;
285
	}
286
287
	/**
288
	 * Set shipping detail.
289
	 *
290
	 * @param Address|null $shipping_detail Shipping address details.
291
	 */
292 1
	public function set_shipping_detail( Address $shipping_detail = null ) {
293 1
		$this->shipping_detail = $shipping_detail;
294 1
	}
295
296
	/**
297
	 * Set billing detail.
298
	 *
299
	 * @param Address|null $billing_detail Billing address details.
300
	 */
301 1
	public function set_billing_detail( Address $billing_detail = null ) {
302 1
		$this->billing_detail = $billing_detail;
303 1
	}
304
305
	/**
306
	 * Set customer information.
307
	 *
308
	 * @param CustomerInformation $customer_information Customer information.
309
	 */
310 1
	public function set_customer_information( CustomerInformation $customer_information ) {
311 1
		$this->customer_information = $customer_information;
312 1
	}
313
314
	/**
315
	 * Get JSON object.
316
	 *
317
	 * @return object
318
	 */
319
	public function get_json() {
320
		$object = (object) array();
321
322
		$object->timestamp       = $this->timestamp->format( \DATE_ATOM );
323
		$object->merchantOrderId = $this->merchant_order_id;
324
325
		if ( null !== $this->description ) {
326
			$object->description = $this->description;
327
		}
328
329
		if ( null !== $this->order_items ) {
330
			$object->orderItems = $this->order_items->get_json();
331
		}
332
333
		$object->amount = $this->amount->get_json();
334
335
		if ( null !== $this->shipping_detail ) {
336
			$object->shippingDetail = $this->shipping_detail->get_json();
337
		}
338
339
		if ( null !== $this->billing_detail ) {
340
			$object->billingDetail = $this->billing_detail->get_json();
341
		}
342
343
		if ( null !== $this->customer_information ) {
344
			$object->customerInformation = $this->customer_information->get_json();
345
		}
346
347
		if ( null !== $this->language ) {
348
			$object->language = $this->language;
349
		}
350
351
		$object->merchantReturnURL = $this->merchant_return_url;
352
353
		if ( null !== $this->payment_brand ) {
354
			$object->paymentBrand = $this->payment_brand;
355
		}
356
357
		if ( null !== $this->payment_brand_force ) {
358
			$object->paymentBrandForce = $this->payment_brand_force;
359
		}
360
361
		$object->signature = $this->get_signature();
362
363
		return $object;
364
	}
365
366
	/**
367
	 * Get signature fields.
368
	 *
369
	 * @param array<string> $fields Fields.
370
	 * @return array<string>
371
	 */
372 2
	public function get_signature_fields( $fields = array() ) {
373 2
		$fields[] = $this->timestamp->format( \DATE_ATOM );
374 2
		$fields[] = $this->merchant_order_id;
375
376 2
		$fields = $this->amount->get_signature_fields( $fields );
377
378 2
		$fields[] = $this->language;
379 2
		$fields[] = $this->description;
380 2
		$fields[] = $this->merchant_return_url;
381
382 2
		if ( null !== $this->order_items ) {
383 1
			$fields = $this->order_items->get_signature_fields( $fields );
384
		}
385
386 2
		if ( null !== $this->shipping_detail ) {
387 1
			$fields = $this->shipping_detail->get_signature_fields( $fields );
388
		}
389
390 2
		if ( null !== $this->payment_brand ) {
391 1
			$fields[] = $this->payment_brand;
392
		}
393
394 2
		if ( null !== $this->payment_brand_force ) {
395 1
			$fields[] = $this->payment_brand_force;
396
		}
397
398 2
		if ( null !== $this->customer_information ) {
399 1
			$fields = $this->customer_information->get_signature_fields( $fields );
400
		}
401
402 2
		if ( null !== $this->billing_detail ) {
403 1
			$fields = $this->billing_detail->get_signature_fields( $fields );
404
		}
405
406 2
		return $fields;
407
	}
408
}
409