Passed
Push — master ( 99a753...df0863 )
by Remco
19:34 queued 09:07
created

Order::get_signature_fields()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

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