Failed Conditions
Push — develop ( 664d7d...d8db7e )
by Remco
03:03
created

PaymentRequest::set_reference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * 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
/**
14
 * Title: Adyen payment request
15
 * Description:
16
 * Copyright: 2005-2019 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v40/payments
20
 *
21
 * @author  Reüel van der Steege
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class PaymentRequest {
26
	/**
27
	 * Allowed payment methods.
28
	 *
29
	 * List of payments methods to be presented to the shopper. To refer to payment methods,
30
	 * use their brandCode from https://docs.adyen.com/developers/payment-methods/payment-methods-overview
31
	 *
32
	 * @var array
33
	 */
34
	public $allowed_payment_methods;
35
36
	/**
37
	 * The transaction amount needs to be represented in minor units according to the table below.
38
	 *
39
	 * Some currencies do not have decimal points, such as JPY, and some have 3 decimal points, such as BHD.
40
	 * For example, 10 GBP is submitted as 1000, whereas 10 JPY is submitted as 10.
41
	 *
42
	 * @link https://docs.adyen.com/developers/development-resources/currency-codes
43
	 *
44
	 * @var int
45
	 */
46
	public $amount_value;
47
48
	/**
49
	 * Channel.
50
	 *
51
	 * The platform where a payment transaction takes place. This field is optional for filtering out
52
	 * payment methods that are only available on specific platforms. If this value is not set,
53
	 * then we will try to infer it from the sdkVersion or token.
54
	 *
55
	 * Possible values: Android, iOS, Web.
56
	 *
57
	 * @var string
58
	 */
59
	public $channel;
60
61
	/**
62
	 * Country code (ISO 3166-1 alpha-2).
63
	 *
64
	 * @var string
65
	 */
66
	public $country_code;
67
68
	/**
69
	 * Currency code.
70
	 *
71
	 * @link https://docs.adyen.com/developers/development-resources/currency-codes
72
	 *
73
	 * @var string
74
	 */
75
	public $currency;
76
77
	/**
78
	 * The merchant account identifier, with which you want to process the transaction.
79
	 *
80
	 * @var string
81
	 */
82
	public $merchant_account;
83
84
	/**
85
	 * Origin URL.
86
	 *
87
	 * Required for the Web integration. Set this parameter to the
88
	 * origin URL of the page that you are loading the SDK from.
89
	 *
90
	 * @var string
91
	 */
92
	public $origin_url;
93
94
	/**
95
	 * The collection that contains the type of the payment method and its
96
	 * specific information (e.g. idealIssuer).
97
	 *
98
	 * @var array
99
	 */
100
	public $payment_method;
101
102
	/**
103
	 * The reference to uniquely identify a payment. This reference is used in all communication
104
	 * with you about the payment status. We recommend using a unique value per payment;
105
	 * however, it is not a requirement. If you need to provide multiple references for
106
	 * a transaction, separate them with hyphens ("-"). Maximum length: 80 characters.
107
	 *
108
	 * @var string
109
	 */
110
	public $reference;
111
112
	/**
113
	 * The URL to return to.
114
	 *
115
	 * @var string
116
	 */
117
	public $return_url;
118
119
	/**
120
	 * SDK version.
121
	 *
122
	 * @var string
123
	 */
124
	public $sdk_version;
125
126
	/**
127
	 * The shopper IP.
128
	 *
129
	 * @var string
130
	 */
131
	public $shopper_ip;
132
133
	/**
134
	 * The shopper gender.
135
	 *
136
	 * @var string
137
	 */
138
	public $shopper_gender;
139
140
	/**
141
	 * The shopper first name.
142
	 *
143
	 * @var string
144
	 */
145
	public $shopper_first_name;
146
147
	/**
148
	 * The name's infix, if applicable. A maximum length of twenty (20) characters is imposed.
149
	 *
150
	 * @var string
151
	 */
152
	public $shopper_name_infix;
153
154
	/**
155
	 * The shopper last name.
156
	 *
157
	 * @var string
158
	 */
159
	public $shopper_last_name;
160
161
	/**
162
	 * The combination of a language code and a country code to specify the language to be used in the payment.
163
	 *
164
	 * @var string
165
	 */
166
	public $shopper_locale;
167
168
	/**
169
	 * The shopper's reference to uniquely identify this shopper (e.g. user ID or account ID). This field is
170
	 * required for recurring payments
171
	 *
172
	 * @var string
173
	 */
174
	public $shopper_reference;
175
176
	/**
177
	 * The text to appear on the shopper's bank statement.
178
	 *
179
	 * @var string
180
	 */
181
	public $shopper_statement;
182
183
	/**
184
	 * The shopper's telephone number.
185
	 *
186
	 * @var string
187
	 */
188
	public $shopper_telephone_number;
189
190
	/**
191
	 * Construct a payment request object.
192
	 *
193
	 * @param Amount        $amount           The amount information for the transaction.
194
	 * @param string        $merchant_account The merchant account identifier, with which you want to process the transaction
195
	 * @param PaymentMethod $payment_method   The collection that contains the type of the payment method and its specific information (e.g. idealIssuer).
196
	 * @param string        $reference        The reference to uniquely identify a payment.
197
	 * @param string        $return_url       The URL to return to.
198
	 */
199
	public function __construct( Amount $amount, $merchant_account, PaymentMethod $payment_method, $reference, $return_url ) {
200
		$this->set_amount( $amount );
201
		$this->set_merchant_account( $merchant_account );
202
		$this->set_payment_method( $payment_method );
203
		$this->set_reference( $reference );
204
		$this->set_return_url( $return_url );
205
	}
