Passed
Push — feature/post-pay ( faf3dd...ce3905 )
by Remco
04:38
created

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