Test Failed
Push — develop ( 27299e...c51b01 )
by Reüel
05:16
created

Gateway::start()   C

Complexity

Conditions 13
Paths 85

Size

Total Lines 128
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 18
Bugs 1 Features 0
Metric Value
cc 13
eloc 65
c 18
b 1
f 0
nc 85
nop 1
dl 0
loc 128
ccs 0
cts 59
cp 0
crap 182
rs 6.0569

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Gateway
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 Exception;
14
use InvalidArgumentException;
15
use Locale;
16
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
17
use Pronamic\WordPress\Pay\Core\PaymentMethods;
18
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
use Pronamic\WordPress\Pay\Plugin;
21
use WP_Error;
22
23
/**
24
 * Gateway
25
 *
26
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
27
 *
28
 * @author  Remco Tolsma
29
 * @version 1.0.0
30
 * @since   1.0.0
31
 */
32
class Gateway extends Core_Gateway {
33
	/**
34
	 * Web SDK version.
35
	 *
36
	 * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk
37
	 *
38
	 * @var string
39
	 */
40
	const SDK_VERSION = '1.9.2';
41
42
	/**
43
	 * Client.
44
	 *
45
	 * @var Client
46
	 */
47
	public $client;
48
49
	/**
50
	 * Constructs and initializes an Adyen gateway.
51
	 *
52
	 * @param Config $config Config.
53
	 */
54 2
	public function __construct( Config $config ) {
55 2
		parent::__construct( $config );
56
57 2
		$this->set_method( self::METHOD_HTTP_REDIRECT );
58
59
		// Supported features.
60 2
		$this->supports = array();
61
62
		// Client.
63 2
		$this->client = new Client( $config );
64 2
	}
65
66
	/**
67
	 * Get supported payment methods
68
	 *
69
	 * @see Core_Gateway::get_supported_payment_methods()
70
	 */
71 1
	public function get_supported_payment_methods() {
72
		return array(
73 1
			PaymentMethods::BANCONTACT,
74
			PaymentMethods::CREDIT_CARD,
75
			PaymentMethods::DIRECT_DEBIT,
76
			PaymentMethods::GIROPAY,
77
			PaymentMethods::IDEAL,
78
			PaymentMethods::MAESTRO,
79
			PaymentMethods::SOFORT,
80
		);
81
	}
82
83
	/**
84
	 * Start.
85
	 *
86
	 * @see Plugin::start()
87
	 *
88
	 * @param Payment $payment Payment.
89
	 * @return void
90
	 */
91
	public function start( Payment $payment ) {
92
		// Amount.
93
		try {
94
			$amount = AmountTransformer::transform( $payment->get_total_amount() );
95
		} catch ( InvalidArgumentException $e ) {
96
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $e->getMessage() );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\GatewayException 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...
97
		}
98
99
		// Payment method type.
100
		$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
101
102
		// Country.
103
		$locale = get_locale();
104
105
		$customer = $payment->get_customer();
106
107
		if ( null !== $customer ) {
108
			$locale = $customer->get_locale();
109
		}
110
111
		$locale = strval( $locale );
112
113
		$country_code = Locale::getRegion( $locale );
114
115
		// Set country from billing address.
116
		$billing_address = $payment->get_billing_address();
117
118
		if ( null !== $billing_address ) {
119
			$country = $billing_address->get_country_code();
120
121
			if ( ! empty( $country ) ) {
122
				$country_code = $country;
123
			}
124
		}
125
126
		/*
127
		 * API Integration
128
		 *
129
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
130
		 */
131
		$api_integration_payment_method_types = array(
132
			PaymentMethodType::IDEAL,
133
			PaymentMethodType::DIRECT_EBANKING,
134
		);
135
136
		if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) {
137
			$payment_method = new PaymentMethod( $payment_method_type );
138
139
			if ( PaymentMethodType::IDEAL === $payment_method_type ) {
140
				$payment_method = new PaymentMethodIDeal( $payment_method_type, (string) $payment->get_issuer() );
141
			}
142
143
			// API integration.
144
			$payment_request = new PaymentRequest(
145
				$amount,
146
				$this->config->get_merchant_account(),
147
				strval( $payment->get_id() ),
148
				$payment->get_return_url(),
149
				$payment_method
150
			);
151
152
			$payment_request->set_country_code( $country_code );
153
154
			PaymentRequestHelper::complement( $payment, $payment_request );
155
156
			try {
157
				$payment_response = $this->client->create_payment( $payment_request );
158
			} catch ( Exception $e ) {
159
				throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $e->getMessage() );
160
			}
