Failed Conditions
Push — feature/post-pay ( 4d43ec...e3663d )
by Remco
04:52
created

Order::get_signature_data()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 64
nop 1
dl 0
loc 32
ccs 0
cts 25
cp 0
crap 56
rs 8.8333
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\Payments\PaymentLines;
14
15
/**
16
 * Order
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.0.2
20
 * @since   1.0.0
21
 */
22
class Order extends Message {
23
	/**
24
	 * ISO 8601 standard Date / time on which the order is announced at ROK.
25
	 * As a rule, this is the current date / time.
26
	 *
27
	 * This field is mandatory and provides protection against so-called
28
	 * replay (playback) attacks
29
	 *
30
	 * @var string
31
	 */
32
	private $timestamp;
33
34
	/**
35
	 * Generated by Merchant. If your webshop wants to use AfterPay, this field must be unique.
36
	 *
37
	 * @var string
38
	 */
39
	private $merchant_order_id;
40
41
	/**
42
	 * Description of the order.
43
	 *
44
	 * @var string
45
	 */
46
	private $description;
47
48
	/**
49
	 * The order items.
50
	 *
51
	 * @var OrderItems
52
	 */
53
	private $order_items;
54
55
	/**
56
	 * Amount.
57
	 *
58
	 * @var Money
59
	 */
60
	private $amount;
61
62
	/**
63
	 * The shipping address.
64
	 *
65
	 * @var Address
66
	 */
67
	private $shipping_detail;
68
69
	/**
70
	 * The billing address.
71
	 *
72
	 * @var Address
73
	 */
74
	private $billing_detail;
75
76
	/**
77
	 * The customer information.
78
	 *
79
	 * @var CustomerInformation
80
	 */
81
	private $customer_information;
82
83
	/**
84
	 * Language.
85
	 *
86
	 * ISO 639-1 standard. Not Case sensitive.
87
	 *
88
	 * @var string
89
	 */
90
	private $language;
91
92
	/**
93
	 * Merchant return URL.
94
	 *
95
	 * The URL to which the consumer's browser will be sent after the payment.
96
	 *
97
	 * @var string
98
	 */
99
	private $merchant_return_url;
100
101
	/**
102
	 * Payment brand.
103
	 *
104
	 * This field is optional and is used to enforce a specific
105
	 * payment method with the consumer instead of the consumer
106
	 * selecting a payment method on the payment method selection
107
	 * page.
108
	 *
109
	 * Valid values are:
110
	 * • IDEAL
111
	 * • AFTERPAY
112
	 * • PAYPAL
113
	 * • MASTERCARD
114
	 * • VISA
115
	 * • BANCONTACT
116
	 * • MAESTRO
117
	 * • V_PAY
118
	 * • CARDS
119
	 *
120
	 * The CARDS value ensures that the consumer can choose
121
	 * between payment methods: MASTERCARD, VISA, BANCONTACT,
122
	 * MAESTRO and V_PAY
123
	 *
124
	 * @var string|null
125
	 */
126
	private $payment_brand;
127
128
	/**
129
	 * Payment brand force.
130
	 *
131
	 * This field should only be delivered if the paymentBrand field (see
132
	 * above) is also specified. This field can be used to send or, after
133
	 * a failed payment, the consumer can or can not select another payment
134
	 * method to still pay the payment.
135
	 *
136
	 * Valid values are:
137
	 * • FORCE_ONCE
138
	 * • FORCE_ALWAYS
139
	 *
140
	 * In the case of FORCE_ONCE, the indicated paymentBrand is only
141
	 * enforced on the first transaction. If this fails, the consumer
142
	 * can still choose another payment method. When FORCE_ALWAYS is
143
	 * chosen, the consumer can not choose another payment method.
144
	 *
145
	 * @var string|null
146
	 */
147
	private $payment_brand_force;
148
149
	/**
150
	 * Construct order.
151
	 *
152
	 * @param string $merchant_order_id    Merchant order ID.
153
	 * @param Money  $amount               Amount.
154
	 * @param string $merchant_return_url  Merchant return URL.
155
	 */
156
	public function __construct( $merchant_order_id, $amount, $merchant_return_url ) {
157
		$this->timestamp           = date( DATE_ATOM );
158
		$this->merchant_order_id   = $merchant_order_id;
159
		$this->amount              = $amount;
160
		$this->merchant_return_url = $merchant_return_url;
161
	}
162
163
	/**
164
	 * Set description.
165
	 *
166
	 * @param string $description Description.
167
	 */
168
	public function set_description( $description ) {
169
		$this->description = $description;
170
	}
171
172
	/**
173
	 * Set language.
174
	 *
175
	 * @param string $language Language.
176
	 */
177
	public function set_language( $language ) {
178
		$this->language = $language;
179
	}
180
181
	/**
182
	 * Set payment brand.
183
	 *
184
	 * @param string|null $payment_brand Payment brand.
185
	 */
186
	public function set_payment_brand( $payment_brand ) {
187
		$this->payment_brand = $payment_brand;
188
	}
189
190
	/**
191
	 * Set payment brand force.
192
	 *
193
	 * @param string $payment_brand_force Payment brand force.
194
	 */
195
	public function set_payment_brand_force( $payment_brand_force ) {
196
		$this->payment_brand_force = $payment_brand_force;
197
	}
198
199
	/**
200
	 * Set order items.
201
	 *
202
	 * @param OrderItems|null $order_items Order items.
203
	 */
204
	public function set_order_items( OrderItems $order_items = null ) {
205
		$this->order_items = $order_items;
206
	}
207
208
	/**
209
	 * Set shipping detail.
210
	 *
211
	 * @param Address $shipping_detail Shipping address details.
212
	 */
213
	public function set_shipping_detail( Address $shipping_detail ) {
214
		$this->shipping_detail = $shipping_detail;
215
	}
216
217
	/**
218
	 * Set billing detail.
219
	 *
220
	 * @param Address $billing_detail Billing address details.
221
	 */
222
	public function set_billing_detail( Address $billing_detail ) {
223
		$this->billing_detail = $billing_detail;
224
	}
225
226
	/**
227
	 * Set customer information.
228
	 *
229
	 * @param CustomerInformation $customer_information Customer information.
230
	 */
231
	public function set_customer_information( CustomerInformation $customer_information ) {
232
		$this->customer_information = $customer_information;
233
	}
234
235
	/**
236
	 * Get JSON object.
237
	 *
238
	 * @return object
239
	 */
240
	public function get_json() {
241
		$data = array(
242
			'timestamp'       => $this->timestamp,
243
			'merchantOrderId' => $this->merchant_order_id,
244
			'description'     => $this->description,
245
		);
246
247
		if ( null !== $this->order_items ) {
248
			$data['orderItems'] = $this->order_items->get_json();
249
		}
250
251
		$data['amount'] = $this->amount->get_json();
252
253
		if ( null !== $this->shipping_detail ) {
254
			$data['shippingDetail'] = $this->shipping_detail->get_json();
255
		}
256
257
		if ( null !== $this->billing_detail ) {
258
			$data['billingDetail'] = $this->billing_detail->get_json();
259
		}
260
261
		if ( null !== $this->customer_information ) {
262
			$data['customerInformation'] = $this->customer_information->get_json();
263
		}
264
265
		$data['language']          = $this->language;
266
		$data['merchantReturnURL'] = $this->merchant_return_url;
267
		$data['paymentBrand']      = $this->payment_brand;
268
		$data['paymentBrandForce'] = $this->payment_brand_force;
269
270
		return (object) $data;
271
	}
272
273
	/**
274
	 * Get signature data.
275
	 *
276
	 * @param array $data Data.
277
	 * @return array
278
	 */
279
	public function get_signature_data( $data = array() ) {
280
		// Required fields.
281
		$fields = array(
282
			$this->timestamp,
283
			$this->merchant_order_id,
284
			$this->amount->get_currency(),
285
			$this->amount->get_amount(),
286
			empty( $this->language ) ? '' : $this->language,
287
			empty( $this->description ) ? '' : $this->description,
288
			$this->merchant_return_url,
289
		);
290
291
		if ( null !== $this->order_items ) {
292
			$fields = array_merge( $fields, $this->order_items->get_signature_data() );
293
		}
294
295
		if ( null !== $this->shipping_detail ) {
296
			$fields = array_merge( $fields, $this->shipping_detail->get_signature_data() );
297
		}
298
299
		$fields[] = $this->payment_brand;
300
		$fields[] = $this->payment_brand_force;
301
302
		if ( null !== $this->customer_information ) {
303
			$fields = $this->customer_information->get_signature_data( $fields );
304
		}
305
306
		if ( null !== $this->billing_detail ) {
307
			$fields = $this->billing_detail->get_signature_data( $fields );
308
		}
309
310
		return $fields;
311
	}
312
}
313