WebSdkGateway::checkout_head()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Web SDK 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
 * Web SDK gateway
25
 *
26
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
27
 *
28
 * @author  Remco Tolsma
29
 * @version 1.0.5
30
 * @since   1.0.0
31
 */
32
class WebSdkGateway extends AbstractGateway {
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
	 * Constructs and initializes an Adyen gateway.
44
	 *
45
	 * @param Config $config Config.
46
	 */
47 2
	public function __construct( Config $config ) {
48 2
		parent::__construct( $config );
49
50
		// Supported features.
51 2
		$this->supports = array(
52
			'webhook_log',
53
			'webhook',
54
		);
55 2
	}
56
57
	/**
58
	 * Get supported payment methods
59
	 *
60
	 * @see Core_Gateway::get_supported_payment_methods()
61
	 *
62
	 * @return array<string>
63
	 */
64 1
	public function get_supported_payment_methods() {
65
		return array(
66 1
			PaymentMethods::BANCONTACT,
67
			PaymentMethods::CREDIT_CARD,
68
			PaymentMethods::DIRECT_DEBIT,
69
			PaymentMethods::GIROPAY,
70
			PaymentMethods::IDEAL,
71
			PaymentMethods::MAESTRO,
72
			PaymentMethods::SOFORT,
73
		);
74
	}
75
76
	/**
77
	 * Start.
78
	 *
79
	 * @param Payment $payment Payment.
80
	 * @return void
81
	 */
82
	public function start( Payment $payment ) {
83
		// Amount.
84
		try {
85
			$amount = AmountTransformer::transform( $payment->get_total_amount() );
86
		} catch ( InvalidArgumentException $e ) {
87
			$this->error = new WP_Error( 'adyen_error', $e->getMessage() );
88
89
			return;
90
		}
91
92
		// Payment method type.
93
		$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
94
95
		// Country.
96
		$locale = Util::get_payment_locale( $payment );
97
98
		$country_code = Locale::getRegion( $locale );
99
100
		// Set country from billing address.
101
		$billing_address = $payment->get_billing_address();
102
103
		if ( null !== $billing_address ) {
104
			$country = $billing_address->get_country_code();
105
106
			if ( ! empty( $country ) ) {
107
				$country_code = $country;
108
			}
109
		}
110
111
		/*
112
		 * API Integration
113
		 *
114
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
115
		 */
116
		$api_integration_payment_method_types = array(
117
			PaymentMethodType::IDEAL,
118
			PaymentMethodType::DIRECT_EBANKING,
119
		);
120
121
		if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) {
122
			$payment_method = array(
123
				'type' => $payment_method_type,
124
			);
125
126
			if ( PaymentMethodType::IDEAL === $payment_method_type ) {
127
				$payment_method['issuer'] = (string) $payment->get_issuer();
128
			}
129
130
			// API integration.
131
			$payment_request = new PaymentRequest(
132
				$amount,
133
				$this->config->get_merchant_account(),
134
				(string) $payment->get_id(),
135
				$payment->get_return_url(),
136
				new PaymentMethod( (object) $payment_method )
137
			);
138
139
			$payment_request->set_country_code( $country_code );
140
141
			PaymentRequestHelper::complement( $payment, $payment_request );
142
143
			try {
144
				$payment_response = $this->client->create_payment( $payment_request );
145
			} catch ( Exception $e ) {
146
				$this->error = new WP_Error( 'adyen_error', $e->getMessage() );
147
148
				return;
149
			}
150
151
			$payment->set_transaction_id( $payment_response->get_psp_reference() );
152
153
			$redirect = $payment_response->get_redirect();
154
155
			if ( null !== $redirect ) {
156
				$payment->set_action_url( $redirect->get_url() );
157
			}
158
159
			// Return early so SDK integration code will not be executed for API integration.
160
			return;
161
		}
162
163
		/**
164
		 * SDK Integration
165
		 *
166
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession
167
		 */
168
		$payment_session_request = new PaymentSessionRequest(
169
			$amount,
170
			$this->config->get_merchant_account(),
171
			(string) $payment->get_id(),
172
			$payment->get_return_url(),
173
			$country_code
174
		);
175
176
		PaymentRequestHelper::complement( $payment, $payment_session_request );
177
178
		// Origin.
179
		$origin = home_url();
180
181
		$origin_url = wp_parse_url( home_url() );
182
183
		if ( is_array( $origin_url ) && isset( $origin_url['scheme'], $origin_url['host'] ) ) {
184
			$origin = sprintf(
185
				'%s://%s',
186
				$origin_url['scheme'],
187
				$origin_url['host']
188
			);
189
		}
190
191
		$payment_session_request->set_origin( $origin );
192
		$payment_session_request->set_sdk_version( self::SDK_VERSION );
193
194
		if ( null !== $payment_method_type ) {
195
			$payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) );
