Test Failed
Push — develop ( ee5b51...397f1c )
by Reüel
05:20
created

DropInGateway::start()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 79
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 40
nc 21
nop 1
dl 0
loc 79
rs 8.0355
c 0
b 0
f 0

How to fix   Long Method   

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
 * Drop-in gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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
22
/**
23
 * Drop-in gateway
24
 *
25
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
26
 *
27
 * @author  Remco Tolsma
28
 * @version 1.0.5
29
 * @since   1.0.0
30
 */
31
class DropInGateway extends AbstractGateway {
32
	/**
33
	 * Web SDK version.
34
	 *
35
	 * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk
36
	 *
37
	 * @var string
38
	 */
39
	const SDK_VERSION = '3.4.0';
40
41
	/**
42
	 * Get supported payment methods
43
	 *
44
	 * @return array<string>
45
	 * @see Core_Gateway::get_supported_payment_methods()
46
	 *
47
	 */
48
	public function get_supported_payment_methods() {
49
		return array(
50
			PaymentMethods::ALIPAY,
51
			PaymentMethods::BANCONTACT,
52
			PaymentMethods::CREDIT_CARD,
53
			PaymentMethods::DIRECT_DEBIT,
54
			PaymentMethods::GIROPAY,
55
			PaymentMethods::IDEAL,
56
			PaymentMethods::MAESTRO,
57
			PaymentMethods::SOFORT,
58
		);
59
	}
60
61
	/**
62
	 * Start.
63
	 *
64
	 * @param Payment $payment Payment.
65
	 *
66
	 * @return void
67
	 * @see Plugin::start()
68
	 *
69
	 */
70
	public function start( Payment $payment ) {
71
		$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION );
72
		$payment->set_action_url( $payment->get_pay_redirect_url() );
73
74
		/*
75
		 * API Integration
76
		 *
77
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
78
		 */
79
		$api_integration_payment_method_types = array(
80
			PaymentMethodType::IDEAL,
81
			PaymentMethodType::DIRECT_EBANKING,
82
		);
83
84
		// Return early if API integration is not being used.
85
		$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
86
87
		if ( ! in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) {
88
			return;
89
		}
90
91
		$payment_method = new PaymentMethod( $payment_method_type );
92
93
		if ( PaymentMethodType::IDEAL === $payment_method_type ) {
94
			$payment_method = new PaymentMethodIDeal( $payment_method_type, (string) $payment->get_issuer() );
95
		}
96
97
		// Amount.
98
		try {
99
			$amount = AmountTransformer::transform( $payment->get_total_amount() );
100
		} catch ( InvalidArgumentException $e ) {
101
			$this->error = new \WP_Error( 'adyen_error', $e->getMessage() );
102
103
			return;
104
		}
105
106
		// Country.
107
		$locale = Util::get_payment_locale( $payment );
108
109
		$country_code = Locale::getRegion( $locale );
110
111
		// Set country from billing address.
112
		$billing_address = $payment->get_billing_address();
113
114
		if ( null !== $billing_address ) {
115
			$country = $billing_address->get_country_code();
116
117
			if ( ! empty( $country ) ) {
118
				$country_code = $country;
119
			}
120
		}
121
122
		// API integration.
123
		$payment_request = new PaymentRequest(
124
			$amount,
125
			$this->config->get_merchant_account(),
126
			strval( $payment->get_id() ),
127
			$payment->get_return_url(),
128
			$payment_method
129
		);
130
131
		$payment_request->set_country_code( $country_code );
132
133
		PaymentRequestHelper::complement( $payment, $payment_request );
134
135
		try {
136
			$payment_response = $this->client->create_payment( $payment_request );
137
		} catch ( Exception $e ) {
138
			$this->error = new \WP_Error( 'adyen_error', $e->getMessage() );
139
140
			return;
141
		}
142
143
		$payment->set_transaction_id( $payment_response->get_psp_reference() );
144
145
		$redirect = $payment_response->get_redirect();
146
147
		if ( null !== $redirect ) {
148
			$payment->set_action_url( $redirect->get_url() );
149
		}
150
	}
151
152
	/**
153
	 * Payment redirect.
154
	 *
155
	 * @param Payment $payment Payment.
156
	 *
157
	 * @return void
158
	 */
