Test Failed
Push — feature/post-pay ( d719f9...02b246 )
by Reüel
06:15
created

Order::set_items()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 29
rs 9.7333
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Order::set_shipping_detail() 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 Pronamic\WordPress\Pay\Core\Util;
14
use Pronamic\WordPress\Pay\Payments\Items;
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 string
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
	public function __construct( $merchant_order_id, $amount, $merchant_return_url ) {
158
		$this->timestamp           = date( DATE_ATOM );
159
		$this->merchant_order_id   = $merchant_order_id;
160
		$this->amount              = $amount;
161
		$this->merchant_return_url = $merchant_return_url;
162
	}
163
164
	/**
165
	 * Set description.
166
	 *
167
	 * @param string $description Description.
168
	 */
169
	public function set_description( $description ) {
170
		$this->description = $description;
171
	}
172
173
	/**
174
	 * Set language.
175
	 *
176
	 * @param string $language Language.
177
	 */
178
	public function set_language( $language ) {
179
		$this->language = $language;
180
	}
181
182
	/**
183
	 * Set payment brand.
184
	 *
185
	 * @param string|null $payment_brand Payment brand.
186
	 */
187
	public function set_payment_brand( $payment_brand ) {
188
		$this->payment_brand = $payment_brand;
189
	}
190
191
	/**
192
	 * Set payment brand force.
193
	 *
194
	 * @param string $payment_brand_force Payment brand force.
195
	 */
196
	public function set_payment_brand_force( $payment_brand_force ) {
197
		$this->payment_brand_force = $payment_brand_force;
198
	}
199
200
	/**
201
	 * Set order items.
202
	 *
203
	 * @param Items $items Payment items.
204
	 *
205
	 * @return void
206
	 */
207
	public function set_order_items( Items $items ) {
208
		$order_items = new OrderItems();
209
210
		$items = $items->getIterator();
211
212
		while ( $items->valid() ) {
213
			$item = $items->current();
214
215
			// New order item.
216
			$order_item = new OrderItem(
217
				array(
218
					'id'       => $item->get_id(),
219
					'name'     => $item->get_description(),
220
					'quantity' => $item->get_quantity(),
221
					'amount'   => new Money(
222
						$this->amount->get_currency(),
223
						Util::amount_to_cents( $item->get_price() )
224
					),
225
					'category' => ProductCategories::DIGITAL,
226
				)
227
			);
228
229
			// Add order item.
230
			$order_items->add_item( $order_item );
231
232
			$items->next();
233
		}
234
235
		$this->order_items = $order_items;
236
	}
237
238
	/**
239
	 * Set shipping detail.
240
	 *
241
	 * @param Address $shipping_detail Shipping address details.
242
	 */
243
	public function set_shipping_detail( Address $shipping_detail ) {
244
		$this->shipping_detail = $shipping_detail;
245
	}
246
247
	/**
248
	 * Set billing detail.
249
	 *
250
	 * @param Address $billing_detail Billing address details.
251
	 */
252
	public function set_billing_detail( Address $billing_detail ) {
253
		$this->billing_detail = $billing_detail;
254
	}
255
256
	/**
257
	 * Set customer information.
258
	 *
259
	 * @param CustomerInformation $customer_information Customer information.
260
	 */
261
	public function set_customer_information( CustomerInformation $customer_information ) {
262
		$this->customer_information = $customer_information;
263
	}
264
265
	/**
266
	 * Get JSON object.
267
	 *
268
	 * @return object
269
	 */
270
	public function get_json() {
271
		$data = array(
272
			'timestamp'       => $this->timestamp,
273
			'merchantOrderId' => $this->merchant_order_id,
274
			'description'     => $this->description,
275
		);
276
277
		if ( null !== $this->order_items ) {
278
			$data['orderItems'] = $this->order_items->get_json();
279
		}
280
281
		$data['amount'] = $this->amount->get_json();
282
283
		if ( null !== $this->shipping_detail ) {
284
			$data['shippingDetail'] = $this->shipping_detail->get_json();
285
		}
286
287
		if ( null !== $this->billing_detail ) {
288
			$data['billingDetail'] = $this->billing_detail->get_json();
289
		}
290
291
		if ( null !== $this->customer_information ) {
292
			$data['customerInformation'] = $this->customer_information->get_json();
293
		}
294
295
		$data['language']          = $this->language;
296
		$data['merchantReturnURL'] = $this->merchant_return_url;
297
		$data['paymentBrand']      = $this->payment_brand;
298
		$data['paymentBrandForce'] = $this->payment_brand_force;
299
300
		return (object) $data;
301
	}
302
303
	/**
304
	 * Get signature data.
305
	 *
306
	 * @return array
307
	 */
308
	public function get_signature_data() {
309
		// Required fields.
310
		$fields = array(
311
			$this->timestamp,
312
			$this->merchant_order_id,
313
			$this->amount->get_currency(),
314
			$this->amount->get_amount(),
315
			$this->language,
316
			$this->description,
317
			$this->merchant_return_url,
318
		);
319
320
		// Optional fields.
321
		$optional = array();
322
323
		if ( null !== $this->order_items ) {
324
			$optional['order_items'] = $this->order_items->get_signature_data();
325
		}
326
327
		if ( null !== $this->shipping_detail ) {
328
			$optional['shipping_detail'] = $this->shipping_detail->get_signature_data();
329
		}
330
331
		$optional['payment_brand']       = $this->payment_brand;
332
		$optional['payment_brand_force'] = $this->payment_brand_force;
333
334
		if ( null !== $this->customer_information ) {
335
			$optional['customer_information'] = $this->customer_information->get_signature_data();
336
		}
337
338
		if ( null !== $this->billing_detail ) {
339
			$optional['billing_detail'] = $this->billing_detail->get_signature_data();
340
		}
341
342
		// Remove empty optional fields.
343
		$optional = array_filter( $optional );
344
345
		return array_merge( $fields, $optional );
346
	}
347
}
348