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

WebSdkGateway::get_available_payment_methods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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