Passed
Push — develop ( 5f0aa1...7492b5 )
by Remco
04:22
created

AbstractPaymentRequest::new_line_items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Abstract payment request
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use DateTime;
14
use InvalidArgumentException;
15
16
/**
17
 * Abstract payment request
18
 *
19
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
20
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession
21
 *
22
 * @author  Remco Tolsma
23
 * @version 1.0.0
24
 * @since   1.0.0
25
 */
26
abstract class AbstractPaymentRequest extends Request {
27
	/**
28
	 * Amount.
29
	 *
30
	 * @var Amount
31
	 */
32
	private $amount;
33
34
	/**
35
	 * Billing address.
36
	 *
37
	 * @var Address|null
38
	 */
39
	private $billing_address;
40
41
	/**
42
	 * Channel.
43
	 *
44
	 * The platform where a payment transaction takes place. This field is optional for filtering out
45
	 * payment methods that are only available on specific platforms. If this value is not set,
46
	 * then we will try to infer it from the sdkVersion or token.
47
	 *
48
	 * Possible values: Android, iOS, Web.
49
	 *
50
	 * @var string|null
51
	 */
52
	private $channel;
53
54
	/**
55
	 * The shopper country.
56
	 *
57
	 * Format: ISO 3166-1 alpha-2 Example: NL or DE
58
	 *
59
	 * @var string|null
60
	 */
61
	private $country_code;
62
63
	/**
64
	 * Date of birth.
65
	 *
66
	 * @var DateTime|null
67
	 */
68
	private $date_of_birth;
69
70
	/**
71
	 * The address where the purchased goods should be delivered
72
	 *
73
	 * @var Address|null
74
	 */
75
	private $delivery_address;
76
77
	/**
78
	 * Line items regarding the payment.
79
	 *
80
	 * @var LineItems|null
81
	 */
82
	private $line_items;
83
84
	/**
85
	 * The merchant account identifier, with which you want to process the transaction.
86
	 *
87
	 * @var string
88
	 */
89
	private $merchant_account;
90
91
	/**
92
	 * The reference to uniquely identify a payment. This reference is used in all communication
93
	 * with you about the payment status. We recommend using a unique value per payment;
94
	 * however, it is not a requirement. If you need to provide multiple references for
95
	 * a transaction, separate them with hyphens ("-"). Maximum length: 80 characters.
96
	 *
97
	 * @var string
98
	 */
99
	private $reference;
100
101
	/**
102
	 * The URL to return to.
103
	 *
104
	 * @var string
105
	 */
106
	private $return_url;
107
108
	/**
109
	 * The shopper's IP address.
110
	 *
111
	 * @var string|null
112
	 */
113
	private $shopper_ip;
114
115
	/**
116
	 * The combination of a language code and a country code to specify the language to be used in the payment.
117
	 *
118
	 * @var string|null
119
	 */
120
	private $shopper_locale;
121
122
	/**
123
	 * The shopper's full name and gender (if specified)
124
	 *
125
	 * @var Name|null
126
	 */
127
	private $shopper_name;
128
129
	/**
130
	 * The shopper's reference to uniquely identify this shopper (e.g. user ID or account ID). This field is
131
	 * required for recurring payments
132
	 *
133
	 * @var string|null
134
	 */
135
	private $shopper_reference;
136
137
	/**
138
	 * The text to appear on the shopper's bank statement.
139
	 *
140
	 * @var string|null
141
	 */
142
	private $shopper_statement;
143
144
	/**
145
	 * The shopper's telephone number
146
	 *
147
	 * @var string|null
148
	 */
149
	private $telephone_number;
150
151
	/**
152
	 * Construct a payment request object.
153
	 *
154
	 * @param Amount $amount           The amount information for the transaction.
155
	 * @param string $merchant_account The merchant account identifier, with which you want to process the transaction.
156
	 * @param string $reference        The reference to uniquely identify a payment.
157
	 * @param string $return_url       The URL to return to.
158
	 */
159 7
	public function __construct( Amount $amount, $merchant_account, $reference, $return_url ) {
160 7
		$this->amount           = $amount;
161 7
		$this->merchant_account = $merchant_account;
162 7
		$this->reference        = $reference;
163 7
		$this->return_url       = $return_url;
164 7
	}
165
166
	/**
167
	 * Get amount.
168
	 *
169
	 * @return Amount
170
	 */
171 6
	public function get_amount() {
172 6
		return $this->amount;
173
	}
174
175
	/**
176
	 * Get billing address.
177
	 *
178
	 * @return Address|null
179
	 */
180 1
	public function get_billing_address() {
181 1
		return $this->billing_address;
182
	}
183
184
	/**
185
	 * Set billing address.
186
	 *
187
	 * @param Address|null $billing_address Billing address.
188
	 * @return void
189
	 */
190 1
	public function set_billing_address( Address $billing_address = null ) {
191 1
		$this->billing_address = $billing_address;
192 1
	}
193
194
	/**
195
	 * Get channel.
196
	 *
197
	 * @return string|null
198
	 */
199 1
	public function get_channel() {
200 1
		return $this->channel;
201
	}
202
203
	/**
204
	 * Set channel.
205
	 *
206
	 * @param string|null $channel Channel.
207
	 * @return void
208
	 */
209 1
	public function set_channel( $channel ) {
210 1
		$this->channel = $channel;
211 1
	}
212
213
	/**
214
	 * Get country code.
215
	 *
216
	 * @return string|null
217
	 */
218 2
	public function get_country_code() {
219 2
		return $this->country_code;
220
	}
221
222
	/**
223
	 * Set country code.
224
	 *
225
	 * @param string|null $country_code Country code.
226
	 * @return void
227
	 * @throws InvalidArgumentException Throws invalid argument exception when country code is not 2 characters.
228
	 */
229 5
	public function set_country_code( $country_code ) {
230 5
		if ( null !== $country_code && 2 !== strlen( $country_code ) ) {
231 1
			throw new InvalidArgumentException(
232 1
				sprintf(
233 1
					'Given country code `%s` not ISO 3166-1 alpha-2 value.',
234 1
					$country_code
235
				)
236
			);
237
		}
238
239 4
		$this->country_code = $country_code;
240 4
	}
241
242
	/**
243
	 * Get date of birth.
244
	 *
245
	 * @return DateTime|null
246
	 */
247 1
	public function get_date_of_birth() {
248 1
		return $this->date_of_birth;
249
	}
250
251
	/**
252
	 * Set date of birth.
253
	 *
254
	 * @param DateTime|null $date_of_birth Date of birth.
255
	 * @return void
256
	 */
257 1
	public function set_date_of_birth( DateTime $date_of_birth = null ) {
258 1
		$this->date_of_birth = $date_of_birth;
259 1
	}
260
261
	/**
262
	 * Get delivery address.
263
	 *
264
	 * @return Address|null
265
	 */
266 1
	public function get_delivery_address() {
267 1
		return $this->delivery_address;
268
	}
269
270
	/**
271
	 * Set delivery address.
272
	 *
273
	 * @param Address|null $delivery_address Delivery address.
274
	 * @return void
275
	 */
276 1
	public function set_delivery_address( Address $delivery_address = null ) {
277 1
		$this->delivery_address = $delivery_address;
278 1
	}
279
280
	/**
281
	 * Get line items.
282
	 *
283
	 * @return LineItems|null
284
	 */
285 1
	public function get_line_items() {
286 1
		return $this->line_items;
287
	}
288
289
	/**
290
	 * Set line items.
291
	 *
292
	 * @param LineItems|null $line_items Line items.
293
	 * @return void
294
	 */
295 1
	public function set_line_items( $line_items ) {
296 1
		$this->line_items = $line_items;
297 1
	}
298
299
	/**
300
	 * Create and set new line items.
301
	 *
302
	 * @return LineItems
303
	 */
304 1
	public function new_line_items() {
305 1
		$this->line_items = new LineItems();
306
307 1
		return $this->line_items;
308
	}
309
310
	/**
311
	 * Get merchant account.
312
	 *
313
	 * @return string
314
	 */
315 6
	public function get_merchant_account() {
316 6
		return $this->merchant_account;
317
	}
318
319
	/**
320
	 * Get reference.
321
	 *
322
	 * @return string
323
	 */
324 6
	public function get_reference() {
325 6
		return $this->reference;
326
	}
327
328
	/**
329
	 * Get return URL.
330
	 *
331
	 * @return string
332
	 */
333 6
	public function get_return_url() {
334 6
		return $this->return_url;
335
	}
336
337
	/**
338
	 * Get shopper IP.
339
	 *
340
	 * @return string|null
341
	 */
342 1
	public function get_shopper_ip() {
343 1
		return $this->shopper_ip;
344
	}
345
346
	/**
347
	 * Set shopper IP.
348
	 *
349
	 * @param string|null $shopper_ip Shopper IP.
350
	 * @return void
351
	 */
352 1
	public function set_shopper_ip( $shopper_ip ) {
353 1
		$this->shopper_ip = $shopper_ip;
354 1
	}
355
356
	/**
357
	 * Get shopper locale.
358
	 *
359
	 * @return string|null
360
	 */
361 1
	public function get_shopper_locale() {
362 1
		return $this->shopper_locale;
363
	}
364
365
	/**
366
	 * Set shopper locale.
367
	 *
368
	 * @param string|null $shopper_locale Shopper locale.
369
	 * @return void
370
	 */
371 1
	public function set_shopper_locale( $shopper_locale ) {
372 1
		$this->shopper_locale = $shopper_locale;
373 1
	}
374
375
	/**
376
	 * Get shopper name.
377
	 *
378
	 * @return Name|null
379
	 */
380 1
	public function get_shopper_name() {
381 1
		return $this->shopper_name;
382
	}
383
384
	/**
385
	 * Set shopper name.
386
	 *
387
	 * @param Name|null $shopper_name Shopper name.
388
	 * @return void
389
	 */
390 1
	public function set_shopper_name( Name $shopper_name = null ) {
391 1
		$this->shopper_name = $shopper_name;
392 1
	}
393
394
	/**
395
	 * Get shopper reference.
396
	 *
397
	 * @return string|null
398
	 */
399 1
	public function get_shopper_reference() {
400 1
		return $this->shopper_reference;
401
	}
402
403
	/**
404
	 * Set shopper reference.
405
	 *
406
	 * @param string|null $shopper_reference Shopper reference.
407
	 * @return void
408
	 */
409 1
	public function set_shopper_reference( $shopper_reference ) {
410 1
		$this->shopper_reference = $shopper_reference;
411 1
	}
412
413
	/**
414
	 * Get shopper statement.
415
	 *
416
	 * @return string|null
417
	 */
418 1
	public function get_shopper_statement() {
419 1
		return $this->shopper_statement;
420
	}
421
422
	/**
423
	 * Set shopper statement.
424
	 *
425
	 * @param string|null $shopper_statement Shopper statement.
426
	 * @return void
427
	 */
428 1
	public function set_shopper_statement( $shopper_statement ) {
429 1
		$this->shopper_statement = $shopper_statement;
430 1
	}
431
432
	/**
433
	 * Get telephone number.
434
	 *
435
	 * @return string|null
436
	 */
437 1
	public function get_telephone_number() {
438 1
		return $this->telephone_number;
439
	}
440
441
	/**
442
	 * Set shopper statement.
443
	 *
444
	 * @param string|null $telephone_number Telephone number.
445
	 * @return void
446
	 */
447 1
	public function set_telephone_number( $telephone_number ) {
448 1
		$this->telephone_number = $telephone_number;
449 1
	}
450
451
	/**
452
	 * Get JSON.
453
	 *
454
	 * @return object
455
	 */
456 5
	public function get_json() {
457 5
		$properties = Util::filter_null(
458
			array(
459 5
				'amount'           => $this->get_amount()->get_json(),
460 5
				'billingAddress'   => is_null( $this->billing_address ) ? null : $this->billing_address->get_json(),
461 5
				'channel'          => $this->channel,
462 5
				'countryCode'      => $this->country_code,
463 5
				'dateOfBirth'      => is_null( $this->date_of_birth ) ? null : $this->date_of_birth->format( 'Y-m-d' ),
464 5
				'deliveryAddress'  => is_null( $this->delivery_address ) ? null : $this->delivery_address->get_json(),
465 5
				'lineItems'        => is_null( $this->line_items ) ? null : $this->line_items->get_json(),
466 5
				'merchantAccount'  => $this->get_merchant_account(),
467 5
				'reference'        => $this->get_reference(),
468 5
				'returnUrl'        => $this->get_return_url(),
469 5
				'shopperIP'        => $this->shopper_ip,
470 5
				'shopperLocale'    => $this->shopper_locale,
471 5
				'shopperName'      => is_null( $this->shopper_name ) ? null : $this->shopper_name->get_json(),
472 5
				'shopperReference' => $this->shopper_reference,
473 5
				'shopperStatement' => $this->shopper_statement,
474 5
				'telephoneNumber'  => $this->telephone_number,
475
			)
476
		);
477
478 5
		$object = (object) $properties;
479
480 5
		return $object;
481
	}
482
}
483