Passed
Push — feature/post-pay ( 12bb6b...7e10da )
by Remco
04:44
created

Order::set_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
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.0.2
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
		$this->description = $description;
217 1
	}
218
219
	/**
220
	 * Set language.
221
	 *
222
	 * @param string|null $language Language.
223
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..2`.
224
	 */
225 1
	public function set_language( $language ) {
226 1
		DataHelper::validate_null_or_an( $language, 2 );
227
228 1
		$this->language = $language;
229 1
	}
230
231
	/**
232
	 * Set payment brand.
233
	 *
234
	 * @param string|null $payment_brand Payment brand.
235
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
236
	 */
237 1
	public function set_payment_brand( $payment_brand ) {
238 1
		DataHelper::validate_null_or_an( $payment_brand, 50 );
239
240 1
		$this->payment_brand = $payment_brand;
241 1
	}
242
243
	/**
244
	 * Set payment brand force.
245
	 *
246
	 * @param string|null $payment_brand_force Payment brand force.
247
	 * @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..50`.
248
	 */
249 1
	public function set_payment_brand_force( $payment_brand_force ) {
250 1
		DataHelper::validate_null_or_an( $payment_brand_force, 50 );
251
252 1
		$this->payment_brand_force = $payment_brand_force;
253 1
	}
254
255
	/**
256
	 * Create and set new order items.
257
	 *
258
	 * @return OrderItems
259
	 */
260 1
	public function new_items() {
261 1
		$this->order_items = new OrderItems();
262
263 1
		return $this->order_items;
264
	}
265
266
	/**
267
	 * Set order items.
268
	 *
269
	 * @param OrderItems|null $order_items Order items.
270
	 */
271
	public function set_order_items( OrderItems $order_items = null ) {
272
		$this->order_items = $order_items;
273
	}
274
275
	/**
276
	 * Set shipping detail.
277
	 *
278
	 * @param Address|null $shipping_detail Shipping address details.
279
	 */
280 1
	public function set_shipping_detail( Address $shipping_detail = null ) {
281 1
		$this->shipping_detail = $shipping_detail;
282 1
	}
283
284
	/**
285
	 * Set billing detail.
286
	 *
287
	 * @param Address|null $billing_detail Billing address details.
288
	 */
289 1
	public function set_billing_detail( Address $billing_detail = null ) {
290 1
		$this->billing_detail = $billing_detail;
291 1
	}
292
293
	/**
294
	 * Set customer information.
295
	 *
296
	 * @param CustomerInformation $customer_information Customer information.
297
	 */
298 1
	public function set_customer_information( CustomerInformation $customer_information ) {
299 1
		$this->customer_information = $customer_information;
300 1
	}
301
302
	/**
303
	 * Get JSON object.
304
	 *
305
	 * @return object
306
	 */
307
	public function get_json() {
308
		$object = (object) array();
309
310
		$object->timestamp       = $this->timestamp->format( DATE_ATOM );
311
		$object->merchantOrderId = $this->merchant_order_id;
312
313
		if ( null !== $this->description ) {
314
			$object->description = $this->description;
315
		}
316
317
		if ( null !== $this->order_items ) {
318
			$object->orderItems = $this->order_items->get_json();
319
		}
320
321
		$object->amount = $this->amount->get_json();
322
323
		if ( null !== $this->shipping_detail ) {
324
			$object->shippingDetail = $this->shipping_detail->get_json();
325
		}
326
327
		if ( null !== $this->billing_detail ) {
328
			$object->billingDetail = $this->billing_detail->get_json();
329
		}
330
331
		if ( null !== $this->customer_information ) {
332
			$object->customerInformation = $this->customer_information->get_json();
333
		}
334
335
		if ( null !== $this->language ) {
336
			$object->language = $this->language;
337
		}
338
339
		$object->merchantReturnURL = $this->merchant_return_url;
340
341
		if ( null !== $this->payment_brand ) {
342
			$object->paymentBrand = $this->payment_brand;
343
		}
344
345
		if ( null !== $this->payment_brand_force ) {
346
			$object->paymentBrandForce = $this->payment_brand_force;
347
		}
348
349
		$object->signature = $this->get_signature();
350
351
		return $object;
352
	}
353
354
	/**
355
	 * Get signature fields.
356
	 *
357
	 * @param array $fields Fields.
358
	 * @return array
359
	 */
360 2
	public function get_signature_fields( $fields = array() ) {
361 2
		$fields[] = $this->timestamp->format( DATE_ATOM );
362 2
		$fields[] = $this->merchant_order_id;
363
364 2
		$fields = $this->amount->get_signature_fields( $fields );
365
366 2
		$fields[] = $this->language;
367 2
		$fields[] = $this->description;
368 2
		$fields[] = $this->merchant_return_url;
369
370 2
		if ( null !== $this->order_items ) {
371 1
			$fields = $this->order_items->get_signature_fields( $fields );
372
		}
373
374 2
		if ( null !== $this->shipping_detail ) {
375 1
			$fields = $this->shipping_detail->get_signature_fields( $fields );
376
		}
377
378 2
		if ( null !== $this->payment_brand ) {
379 1
			$fields[] = $this->payment_brand;
380
		}
381
382 2
		if ( null !== $this->payment_brand_force ) {
383 1
			$fields[] = $this->payment_brand_force;
384
		}
385
386 2
		if ( null !== $this->customer_information ) {
387 1
			$fields = $this->customer_information->get_signature_fields( $fields );
388
		}
389
390 2
		if ( null !== $this->billing_detail ) {
391 1
			$fields = $this->billing_detail->get_signature_fields( $fields );
392
		}
393
394 2
		return $fields;
395
	}
396
}
397