Test Failed
Push — develop ( 9d68c8...7a4ce3 )
by Reüel
06:00
created

DropInGateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
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 Locale;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Core\Server;
17
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
18
use Pronamic\WordPress\Pay\Payments\Payment;
19
use Pronamic\WordPress\Pay\Plugin;
20
21
/**
22
 * Drop-in gateway
23
 *
24
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
25
 *
26
 * @author  Remco Tolsma
27
 * @version 1.0.5
28
 * @since   1.0.0
29
 */
30
class DropInGateway extends AbstractGateway {
31
	/**
32
	 * Web SDK version.
33
	 *
34
	 * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk
35
	 *
36
	 * @var string
37
	 */
38
	const SDK_VERSION = '3.4.0';
39
40
	/**
41
	 * Constructs and initializes an Adyen gateway.
42
	 *
43
	 * @param Config $config Config.
44
	 */
45 1
	public function __construct( Config $config ) {
46 1
		parent::__construct( $config );
47
48
		// Supported features.
49 1
		$this->supports = array(
50
			'payment_status_request',
51
			'webhook_log',
52
			'webhook',
53
		);
54 1
	}
55
56
	/**
57
	 * Get supported payment methods
58
	 *
59
	 * @return array<string>
60
	 * @see Core_Gateway::get_supported_payment_methods()
61
	 */
62 1
	public function get_supported_payment_methods() {
63
		return array(
64 1
			PaymentMethods::ALIPAY,
65
			PaymentMethods::BANCONTACT,
66
			PaymentMethods::CREDIT_CARD,
67
			PaymentMethods::DIRECT_DEBIT,
68
			PaymentMethods::EPS,
69
			PaymentMethods::GIROPAY,
70
			PaymentMethods::GOOGLE_PAY,
71
			PaymentMethods::IDEAL,
72
			PaymentMethods::SOFORT,
73
		);
74
	}
75
76
	/**
77
	 * Start.
78
	 *
79
	 * @param Payment $payment Payment.
80
	 *
81
	 * @return void
82
	 * @see Plugin::start()
83
	 */
84
	public function start( Payment $payment ) {
85
		$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION );
86
		$payment->set_action_url( $payment->get_pay_redirect_url() );
87
88
		/*
89
		 * API Integration
90
		 *
91
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
92
		 */
93
		$api_integration_payment_method_types = array(
94
			PaymentMethodType::ALIPAY,
95
			PaymentMethodType::IDEAL,
96
			PaymentMethodType::DIRECT_EBANKING,
97
		);
98
99
		// Return early if API integration is not being used.
100
		$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
101
102
		if ( ! in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) {
103
			return;
104
		}
105
106
		// Payment method.
107
		$payment_method = array(
108
			'type' => $payment_method_type,
109
		);
110
111
		if ( PaymentMethodType::IDEAL === $payment_method_type ) {
112
			$payment_method['issuer'] = (string) $payment->get_issuer();
113
		}
114
115
		$payment_method = new PaymentMethod( (object) $payment_method );
116
117
		// Create payment.
118
		$payment_response = $this->create_payment( $payment, $payment_method );
119
120
		// Set payment action URL.
121
		$redirect = $payment_response->get_redirect();
122
123
		if ( null !== $redirect ) {
124
			$payment->set_action_url( $redirect->get_url() );
125
		}
126
	}
127
128
	/**
129
	 * Payment redirect.
130
	 *
131
	 * @param Payment $payment Payment.
132
	 *
133
	 * @return void
134
	 */