161
162
			$payment->set_transaction_id( $payment_response->get_psp_reference() );
163
164
			$redirect = $payment_response->get_redirect();
165
166
			if ( null !== $redirect ) {
167
				$payment->set_action_url( $redirect->get_url() );
168
			}
169
170
			// Return early so SDK integration code will not be executed for API integration.
171
			return;
172
		}
173
174
		/*
175
		 * SDK Integration
176
		 *
177
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession
178
		 */
179
		$payment_session_request = new PaymentSessionRequest(
180
			$amount,
181
			$this->config->get_merchant_account(),
182
			strval( $payment->get_id() ),
183
			$payment->get_return_url(),
184
			$country_code
185
		);
186
187
		PaymentRequestHelper::complement( $payment, $payment_session_request );
188
189
		// Origin.
190
		$origin = home_url();
191
192
		$origin_url = wp_parse_url( home_url() );
193
194
		if ( is_array( $origin_url ) && isset( $origin_url['scheme'], $origin_url['host'] ) ) {
195
			$origin = sprintf(
196
				'%s://%s',
197
				$origin_url['scheme'],
198
				$origin_url['host']
199
			);
200
		}
201
202
		$payment_session_request->set_origin( $origin );
203
		$payment_session_request->set_sdk_version( self::SDK_VERSION );
204
205
		if ( null !== $payment_method_type ) {
206
			$payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) );
207
		}
208
209
		try {
210
			$payment_session_response = $this->client->create_payment_session( $payment_session_request );
211
		} catch ( Exception $e ) {
212
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $e->getMessage() );
213
		}
214
215
		$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION );
216
		$payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() );
217
218
		$payment->set_action_url( $payment->get_pay_redirect_url() );
219
	}
220
221
	/**
222
	 * Payment redirect.
223
	 *
224
	 * @param Payment $payment Payment.
225
	 *
226
	 * @return void
227
	 */
228
	public function payment_redirect( Payment $payment ) {
229
		$sdk_version     = $payment->get_meta( 'adyen_sdk_version' );
230
		$payment_session = $payment->get_meta( 'adyen_payment_session' );
231
232
		if ( empty( $sdk_version ) || empty( $payment_session ) ) {
233
			return;
234
		}
235
236
		if ( empty( $payment->config_id ) ) {
237
			return;
238
		}
239
240
		$url = sprintf(
241
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js',
242
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
243
			$sdk_version
244
		);
245
246
		wp_register_script(
247
			'pronamic-pay-adyen-checkout',
248
			$url,
249
			array(
250
				'jquery',
251
			),
252
			$sdk_version,
253
			false
254
		);
255
256
		/**
257
		 * Config object.
258
		 *
259
		 * @link https://docs.adyen.com/checkout/web-sdk/
260
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/settings/
261
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields
262
		 */
263
		$config_object = (object) array(
264
			'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
265
		);
266
267
		/**
268
		 * Filters the Adyen config object.
269
		 *
270
		 * @link https://github.com/wp-pay-gateways/adyen#pronamic_pay_adyen_config_object
271
		 * @link https://docs.adyen.com/checkout/web-sdk/
272
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/settings/
273
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields
274
		 *
275
		 * @param object $config_object Adyen config object.
276
		 *
277
		 * @since 1.1
278
		 */
279
		$config_object = apply_filters( 'pronamic_pay_adyen_config_object', $config_object );
280
281
		wp_localize_script(
282
			'pronamic-pay-adyen-checkout',
283
			'pronamicPayAdyenCheckout',
284
			array(
285
				'paymentsResultUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/result/' . $payment->config_id ),
286
				'paymentReturnUrl'  => $payment->get_return_url(),
287
				'paymentSession'    => $payment_session,
288
				'configObject'      => $config_object,
289
			)
290
		);