206
207
	/**
208
	 * Get amount.
209
	 *
210
	 * @return Amount
211
	 */
212
	public function get_amount() {
213
		return $this->amount;
214
	}
215
216
	/**
217
	 * Set amount.
218
	 *
219
	 * @param Amount $amount Amount.
220
	 */
221
	public function set_amount( Amount $amount ) {
222
		$this->amount = $amount;
0 ignored issues
show
Bug Best Practice introduced by
The property amount does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
223
	}
224
225
	/**
226
	 * Get merchant account.
227
	 *
228
	 * @return string
229
	 */
230
	public function get_merchant_account() {
231
		return $this->merchant_account;
232
	}
233
234
	/**
235
	 * Set merchant account.
236
	 *
237
	 * @param string $merchant_account Merchant account.
238
	 */
239
	public function set_merchant_account( $merchant_account ) {
240
		$this->merchant_account = $merchant_account;
241
	}
242
243
	/**
244
	 * Get payment method.
245
	 *
246
	 * @return PaymentMethod
247
	 */
248
	public function get_payment_method() {
249
		return $this->payment_method;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->payment_method returns the type array which is incompatible with the documented return type Pronamic\WordPress\Pay\G...ays\Adyen\PaymentMethod.
Loading history...
250
	}
251
252
	/**
253
	 * Set payment method.
254
	 *
255
	 * @param PaymentMethod $payment_method Payment method.
256
	 */
257
	public function set_payment_method( PaymentMethod $payment_method ) {
258
		$this->payment_method = $payment_method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $payment_method of type Pronamic\WordPress\Pay\G...ays\Adyen\PaymentMethod is incompatible with the declared type array of property $payment_method.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
259
	}
260
261
	/**
262
	 * Get reference.
263
	 *
264
	 * @return string
265
	 */
266
	public function get_reference() {
267
		return $this->reference;
268
	}
269
270
	/**
271
	 * Set reference.
272
	 *
273
	 * @param string $reference Reference.
274
	 */
275
	public function set_reference( $reference ) {
276
		$this->reference = $reference;
277
	}
278
279
	/**
280
	 * Get return URL.
281
	 *
282
	 * @return string
283
	 */
284
	public function get_return_url() {
285
		return $this->return_url;
286
	}
287
288
	/**
289
	 * Set return URL.
290
	 *
291
	 * @param string $return_url Return URL.
292
	 */
293
	public function set_return_url( $return_url ) {
294
		$this->return_url = $return_url;
295
	}
296
297
	/**
298
	 * Get shopper name.
299
	 *
300
	 * @return ShopperName|null
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Gateways\Adyen\ShopperName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
301
	 */
302
	public function get_shopper_name() {
303
		return $this->shopper_name;
304
	}
305
306
	/**
307
	 * Set shopper name.
308
	 *
309
	 * @param ShopperName|null $shopper_name Shopper name.
310
	 */
311
	public function set_shopper_name( ShopperName $shopper_name = null ) {
312
		$this->shopper_name = $shopper_name;
0 ignored issues
show
Bug Best Practice introduced by
The property shopper_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
313
	}
314
315
	/**
316
	 * Get JSON.
317
	 *
318
	 * @return object
319
	 */
320
	public function get_json() {
321
		$object = (object) array();
322
323
		// Amount.
324
		$object->amount = $this->get_amount()->get_json();
325
326
		// Merchant account.
327
		$object->merchantAccount = $this->get_merchant_account();
328
329
		// Payment method.
330
		$object->paymentMethod = $this->get_payment_method()->get_json();
331
332
		// Reference.
333
		$object->reference = $this->get_reference();
334
335
		// Return URL.
336
		$object->returnUrl = $this->get_return_url();
337
338
		// Shopper name.
339
		$shopper_name = $this->get_shopper_name();
340
341
		if ( null !== $shopper_name ) {
342
			$object->shopperName = $shopper_name->get_json();
343
		}
344
345
		// Return object.
346
		return $object;
347
348
		$array = array(
0 ignored issues
show
Unused Code introduced by
$array = array('amount' ...opper_telephone_number) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
349
			'amount'                => array(
350
				'currency' => $this->currency,
351
				'value'    => $this->amount_value,
352
			),
353
			'allowedPaymentMethods' => $this->allowed_payment_methods,
354
			'channel'               => $this->channel,
355
			'countryCode'           => $this->country_code,
356
			'merchantAccount'       => $this->merchant_account,
357
			'origin'                => $this->origin_url,
358
			'paymentMethod'         => $this->payment_method,
359
			'reference'             => $this->reference,
360
			'returnUrl'             => $this->return_url,
361
			'sdkVersion'            => $this->sdk_version,
362
			'shopperIp'             => $this->shopper_ip,
363
			'shopperName'           => array(
364
				'firstName' => $this->shopper_first_name,
365
				'gender'    => $this->shopper_gender,
366
				'infix'     => $this->shopper_name_infix,
367
				'lastName'  => $this->shopper_last_name,
368
			),
369
			'shopperLocale'         => $this->shopper_locale,
370
			'shopperReference'      => $this->shopper_reference,
371
			'shopperStatement'      => $this->shopper_statement,
372
			'telephoneNumber'       => $this->shopper_telephone_number,
373
		);
374
375
		/*
376
		 * Array filter will remove values NULL, FALSE and empty strings ('')
377
		 */
378
		$array['paymentMethod'] = (object) array_filter( $array['paymentMethod'] );
379
		$array['shopperName']   = (object) array_filter( $array['shopperName'] );
380
		$array                  = array_filter( $array );
381
382
		return $array;
383
	}
384
}
385