135
	public function payment_redirect( Payment $payment ) {
136
		// Check payment ID.
137
		$payment_id = $payment->get_id();
138
139
		if ( null === $payment_id ) {
140
			return;
141
		}
142
143
		$payment_response = $payment->get_meta( 'adyen_payment_response' );
144
145
		// Only show drop-in checkout page if payment method does not redirect.
146
		if ( is_string( $payment_response ) && '' !== $payment_response ) {
147
			$payment_response = \json_decode( $payment_response );
148
149
			$payment_response = PaymentResponse::from_object( $payment_response );
150
151
			$redirect = $payment_response->get_redirect();
152
153
			if ( null !== $redirect ) {
154
				\wp_redirect( $redirect->get_url() );
155
			}
156
		}
157
158
		/**
159
		 * Payment methods.
160
		 */
161
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
162
163
		if ( null !== $payment->get_method() ) {
164
			// Payment method type.
165
			$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
166
167
			if ( null !== $payment_method_type ) {
168
				$request->set_allowed_payment_methods( array( $payment_method_type ) );
169
			}
170
		}
171
172
		$locale = Util::get_payment_locale( $payment );
173
174
		$country_code = Locale::getRegion( $locale );
175
176
		$request->set_country_code( $country_code );
177
		$request->set_amount( AmountTransformer::transform( $payment->get_total_amount() ) );
178
179
		try {
180
			$payment_methods = $this->client->get_payment_methods( $request );
181
		} catch ( \Exception $e ) {
182
			Plugin::render_exception( $e );
183
184
			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...
185
		}
186
187
		$payment_method_types = $payment_methods->get_payment_method_types();
188
189
		// Register scripts.
190
		$url_script = sprintf(
191
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/sdk/%s/adyen.js',
192
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
193
			self::SDK_VERSION
194
		);
195
196
		// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version is part of URL.
197
		wp_register_script(
198
			'pronamic-pay-adyen-checkout',
199
			$url_script,
200
			array(),
201
			self::SDK_VERSION,
202
			false
203
		);
204
205
		wp_register_script(
206
			'pronamic-pay-adyen-google-pay',
207
			'https://pay.google.com/gp/p/js/pay.js',
208
			array(),
209
			\pronamic_pay_plugin()->get_version(),
210
			false
211
		);
212
213
		$dependencies = array( 'pronamic-pay-adyen-checkout' );
214
215
		if ( \in_array( PaymentMethodType::GOOGLE_PAY, $payment_method_types, true ) ) {
216
			$dependencies[] = 'pronamic-pay-adyen-google-pay';
217
		}
218
219
		wp_register_script(
220
			'pronamic-pay-adyen-checkout-drop-in',
221
			plugins_url( '../js/dist/checkout-drop-in.js', __FILE__ ),
222
			$dependencies,
223
			\pronamic_pay_plugin()->get_version(),
224
			true
225
		);
226
227
		// Register styles.
228
		$url_stylesheet = sprintf(
229
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/sdk/%s/adyen.css',
230
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
231
			self::SDK_VERSION
232
		);
233
234
		// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version is part of URL.
235
		wp_register_style(
236
			'pronamic-pay-adyen-checkout',
237
			$url_stylesheet,
238
			array(),
239
			null
240
		);
241
242
		/**
243
		 * Adyen checkout configuration.
244
		 *
245
		 * @link https://docs.adyen.com/checkout/drop-in-web
246
		 * @link https://docs.adyen.com/checkout/components-web
247
		 */
248
		$configuration = (object) array(
249
			'locale'                 => Util::get_payment_locale( $payment ),
250
			'environment'            => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
251
			'originKey'              => $this->config->origin_key,
252
			'paymentMethodsResponse' => $payment_methods->get_original_object(),
253
			'amount'                 => AmountTransformer::transform( $payment->get_total_amount() )->get_json(),
254
		);
255
256
		/**
257
		 * Filters the Adyen checkout configuration.
258
		 *
259
		 * @param object $configuration Adyen checkout configuration.
260
		 * @since 1.2.0
261
		 */
262
		$configuration = apply_filters( 'pronamic_pay_adyen_checkout_configuration', $configuration );
263
264
		wp_localize_script(
265
			'pronamic-pay-adyen-checkout',
266
			'pronamicPayAdyenCheckout',
267
			array(
268
				'paymentMethodsConfiguration' => $this->get_checkout_payment_methods_configuration( $payment_method_types, $payment ),
269
				'paymentsUrl'                 => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/' . $payment_id ),
270
				'paymentsDetailsUrl'          => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/details/' ),
271
				'paymentReturnUrl'            => $payment->get_return_url(),
272
				'configuration'               => $configuration,
273
				'paymentAuthorised'           => __( 'Payment completed successfully.', 'pronamic_ideal' ),
274
				'paymentReceived'             => __( 'The order has been received and we are waiting for the payment to clear.', 'pronamic_ideal' ),
275
				'paymentRefused'              => __( 'The payment has been refused. Please try again using a different method or card.', 'pronamic_ideal' ),
276
			)
277
		);
278
279
		// Add checkout head action.
280
		add_action( 'pronamic_pay_adyen_checkout_head', array( $this, 'checkout_head' ) );
281
282
		// No cache.
283
		Core_Util::no_cache();
284
285
		require __DIR__ . '/../views/checkout-drop-in.php';
286
287
		exit;
288
	}