291
292
		// Add checkout head action.
293
		add_action( 'pronamic_pay_adyen_checkout_head', array( $this, 'checkout_head' ) );
294
295
		// No cache.
296
		Core_Util::no_cache();
297
298
		require __DIR__ . '/../views/checkout.php';
299
300
		exit;
301
	}
302
303
	/**
304
	 * Checkout head.
305
	 *
306
	 * @return void
307
	 */
308
	public function checkout_head() {
309
		wp_print_styles( 'pronamic-pay-redirect' );
310
311
		wp_print_scripts( 'pronamic-pay-adyen-checkout' );
312
	}
313
314
	/**
315
	 * Update status of the specified payment.
316
	 *
317
	 * @param Payment $payment Payment.
318
	 *
319
	 * @return void
320
	 */
321
	public function update_status( Payment $payment ) {
322
		// Process payload on return.
323
		if ( ! filter_has_var( INPUT_GET, 'payload' ) ) {
324
			return;
325
		}
326
327
		$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
328
329
		$payment_result_request = new PaymentResultRequest( $payload );
330
331
		try {
332
			$payment_result_response = $this->client->get_payment_result( $payment_result_request );
333
334
			PaymentResultHelper::update_payment( $payment, $payment_result_response );
335
		} catch ( Exception $e ) {
336
			$note = sprintf(
337
				/* translators: %s: exception message */
338
				__( 'Error getting payment result: %s', 'pronamic_ideal' ),
339
				$e->getMessage()
340
			);
341
342
			$payment->add_note( $note );
343
		}
344
	}
345
346
	/**
347
	 * Get available payment methods.
348
	 *
349
	 * @see Core_Gateway::get_available_payment_methods()
350
	 */
351
	public function get_available_payment_methods() {
352
		$core_payment_methods = array();
353
354
		try {
355
			$payment_methods_response = $this->client->get_payment_methods();
356
		} catch ( Exception $e ) {
357
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $e->getMessage() );
358
		}
359
360
		foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) {
361
			$core_payment_method = PaymentMethodType::to_wp( $payment_method->get_type() );
362
363
			$core_payment_methods[] = $core_payment_method;
364
		}
365
366
		$core_payment_methods = array_filter( $core_payment_methods );
367
		$core_payment_methods = array_unique( $core_payment_methods );
368
369
		return $core_payment_methods;
370
	}
371
372
	/**
373
	 * Get issuers.
374
	 *
375
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
376
	 * @return array
377
	 */
378
	public function get_issuers() {
379
		$issuers = array();
380
381
		try {
382
			$payment_methods_response = $this->client->get_payment_methods();
383
		} catch ( Exception $e ) {
384
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $e->getMessage() );
385
		}
386
387
		$payment_methods = $payment_methods_response->get_payment_methods();
388
389
		// Limit to iDEAL payment methods.
390
		$payment_methods = array_filter(
391
			$payment_methods,
392
			/**
393
			 * Check if payment method is iDEAL.
394
			 *
395
			 * @param PaymentMethod $payment_method Payment method.
396
			 * @return boolean True if payment method is iDEAL, false otherwise.
397
			 */
398
			function( $payment_method ) {
399
				return ( PaymentMethodType::IDEAL === $payment_method->get_type() );
400
			}
401
		);
402
403
		foreach ( $payment_methods as $payment_method ) {
404
			$details = $payment_method->get_details();
405
406
			if ( is_array( $details ) ) {
407
				foreach ( $details as $detail ) {
408
					if ( 'issuer' === $detail->key && 'select' === $detail->type ) {
409
						foreach ( $detail->items as $item ) {
410
							$issuers[ $item->id ] = $item->name;
411
						}
412
					}
413
				}
414
			}
415
		}
416
417
		if ( empty( $issuers ) ) {
418
			return $issuers;
419
		}
420
421
		return array(
422
			array(
423
				'options' => $issuers,
424
			),
425
		);
426
	}
427
}
428