159
	public function payment_redirect( Payment $payment ) {
160
		$url_script = sprintf(
161
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/sdk/%s/adyen.js',
162
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
163
			self::SDK_VERSION
164
		);
165
166
		// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version is part of URL.
167
		wp_register_script(
168
			'pronamic-pay-adyen-checkout',
169
			$url_script,
170
			array(),
171
			null,
172
			false
173
		);
174
175
		$url_stylesheet = sprintf(
176
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/sdk/%s/adyen.css',
177
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
178
			self::SDK_VERSION
179
		);
180
181
		// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version is part of URL.
182
		wp_register_style(
183
			'pronamic-pay-adyen-checkout',
184
			$url_stylesheet,
185
			array(),
186
			null
187
		);
188
189
		/**
190
		 * Payment methods.
191
		 */
192
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
193
194
		$locale = Util::get_payment_locale( $payment );
195
196
		$country_code = Locale::getRegion( $locale );
197
198
		$request->set_country_code( $country_code );
199
		$request->set_amount( AmountTransformer::transform( $payment->get_total_amount() ) );
200
201
		$payment_methods = $this->client->get_payment_methods( $request );
202
203
		/**
204
		 * Adyen checkout configuration.
205
		 *
206
		 * @link https://docs.adyen.com/checkout/drop-in-web
207
		 * @link https://docs.adyen.com/checkout/components-web
208
		 */
209
		$configuration = (object) array(
210
			'locale'                 => Util::get_payment_locale( $payment ),
211
			'environment'            => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
212
			'originKey'              => $this->config->origin_key,
213
			'paymentMethodsResponse' => $payment_methods->get_original_object(),
214
		);
215
216
		/**
217
		 * Filters the Adyen checkout configuration.
218
		 *
219
		 * @param object $configuration Adyen checkout configuration.
220
		 * @since 1.2.0
221
		 */
222
		$configuration = apply_filters( 'pronamic_pay_adyen_checkout_configuration', $configuration );
223
224
		wp_localize_script(
225
			'pronamic-pay-adyen-checkout',
226
			'pronamicPayAdyenCheckout',
227
			array(
228
				'paymentsUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/' . $payment->get_id() ),
229
				'paymentsDetailsUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/details/' ),
230
				'configuration' => $configuration,
231
			)
232
		);
233
234
		// Add checkout head action.
235
		add_action( 'pronamic_pay_adyen_checkout_head', array( $this, 'checkout_head' ) );
236
237
		// No cache.
238
		Core_Util::no_cache();
239
240
		require __DIR__ . '/../views/checkout-drop-in.php';
241
242
		exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
243
	}
244
245
	/**
246
	 * Checkout head.
247
	 *
248
	 * @return void
249
	 */
250
	public function checkout_head() {
251
		wp_print_styles( 'pronamic-pay-redirect' );
252
253
		wp_print_scripts( 'pronamic-pay-adyen-checkout' );
254
255
		wp_print_styles( 'pronamic-pay-adyen-checkout' );
256
	}
257
258
	/**
259
	 * Create payment.
260
	 *
261
	 * @param Payment       $payment        Payment.
262
	 * @param PaymentMethod $payment_method Payment method.
263
	 *
264
	 * @return \WP_Error|PaymentResponse
265
	 */
266
	public function create_payment( Payment $payment, PaymentMethod $payment_method ) {
267
		// Amount.
268
		try {
269
			$amount = AmountTransformer::transform( $payment->get_total_amount() );
270
		} catch ( \InvalidArgumentException $e ) {
271
			return new \WP_Error( 'adyen_error', $e->getMessage() );
272
		}
273
274
		// Payment request.
275
		$payment_request = new PaymentRequest(
276
			$amount,
277
			$this->config->get_merchant_account(),
278
			strval( $payment->get_id() ),
279
			$payment->get_return_url(),
280
			$payment_method
281
		);
282
283
		// Country.
284
		$locale = Util::get_payment_locale( $payment );
285
286
		$country_code = \Locale::getRegion( $locale );
287
288
		// Set country from billing address.
289
		$billing_address = $payment->get_billing_address();
290
291
		if ( null !== $billing_address ) {
292
			$country = $billing_address->get_country_code();
293
294
			if ( ! empty( $country ) ) {
295
				$country_code = $country;
296
			}
297
		}
298
299
		$payment_request->set_country_code( $country_code );
300
301
		PaymentRequestHelper::complement( $payment, $payment_request );
302
303
		// Create payment.
304
		try {
305
			$payment_response = $this->client->create_payment( $payment_request );
306
		} catch ( \Exception $e ) {
307
			return new \WP_Error( 'adyen_error', $e->getMessage() );
308
		}
309
310
		return $payment_response;
311
	}
312
313
	/**
314
	 * Update status of the specified payment.
315
	 *
316
	 * @param Payment $payment Payment.
317
	 *
318
	 * @return void
319
	 */
320
	public function update_status( Payment $payment ) {
321
		// Process payload on return.
322
		if ( ! filter_has_var( INPUT_GET, 'payload' ) ) {
323
			return;
324
		}
325
326
		$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
327
328
		$payment_result_request = new PaymentResultRequest( $payload );
329
330
		try {
331
			$payment_result_response = $this->client->get_payment_result( $payment_result_request );
332
333
			PaymentResultHelper::update_payment( $payment, $payment_result_response );
334
		} catch ( Exception $e ) {
335
			$note = sprintf(
336
				/* translators: %s: exception message */
337
				__( 'Error getting payment result: %s', 'pronamic_ideal' ),
338
				$e->getMessage()
339
			);
340
341
			$payment->add_note( $note );
342
		}
343
	}
344
}
345