289
290
	/**
291
	 * Checkout head.
292
	 *
293
	 * @return void
294
	 */
295
	public function checkout_head() {
296
		wp_print_styles( 'pronamic-pay-redirect' );
297
298
		wp_print_scripts( 'pronamic-pay-adyen-checkout' );
299
300
		wp_print_styles( 'pronamic-pay-adyen-checkout' );
301
	}
302
303
	/**
304
	 * Update status of the specified payment.
305
	 *
306
	 * @param Payment $payment Payment.
307
	 *
308
	 * @return void
309
	 */
310
	public function update_status( Payment $payment ) {
311
		// Process payload on return.
312
		if ( filter_has_var( INPUT_GET, 'payload' ) ) {
313
			$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
314
315
			$payment_result_request = new PaymentResultRequest( $payload );
316
317
			try {
318
				$payment_result_response = $this->client->get_payment_result( $payment_result_request );
319
320
				PaymentResultHelper::update_payment( $payment, $payment_result_response );
321
			} catch ( \Exception $e ) {
322
				$note = sprintf(
323
				/* translators: %s: exception message */
324
					__( 'Error getting payment result: %s', 'pronamic_ideal' ),
325
					$e->getMessage()
326
				);
327
328
				$payment->add_note( $note );
329
			}
330
331
			return;
332
		}
333
334
		// Retrieve status from payment details.
335
		$payment_response = $payment->get_meta( 'adyen_payment_response' );
336
337
		if ( is_string( $payment_response ) && '' !== $payment_response ) {
338
			$payment_response = \json_decode( $payment_response );
339
340
			$payment_response = PaymentResponse::from_object( $payment_response );
341
342
			$details_result = $payment->get_meta( 'adyen_details_result' );
343
344
			// JSON decode details result meta.
345
			if ( is_string( $details_result ) && '' !== $details_result ) {
346
				$details_result = \json_decode( $details_result );
347
			}
348
349
			// Set details result meta from GET or POST request parameters.
350
			if ( '' === $details_result ) {
351
				$details_result = array();
352
353
				$details = $payment_response->get_details();
354
355
				if ( null !== $details ) {
356
					$input_type = ( 'POST' === Server::get( 'REQUEST_METHOD' ) ? INPUT_POST : INPUT_GET );
357
358
					foreach ( $details as $detail ) {
359
						$key = (string) $detail->get_key();
360
361
						$details_result[ $key ] = \filter_input( $input_type, $key, FILTER_SANITIZE_STRING );
362
					}
363
364
					$details_result = Util::filter_null( $details_result );
365
				}
366
367
				if ( ! empty( $details_result ) ) {
368
					$payment->set_meta( 'adyen_details_result', \wp_json_encode( (object) $details_result ) );
369
				}
370
			}
371
372
			$payment_data = $payment_response->get_payment_data();
373
374
			// Do not attempt to retrieve status without any request data,
375
			// payment status already updated when additional details were submitted (i.e. cards).
376
			if ( empty( $details_result ) && empty( $payment_data ) ) {
377
				return;
378
			}
379
380
			// Update payment status from payment details.
381
			$payment_details_request = new PaymentDetailsRequest();
382
383
			$payment_details_request->set_details( (object) $details_result );
384
385
			$payment_details_request->set_payment_data( $payment_data );
386
387
			try {
388
				$payment_details_response = $this->client->request_payment_details( $payment_details_request );
389
390
				PaymentResponseHelper::update_payment( $payment, $payment_details_response );
391
			} catch ( \Exception $e ) {
392
				$note = sprintf(
393
					/* translators: %s: exception message */
394
					__( 'Error getting payment details: %s', 'pronamic_ideal' ),
395
					$e->getMessage()
396
				);
397
398
				$payment->add_note( $note );
399
			}
400
		}
401
	}
402
403
	/**
404
	 * Create payment.
405
	 *
406
	 * @param Payment       $payment        Payment.
407
	 * @param PaymentMethod $payment_method Payment method.
408
	 *
409
	 * @return PaymentResponse
410
	 * @throws \InvalidArgumentException Throws exception on invalid amount.
411
	 * @throws \Exception Throws exception if payment creation request fails.
412
	 */
