Failed Conditions
Push — feature/post-pay ( d719f9 )
by Reüel
08:19
created

Order::set_shipping_detail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
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 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 OrderItems $order_items Order items.
204
	 *
205
	 * @return void
206
	 */
207
	public function set_order_items( OrderItems $order_items ) {
208
		$this->order_items = $order_items;
209
	}
210
211
	/**
212
	 * Set items.
213
	 *
214
	 * @param Items $items Payment items.
215
	 *
216
	 * @return void
217
	 */
218
	public function set_items( Items $items ) {
219
		$order_items = new OrderItems();
220
221
		$items = $items->getIterator();
222
223
		while ( $items->valid() ) {
224
			$item = $items->current();
225
226
			// New order item.
227
			$order_item = new OrderItem(
228
				array(
229
					'id'       => $item->get_id(),
230
					'name'     => $item->get_description(),
231
					'quantity' => $item->get_quantity(),
232
					'amount'   => new Money(
233
						$this->amount->get_currency(),
234
						Util::amount_to_cents( $item->get_price() )
235
					),
236
					'category' => ProductCategories::DIGITAL,
237
				)
238
			);
239
240
			// Add order item.
241
			$order_items->add_item( $order_item );
242
243
			$items->next();
244
		}
245
246
		$this->set_order_items( $order_items );
247
	}
248
249
	/**
250
	 * Set shipping detail.
251
	 *
252
	 * @param Address $shipping_detail Shipping address details.
253
	 */
254
	public function set_shipping_detail( $shipping_detail ) {
255
		$this->billing_detail = $shipping_detail;
256
	}
257
258
	/**
259
	 * Set billing detail.
260
	 *
261
	 * @param Address $billing_detail Billing address details.
262
	 */
263
	public function set_billing_detail( $billing_detail ) {
264
		$this->billing_detail = $billing_detail;
265
	}
266
267
	/**
268
	 * Set customer information.
269
	 *
270
	 * @param CustomerInformation $customer_information Customer information.
271
	 */
272
	public function set_customer_information( $customer_information ) {
273
		$this->customer_information = $customer_information;
274
	}
275
276
	/**
277
	 * Get JSON object.
278
	 *
279
	 * @return object
280
	 */
281
	public function get_json() {
282
		$data = array(
283
			'timestamp'       => $this->timestamp,
284
			'merchantOrderId' => $this->merchant_order_id,
285
			'description'     => $this->description,
286
		);
287
288
		if ( $this->order_items instanceof OrderItems ) {
0 ignored issues
show
introduced by
$this->order_items is always a sub-type of Pronamic\WordPress\Pay\G...s\OmniKassa2\OrderItems. If $this->order_items can have other possible types, add them to src/Order.php:52.
Loading history...
289
			$data['orderItems'] = $this->order_items->get_json();
290
		}
291
292
		$data['amount'] = $this->amount->get_json();
293
294
		if ( $this->shipping_detail instanceof Address ) {
0 ignored issues
show
introduced by
$this->shipping_detail is always a sub-type of Pronamic\WordPress\Pay\Gateways\OmniKassa2\Address. If $this->shipping_detail can have other possible types, add them to src/Order.php:66.
Loading history...
295
			$data['shippingDetail'] = $this->shipping_detail->get_json();
296
		}
297
298
		if ( $this->billing_detail instanceof Address ) {
0 ignored issues
show
introduced by
$this->billing_detail is always a sub-type of Pronamic\WordPress\Pay\Gateways\OmniKassa2\Address. If $this->billing_detail can have other possible types, add them to src/Order.php:73.
Loading history...
299
			$data['billingDetail'] = $this->billing_detail->get_json();
300
		}
301
302
		if ( $this->customer_information instanceof CustomerInformation ) {
0 ignored issues
show
introduced by
$this->customer_information is always a sub-type of Pronamic\WordPress\Pay\G...sa2\CustomerInformation. If $this->customer_information can have other possible types, add them to src/Order.php:80.
Loading history...
303
			$data['customerInformation'] = $this->customer_information->get_json();
304
		}
305
306
		$data['language']          = $this->language;
307
		$data['merchantReturnURL'] = $this->merchant_return_url;
308
		$data['paymentBrand']      = $this->payment_brand;
309
		$data['paymentBrandForce'] = $this->payment_brand_force;
310
311
		return (object) $data;
312
	}
313
314
	/**
315
	 * Get signature data.
316
	 *
317
	 * @return array
318
	 */
319
	public function get_signature_data() {
320
		// Required fields.
321
		$fields = array(
322
			$this->timestamp,
323
			$this->merchant_order_id,
324
			$this->amount->get_currency(),
325
			$this->amount->get_amount(),
326
			$this->language,
327
			$this->description,
328
			$this->merchant_return_url,
329
		);
330
331
		// Optional fields.
332
		$order_items          = null;
333
		$shipping_detail      = null;
334
		$customer_information = null;
335
		$billing_detail       = null;
336
337
		if ( $this->order_items instanceof OrderItems ) {
0 ignored issues
show
introduced by
$this->order_items is always a sub-type of Pronamic\WordPress\Pay\G...s\OmniKassa2\OrderItems. If $this->order_items can have other possible types, add them to src/Order.php:52.
Loading history...
338
			$order_items = $this->order_items->get_signature_data();
339
		}
340
341
		if ( $this->shipping_detail instanceof Address ) {
0 ignored issues
show
introduced by
$this->shipping_detail is always a sub-type of Pronamic\WordPress\Pay\Gateways\OmniKassa2\Address. If $this->shipping_detail can have other possible types, add them to src/Order.php:66.
Loading history...
342
			$shipping_detail = $this->shipping_detail->get_signature_data();
343
		}
344
345
		if ( $this->customer_information instanceof CustomerInformation ) {
0 ignored issues
show
introduced by
$this->customer_information is always a sub-type of Pronamic\WordPress\Pay\G...sa2\CustomerInformation. If $this->customer_information can have other possible types, add them to src/Order.php:80.
Loading history...
346
			$customer_information = $this->customer_information->get_signature_data();
347
		}
348
349
		if ( $this->billing_detail instanceof Address ) {
0 ignored issues
show
introduced by
$this->billing_detail is always a sub-type of Pronamic\WordPress\Pay\Gateways\OmniKassa2\Address. If $this->billing_detail can have other possible types, add them to src/Order.php:73.
Loading history...
350
			$billing_detail = $this->billing_detail->get_signature_data();
351
		}
352
353
		$optional = array(
354
			'order_items'          => $order_items,
355
			'shipping_detail'      => $shipping_detail,
356
			'payment_brand'        => $this->payment_brand,
357
			'payment_brand_force'  => $this->payment_brand_force,
358
			'customer_information' => $customer_information,
359
			'billing_detail'       => $billing_detail,
360
		);
361
362
		// Remove empty optional fields.
363
		$optional = array_filter( $optional );
364
365
		return array_merge( $fields, $optional );
366
	}
367
}
368