Passed
Push — feature/post-pay ( 906e88...faf3dd )
by Remco
04:39
created

Order   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Test Coverage

Coverage 67.5%

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 323
ccs 54
cts 80
cp 0.675
rs 10
c 0
b 0
f 0
wmc 27

13 Methods

Rating   Name   Duplication   Size   Complexity  
A set_billing_detail() 0 2 1
A set_shipping_detail() 0 2 1
A set_payment_brand_force() 0 2 1
C get_json() 0 43 9
A set_payment_brand() 0 2 1
A __construct() 0 5 1
A set_description() 0 2 1
A set_timestamp() 0 2 1
A new_items() 0 4 1
A set_order_items() 0 2 1
A set_customer_information() 0 2 1
B get_signature_fields() 0 34 7
A set_language() 0 2 1
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->timestamp           = new DateTime();
159 2
		$this->merchant_order_id   = $merchant_order_id;
160 2
		$this->amount              = $amount;
161 2
		$this->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 description.
175
	 *
176
	 * @param string $description Description.
177
	 */
178 1
	public function set_description( $description ) {
179 1
		$this->description = $description;
180 1
	}
181
182
	/**
183
	 * Set language.
184
	 *
185
	 * @param string $language Language.
186
	 */
187 1
	public function set_language( $language ) {
188 1
		$this->language = $language;
189 1
	}
190
191
	/**
192
	 * Set payment brand.
193
	 *
194
	 * @param string|null $payment_brand Payment brand.
195
	 */
196 1
	public function set_payment_brand( $payment_brand ) {
197 1
		$this->payment_brand = $payment_brand;
198 1
	}
199
200
	/**
201
	 * Set payment brand force.
202
	 *
203
	 * @param string $payment_brand_force Payment brand force.
204
	 */
205 1
	public function set_payment_brand_force( $payment_brand_force ) {
206 1
		$this->payment_brand_force = $payment_brand_force;
207 1
	}
208
209
	/**
210
	 * Create and set new order items.
211
	 *
212
	 * @return OrderItems
213
	 */
214 1
	public function new_items() {
215 1
		$this->order_items = new OrderItems();
216
217 1
		return $this->order_items;
218
	}
219
220
	/**
221
	 * Set order items.
222
	 *
223
	 * @param OrderItems|null $order_items Order items.
224
	 */
225
	public function set_order_items( OrderItems $order_items = null ) {
226
		$this->order_items = $order_items;
227
	}
228
229
	/**
230
	 * Set shipping detail.
231
	 *
232
	 * @param Address|null $shipping_detail Shipping address details.
233
	 */
234 1
	public function set_shipping_detail( Address $shipping_detail = null ) {
235 1
		$this->shipping_detail = $shipping_detail;
236 1
	}
237
238
	/**
239
	 * Set billing detail.
240
	 *
241
	 * @param Address|null $billing_detail Billing address details.
242
	 */
243 1
	public function set_billing_detail( Address $billing_detail = null ) {
244 1
		$this->billing_detail = $billing_detail;
245 1
	}
246
247
	/**
248
	 * Set customer information.
249
	 *
250
	 * @param CustomerInformation $customer_information Customer information.
251
	 */
252 1
	public function set_customer_information( CustomerInformation $customer_information ) {
253 1
		$this->customer_information = $customer_information;
254 1
	}
255
256
	/**
257
	 * Get JSON object.
258
	 *
259
	 * @return object
260
	 */
261
	public function get_json() {
262
		$object = (object) array();
263
264
		$object->timestamp       = $this->timestamp->format( DATE_ATOM );
265
		$object->merchantOrderId = $this->merchant_order_id;
266
267
		if ( null !== $this->description ) {
268
			$object->description = $this->description;
269
		}
270
271
		if ( null !== $this->order_items ) {
272
			$object->orderItems = $this->order_items->get_json();
273
		}
274
275
		$object->amount = $this->amount->get_json();
276
277
		if ( null !== $this->shipping_detail ) {
278
			$object->shippingDetail = $this->shipping_detail->get_json();
279
		}
280
281
		if ( null !== $this->billing_detail ) {
282
			$object->billingDetail = $this->billing_detail->get_json();
283
		}
284
285
		if ( null !== $this->customer_information ) {
286
			$object->customerInformation = $this->customer_information->get_json();
287
		}
288
289
		if ( null !== $this->language ) {
290
			$object->language = $this->language;
291
		}
292
293
		$object->merchantReturnURL = $this->merchant_return_url;
294
295
		if ( null !== $this->payment_brand ) {
296
			$object->paymentBrand = $this->payment_brand;
297
		}
298
299
		if ( null !== $this->payment_brand_force ) {
300
			$object->paymentBrandForce = $this->payment_brand_force;
301
		}
302
303
		return $object;
304
	}
305
306
	/**
307
	 * Get signature fields.
308
	 *
309
	 * @param array $fields Fields.
310
	 * @return array
311
	 */
312 2
	public function get_signature_fields( $fields = array() ) {
313 2
		$fields[] = $this->timestamp->format( DATE_ATOM );
314 2
		$fields[] = $this->merchant_order_id;
315 2
		$fields[] = $this->amount->get_currency();
316 2
		$fields[] = $this->amount->get_amount();
317 2
		$fields[] = $this->language;
318 2
		$fields[] = $this->description;
319 2
		$fields[] = $this->merchant_return_url;
320
321 2
		if ( null !== $this->order_items ) {
322 1
			$fields = $this->order_items->get_signature_fields( $fields );
323
		}
324
325 2
		if ( null !== $this->shipping_detail ) {
326 1
			$fields = $this->shipping_detail->get_signature_fields( $fields );
327
		}
328
329 2
		if ( null !== $this->payment_brand ) {
330 1
			$fields[] = $this->payment_brand;
331
		}
332
333 2
		if ( null !== $this->payment_brand_force ) {
334 1
			$fields[] = $this->payment_brand_force;
335
		}
336
337 2
		if ( null !== $this->customer_information ) {
338 1
			$fields = $this->customer_information->get_signature_fields( $fields );
339
		}
340
341 2
		if ( null !== $this->billing_detail ) {
342 1
			$fields = $this->billing_detail->get_signature_fields( $fields );
343
		}
344
345 2
		return $fields;
346
	}
347
}
348