413
	public function create_payment( Payment $payment, PaymentMethod $payment_method ) {
414
		$amount = AmountTransformer::transform( $payment->get_total_amount() );
415
416
		// Payment request.
417
		$payment_request = new PaymentRequest(
418
			$amount,
419
			$this->config->get_merchant_account(),
420
			strval( $payment->get_id() ),
421
			$payment->get_return_url(),
422
			$payment_method
423
		);
424
425
		/**
426
		 * Application info.
427
		 *
428
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/payments__reqParam_applicationInfo
429
		 * @link https://docs.adyen.com/development-resources/building-adyen-solutions
430
		 */
431
		$application_info = new ApplicationInfo();
432
433
		$application_info->merchant_application = (object) array(
434
			'name'    => 'Pronamic Pay',
435
			'version' => \pronamic_pay_plugin()->get_version(),
436
		);
437
438
		$application_info->external_platform = (object) array(
439
			'integrator' => 'Pronamic',
440
			'name'       => 'WordPress',
441
			'version'    => \get_bloginfo( 'version' ),
442
		);
443
444
		$payment_request->set_application_info( $application_info );
445
446
		// Set country code.
447
		$locale = Util::get_payment_locale( $payment );
448
449
		$country_code = \Locale::getRegion( $locale );
450
451
		$billing_address = $payment->get_billing_address();
452
453
		if ( null !== $billing_address ) {
454
			$country = $billing_address->get_country_code();
455
456
			if ( ! empty( $country ) ) {
457
				$country_code = $country;
458
			}
459
		}
460
461
		$payment_request->set_country_code( $country_code );
462
463
		// Complement payment request.
464
		PaymentRequestHelper::complement( $payment, $payment_request );
465
466
		// Create payment.
467
		$payment_response = $this->client->create_payment( $payment_request );
468
469
		/*
470
		 * Store payment response for later requests to `/payments/details`.
471
		 *
472
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/payments/details
473
		 */
474
		$payment->set_meta( 'adyen_payment_response', $payment_response->get_json() );
475
476
		// Update payment status based on response.
477
		PaymentResponseHelper::update_payment( $payment, $payment_response );
478
479
		return $payment_response;
480
	}
481
482
	/**
483
	 * Send payment details.
484
	 *
485
	 * @param PaymentDetailsRequest $payment_details_request Payment details request.
486
	 *
487
	 * @return PaymentResponse
488
	 * @throws \Exception Throws error if request fails.
489
	 */
490
	public function send_payment_details( PaymentDetailsRequest $payment_details_request ) {
491
		$payment_response = $this->client->request_payment_details( $payment_details_request );
492
493
		return $payment_response;
494
	}
495
496
	/**
497
	 * Get checkout payment methods configuration.
498
	 *
499
	 * @param array<int, string> $payment_method_types Payment method types.
500
	 * @param Payment            $payment              Payment.
501
	 *
502
	 * @return object
503
	 */
504
	public function get_checkout_payment_methods_configuration( $payment_method_types, Payment $payment ) {
505
		$configuration = array();
506
507
		/*
508
		 * Cards.
509
		 *
510
		 * @link https://docs.adyen.com/payment-methods/cards/web-drop-in#show-the-available-cards-in-your-payment-form
511
		 */
512
		if ( \in_array( PaymentMethodType::SCHEME, $payment_method_types, true ) ) {
513
			$configuration['card'] = array(
514
				'enableStoreDetails' => true,
515
				'hasHolderName'      => true,
516
				'holderNameRequired' => true,
517
				'hideCVC'            => false,
518
				'name'               => __( 'Credit or debit card', 'pronamic_ideal' ),
519
			);
520
		}
521
522
		/*
523
		 * Google Pay.
524
		 *
525
		 * @link https://docs.adyen.com/payment-methods/google-pay/web-drop-in#show-google-pay-in-your-payment-form
526
		 */
527
		if ( \in_array( PaymentMethodType::GOOGLE_PAY, $payment_method_types, true ) ) {
528
			$configuration['paywithgoogle'] = array(
529
				'environment'   => ( self::MODE_TEST === $this->config->mode ? 'TEST' : 'PRODUCTION' ),
530
				'amount'        => array(
531
					'currency' => $payment->get_total_amount()->get_currency()->get_alphabetic_code(),
532
					'value'    => $payment->get_total_amount()->get_minor_units(),
533
				),
534
				'configuration' => array(
535
					'gatewayMerchantId' => $this->config->merchant_account,
536
				),
537
			);
538
539
			if ( self::MODE_LIVE === $this->config->mode ) {
540
				$configuration['paywithgoogle']['configuration']['merchantIdentifier'] = $this->config->get_google_pay_merchant_identifier();
541
			}
542
		}
543
544
		return (object) $configuration;
545
	}
546
}
547