Failed Conditions
Push — develop ( 73c13e...f7ee4b )
by Remco
04:05
created

AbstractPaymentRequest::get_country_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
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 {
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
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 2
	public function __construct( Amount $amount, $merchant_account, $reference, $return_url ) {
160 2
		$this->set_amount( $amount );
161 2
		$this->set_merchant_account( $merchant_account );
162 2
		$this->set_reference( $reference );
163 2
		$this->set_return_url( $return_url );
164 2
	}
165
166
	/**
167
	 * Get amount.
168
	 *
169
	 * @return Amount
170
	 */
171 2
	public function get_amount() {
172 2
		return $this->amount;
173
	}
174
175
	/**
176
	 * Set amount.
177
	 *
178
	 * @param Amount $amount Amount.
179
	 */
180 2
	public function set_amount( Amount $amount ) {
181 2
		$this->amount = $amount;
182 2
	}
183
184
	/**
185
	 * Get billing address.
186
	 *
187
	 * @return Address|null
188
	 */
189
	public function get_billing_address() {
190
		return $this->billing_address;
191
	}
192
193
	/**
194
	 * Set billing address.
195
	 *
196
	 * @param Address|null $billing_address Billing address.
197
	 */
198
	public function set_billing_address( Address $billing_address = null ) {
199
		$this->billing_address = $billing_address;
200
	}
201
202
	/**
203
	 * Get channel.
204
	 *
205
	 * @return string|null
206
	 */
207
	public function get_channel() {
208
		return $this->channel;
209
	}
210
211
	/**
212
	 * Set channel.
213
	 *
214
	 * @param string|null $channel Channel.
215
	 */
216
	public function set_channel( $channel ) {
217
		$this->channel = $channel;
218
	}
219
220
	/**
221
	 * Get country code.
222
	 *
223
	 * @return string|null
224
	 */
225 1
	public function get_country_code() {
226 1
		return $this->country_code;
227
	}
228
229
	/**
230
	 * Set country code.
231
	 *
232
	 * @param string|null $country_code Country code.
233
	 */
234 1
	public function set_country_code( $country_code ) {
235 1
		if ( null !== $country_code && 2 !== strlen( $country_code ) ) {
236
			throw new InvalidArgumentException(
237
				sprintf(
238
					'Given country code `%s` not ISO 3166-1 alpha-2 value.',
239
					$country_code
240
				)
241
			);
242
		}
243
244 1
		$this->country_code = $country_code;
245 1
	}
246
247
	/**
248
	 * Get date of birth.
249
	 *
250
	 * @return DateTime|null
251
	 */
252
	public function get_date_of_birth() {
253
		return $this->date_of_birth;
254
	}
255
256
	/**
257
	 * Set date of birth.
258
	 *
259
	 * @param DateTime|null $date_of_birth Date of birth.
260
	 */
261
	public function set_date_of_birth( DateTime $date_of_birth = null ) {
262
		$this->date_of_birth = $date_of_birth;
263
	}
264
265
	/**
266
	 * Get delivery address.
267
	 *
268
	 * @return Address|null
269
	 */
270
	public function get_delivery_address() {
271
		return $this->delivery_address;
272
	}
273
274
	/**
275
	 * Set delivery address.
276
	 *
277
	 * @param Address|null $delivery_address Delivery address.
278
	 */
279
	public function set_delivery_address( Address $delivery_address = null ) {
280
		$this->delivery_address = $delivery_address;
281
	}
282
283
	/**
284
	 * Get line items.
285
	 *
286
	 * @return LineItems
287
	 */
288
	public function get_line_items() {
289
		return $this->line_items;
290
	}
291
292
	/**
293
	 * Set line items.
294
	 *
295
	 * @param LineItems $line_items Line items.
296
	 */
297
	public function set_line_items( $line_items ) {
298
		$this->line_items = $line_items;
299
	}
300
301
	/**
302
	 * Create and set new line items.
303
	 *
304
	 * @return LineItems
305
	 */
306
	public function new_items() {
307
		$this->line_items = new LineItems();
308
309
		return $this->line_items;
310
	}
311
312
	/**
313
	 * Get merchant account.
314
	 *
315
	 * @return string
316
	 */
317 2
	public function get_merchant_account() {
318 2
		return $this->merchant_account;
319
	}
320
321
	/**
322
	 * Set merchant account.
323
	 *
324
	 * @param string $merchant_account Merchant account.
325
	 */
326 2
	public function set_merchant_account( $merchant_account ) {
327 2
		$this->merchant_account = $merchant_account;
328 2
	}
329
330
	/**
331
	 * Get reference.
332
	 *
333
	 * @return string
334
	 */
335 2
	public function get_reference() {
336 2
		return $this->reference;
337
	}
338
339
	/**
340
	 * Set reference.
341
	 *
342
	 * @param string $reference Reference.
343
	 */
344 2
	public function set_reference( $reference ) {
345 2
		$this->reference = $reference;
346 2
	}
347
348
	/**
349
	 * Get return URL.
350
	 *
351
	 * @return string
352
	 */
353 2
	public function get_return_url() {
354 2
		return $this->return_url;
355
	}
356
357
	/**
358
	 * Set return URL.
359
	 *
360
	 * @param string $return_url Return URL.
361
	 */
362 2
	public function set_return_url( $return_url ) {
363 2
		$this->return_url = $return_url;
364 2
	}
365
366
	/**
367
	 * Get shopper IP.
368
	 *
369
	 * @return string|null
370
	 */
371
	public function get_shopper_ip() {
372
		return $this->shopper_ip;
373
	}
374
375
	/**
376
	 * Set shopper IP.
377
	 *
378
	 * @param string|null $shopper_ip Shopper IP.
379
	 */
380
	public function set_shopper_ip( $shopper_ip ) {
381
		$this->shopper_ip = $shopper_ip;
382
	}
383
384
	/**
385
	 * Get shopper locale.
386
	 *
387
	 * @return string|null
388
	 */
389
	public function get_shopper_locale() {
390
		return $this->shopper_locale;
391
	}
392
393
	/**
394
	 * Set shopper locale.
395
	 *
396
	 * @param string|null $shopper_locale Shopper locale.
397
	 */
398
	public function set_shopper_locale( $shopper_locale ) {
399
		$this->shopper_locale = $shopper_locale;
400
	}
401
402
	/**
403
	 * Get shopper name.
404
	 *
405
	 * @return Name|null
406
	 */
407
	public function get_shopper_name() {
408
		return $this->shopper_name;
409
	}
410
411
	/**
412
	 * Set shopper name.
413
	 *
414
	 * @param Name|null $shopper_name Shopper name.
415
	 */
416
	public function set_shopper_name( Name $shopper_name = null ) {
417
		$this->shopper_name = $shopper_name;
418
	}
419
420
	/**
421
	 * Get shopper reference.
422
	 *
423
	 * @return string|null
424
	 */
425
	public function get_shopper_reference() {
426
		return $this->shopper_reference;
427
	}
428
429
	/**
430
	 * Set shopper reference.
431
	 *
432
	 * @param string|null $shopper_reference Shopper reference.
433
	 */
434
	public function set_shopper_reference( $shopper_reference ) {
435
		$this->shopper_reference = $shopper_reference;
436
	}
437
438
	/**
439
	 * Get shopper statement.
440
	 *
441
	 * @return string|null
442
	 */
443
	public function get_shopper_statement() {
444
		return $this->shopper_statement;
445
	}
446
447
	/**
448
	 * Set shopper statement.
449
	 *
450
	 * @param string|null $shopper_statement Shopper statement.
451
	 */
452
	public function set_shopper_statement( $shopper_statement ) {
453
		$this->shopper_statement = $shopper_statement;
454
	}
455
456
	/**
457
	 * Get telephone number.
458
	 *
459
	 * @return string|null
460
	 */
461
	public function get_telephone_number() {
462
		return $this->telephone_number;
463
	}
464
465
	/**
466
	 * Set shopper statement.
467
	 *
468
	 * @param string|null $telephone_number Telephone number.
469
	 */
470
	public function set_telephone_number( $telephone_number ) {
471
		$this->telephone_number = $telephone_number;
472
	}
473
474
	/**
475
	 * Get JSON.
476
	 *
477
	 * @return object
478
	 */
479 2
	public function get_json() {
480 2
		$object = (object) array();
481
482
		// Amount.
483 2
		$object->amount = $this->get_amount()->get_json();
484
485
		// Billing address.
486 2
		if ( null !== $this->billing_address ) {
487
			$object->billingAddress = $this->billing_address->get_json();
488
		}
489
490
		// Channel.
491 2
		if ( null !== $this->channel ) {
492
			$object->channel = $this->channel;
493
		}
494
495
		// Country code.
496 2
		if ( null !== $this->country_code ) {
497 1
			$object->countryCode = $this->country_code;
498
		}
499
500
		// Date of birth.
501 2
		if ( null !== $this->date_of_birth ) {
502
			$object->dateOfBirth = $this->date_of_birth->format( 'Y-m-d' );
503
		}
504
505
		// Delivery address.
506 2
		if ( null !== $this->delivery_address ) {
507
			$object->deliveryAddress = $this->delivery_address->get_json();
508
		}
509
510
		// Line items.
511 2
		if ( null !== $this->line_items ) {
512
			$object->lineItems = $this->line_items->get_json();
513
		}
514
515
		// Merchant account.
516 2
		$object->merchantAccount = $this->get_merchant_account();
517
518
		// Reference.
519 2
		$object->reference = $this->get_reference();
520
521
		// Return URL.
522 2
		$object->returnUrl = $this->get_return_url();
523
524
		// Shopper IP.
525 2
		if ( null !== $this->shopper_ip ) {
526
			$object->shopperIP = $this->shopper_ip;
527
		}
528
529
		// Shopper locale.
530 2
		if ( null !== $this->shopper_locale ) {
531
			$object->shopperLocale = $this->shopper_locale;
532
		}
533
534
		// Shopper name.
535 2
		if ( null !== $this->shopper_name ) {
536
			$object->shopperName = $this->shopper_name->get_json();
537
		}
538
539
		// Shopper reference.
540 2
		if ( null !== $this->shopper_reference ) {
541
			$object->shopperReference = $this->shopper_reference;
542
		}
543
544
		// Shopper statement.
545 2
		if ( null !== $this->shopper_statement ) {
546
			$object->shopperStatement = $this->shopper_statement;
547
		}
548
549
		// Telephone number.
550 2
		if ( null !== $this->telephone_number ) {
551
			$object->telephoneNumber = $this->telephone_number;
552
		}
553
554
		// Return object.
555 2
		return $object;
556
	}
557
}
558