196
		}
197
198
		try {
199
			$payment_session_response = $this->client->create_payment_session( $payment_session_request );
200
		} catch ( Exception $e ) {
201
			$this->error = new WP_Error( 'adyen_error', $e->getMessage() );
202
203
			return;
204
		}
205
206
		$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION );
207
		$payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() );
208
209
		$payment->set_action_url( $payment->get_pay_redirect_url() );
210
	}
211
212
	/**
213
	 * Payment redirect.
214
	 *
215
	 * @param Payment $payment Payment.
216
	 * @return void
217
	 */
218
	public function payment_redirect( Payment $payment ) {
219
		$sdk_version     = $payment->get_meta( 'adyen_sdk_version' );
220
		$payment_session = $payment->get_meta( 'adyen_payment_session' );
221
222
		if ( empty( $sdk_version ) || empty( $payment_session ) ) {
223
			return;
224
		}
225
226
		if ( empty( $payment->config_id ) ) {
227
			return;
228
		}
229
230
		$url = sprintf(
231
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js',
232
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
233
			$sdk_version
234
		);
235
236
		wp_register_script(
237
			'pronamic-pay-adyen-checkout',
238
			$url,
239
			array(
240
				'jquery',
241
			),
242
			$sdk_version,
243
			false
244
		);
245
246
		/**
247
		 * Config object.
248
		 *
249
		 * @link https://docs.adyen.com/checkout/web-sdk/
250
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/settings/
251
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields
252
		 */
253
		$config_object = (object) array(
254
			'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
255
		);
256
257
		/**
258
		 * Filters the Adyen config object.
259
		 *
260
		 * @link https://github.com/wp-pay-gateways/adyen#pronamic_pay_adyen_config_object
261
		 * @link https://docs.adyen.com/checkout/web-sdk/
262
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/settings/
263
		 * @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields
264
		 *
265
		 * @param object $config_object Adyen config object.
266
		 *
267
		 * @since 1.1
268
		 */
269
		$config_object = apply_filters( 'pronamic_pay_adyen_config_object', $config_object );
270
271
		wp_localize_script(
272
			'pronamic-pay-adyen-checkout',
273
			'pronamicPayAdyenCheckout',
274
			array(
275
				'paymentsResultUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/result/' . $payment->config_id ),
276
				'paymentReturnUrl'  => $payment->get_return_url(),
277
				'paymentSession'    => $payment_session,
278
				'configObject'      => $config_object,
279
			)
280
		);
281
282
		// Add checkout head action.
283
		add_action( 'pronamic_pay_adyen_checkout_head', array( $this, 'checkout_head' ) );
284
285
		// No cache.
286
		Core_Util::no_cache();
287
288
		require __DIR__ . '/../views/checkout-web-sdk.php';
289
290
		exit;
291
	}
292
293
	/**
294
	 * Checkout head.
295
	 *
296
	 * @return void
297
	 */
298
	public function checkout_head() {
299
		wp_print_styles( 'pronamic-pay-redirect' );
300
301
		wp_print_scripts( 'pronamic-pay-adyen-checkout' );
302
	}
303
304
	/**
305
	 * Update status of the specified payment.
306
	 *
307
	 * @param Payment $payment Payment.
308
	 *
309
	 * @return void
310
	 */
311
	public function update_status( Payment $payment ) {
312
		// Process payload on return.
313
		if ( ! filter_has_var( INPUT_GET, 'payload' ) ) {
314
			return;
315
		}
316
317
		$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
318
319
		$payment_result_request = new PaymentResultRequest( $payload );
320
321
		try {
322
			$payment_result_response = $this->client->get_payment_result( $payment_result_request );
323
324
			PaymentResultHelper::update_payment( $payment, $payment_result_response );
325
		} catch ( Exception $e ) {
326
			$note = sprintf(
327
				/* translators: %s: exception message */
328
				__( 'Error getting payment result: %s', 'pronamic_ideal' ),
329
				$e->getMessage()
330
			);
331
332
			$payment->add_note( $note );
333
		}
334
	}
335
}
336