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
|
|
|
$this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
97
|
|
|
|
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Payment method type. |
102
|
|
|
$payment_method_type = PaymentMethodType::transform( $payment->get_method() ); |
103
|
|
|
|
104
|
|
|
// Country. |
105
|
|
|
$locale = get_locale(); |
106
|
|
|
|
107
|
|
|
$customer = $payment->get_customer(); |
108
|
|
|
|
109
|
|
|
if ( null !== $customer ) { |
110
|
|
|
$locale = $customer->get_locale(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$locale = strval( $locale ); |
114
|
|
|
|
115
|
|
|
$country_code = Locale::getRegion( $locale ); |
116
|
|
|
|
117
|
|
|
// Set country from billing address. |
118
|
|
|
$billing_address = $payment->get_billing_address(); |
119
|
|
|
|
120
|
|
|
if ( null !== $billing_address ) { |
121
|
|
|
$country = $billing_address->get_country_code(); |
122
|
|
|
|
123
|
|
|
if ( ! empty( $country ) ) { |
124
|
|
|
$country_code = $country; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
* API Integration |
130
|
|
|
* |
131
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments |
132
|
|
|
*/ |
133
|
|
|
$api_integration_payment_method_types = array( |
134
|
|
|
PaymentMethodType::IDEAL, |
135
|
|
|
PaymentMethodType::DIRECT_EBANKING, |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) { |
139
|
|
|
$payment_method = new PaymentMethod( $payment_method_type ); |
140
|
|
|
|
141
|
|
|
if ( PaymentMethodType::IDEAL === $payment_method_type ) { |
142
|
|
|
$payment_method = new PaymentMethodIDeal( $payment_method_type, (string) $payment->get_issuer() ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// API integration. |
146
|
|
|
$payment_request = new PaymentRequest( |
147
|
|
|
$amount, |
148
|
|
|
$this->config->get_merchant_account(), |
149
|
|
|
strval( $payment->get_id() ), |
150
|
|
|
$payment->get_return_url(), |
151
|
|
|
$payment_method |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$payment_request->set_country_code( $country_code ); |
155
|
|
|
|
156
|
|
|
PaymentRequestHelper::complement( $payment, $payment_request ); |
157
|
|
|
|
158
|
|
|
try { |
159
|
|
|
$payment_response = $this->client->create_payment( $payment_request ); |
160
|
|
|
} catch ( Exception $e ) { |
161
|
|
|
$this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
162
|
|
|
|
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$payment->set_transaction_id( $payment_response->get_psp_reference() ); |
167
|
|
|
|
168
|
|
|
$redirect = $payment_response->get_redirect(); |
169
|
|
|
|
170
|
|
|
if ( null !== $redirect ) { |
171
|
|
|
$payment->set_action_url( $redirect->get_url() ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Return early so SDK integration code will not be executed for API integration. |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/* |
179
|
|
|
* SDK Integration |
180
|
|
|
* |
181
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession |
182
|
|
|
*/ |
183
|
|
|
$payment_session_request = new PaymentSessionRequest( |
184
|
|
|
$amount, |
185
|
|
|
$this->config->get_merchant_account(), |
186
|
|
|
strval( $payment->get_id() ), |
187
|
|
|
$payment->get_return_url(), |
188
|
|
|
$country_code |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
PaymentRequestHelper::complement( $payment, $payment_session_request ); |
192
|
|
|
|
193
|
|
|
// Origin. |
194
|
|
|
$origin = home_url(); |
195
|
|
|
|
196
|
|
|
$origin_url = wp_parse_url( home_url() ); |
197
|
|
|
|
198
|
|
|
if ( is_array( $origin_url ) && isset( $origin_url['scheme'], $origin_url['host'] ) ) { |
199
|
|
|
$origin = sprintf( |
200
|
|
|
'%s://%s', |
201
|
|
|
$origin_url['scheme'], |
202
|
|
|
$origin_url['host'] |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$payment_session_request->set_origin( $origin ); |
207
|
|
|
$payment_session_request->set_sdk_version( self::SDK_VERSION ); |
208
|
|
|
|
209
|
|
|
if ( null !== $payment_method_type ) { |
210
|
|
|
$payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
try { |
214
|
|
|
$payment_session_response = $this->client->create_payment_session( $payment_session_request ); |
215
|
|
|
} catch ( Exception $e ) { |
216
|
|
|
$this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
217
|
|
|
|
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION ); |
222
|
|
|
$payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() ); |
223
|
|
|
|
224
|
|
|
$payment->set_action_url( $payment->get_pay_redirect_url() ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Payment redirect. |
229
|
|
|
* |
230
|
|
|
* @param Payment $payment Payment. |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function payment_redirect( Payment $payment ) { |
235
|
|
|
$sdk_version = $payment->get_meta( 'adyen_sdk_version' ); |
236
|
|
|
$payment_session = $payment->get_meta( 'adyen_payment_session' ); |
237
|
|
|
|
238
|
|
|
if ( empty( $sdk_version ) || empty( $payment_session ) ) { |
239
|
|
|
return; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ( empty( $payment->config_id ) ) { |
243
|
|
|
return; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$url = sprintf( |
247
|
|
|
'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js', |
248
|
|
|
( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
249
|
|
|
$sdk_version |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
wp_register_script( |
253
|
|
|
'pronamic-pay-adyen-checkout', |
254
|
|
|
$url, |
255
|
|
|
array( |
256
|
|
|
'jquery', |
257
|
|
|
), |
258
|
|
|
$sdk_version, |
259
|
|
|
false |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Config object. |
264
|
|
|
* |
265
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/ |
266
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/customization/settings/ |
267
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields |
268
|
|
|
*/ |
269
|
|
|
$config_object = (object) array( |
270
|
|
|
'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Filters the Adyen config object. |
275
|
|
|
* |
276
|
|
|
* @link https://github.com/wp-pay-gateways/adyen#pronamic_pay_adyen_config_object |
277
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/ |
278
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/customization/settings/ |
279
|
|
|
* @link https://docs.adyen.com/checkout/web-sdk/customization/styling/#styling-the-card-fields |
280
|
|
|
* |
281
|
|
|
* @param object $config_object Adyen config object. |
282
|
|
|
* |
283
|
|
|
* @since 1.1 |
284
|
|
|
*/ |
285
|
|
|
$config_object = apply_filters( 'pronamic_pay_adyen_config_object', $config_object ); |
286
|
|
|
|
287
|
|
|
wp_localize_script( |
288
|
|
|
'pronamic-pay-adyen-checkout', |
289
|
|
|
'pronamicPayAdyenCheckout', |
290
|
|
|
array( |
291
|
|
|
'paymentsResultUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/result/' . $payment->config_id ), |
292
|
|
|
'paymentReturnUrl' => $payment->get_return_url(), |
293
|
|
|
'paymentSession' => $payment_session, |
294
|
|
|
'configObject' => $config_object, |
295
|
|
|
) |
296
|
|
|
); |
297
|
|
|
|
298
|
|
|
// Add checkout head action. |
299
|
|
|
add_action( 'pronamic_pay_adyen_checkout_head', array( $this, 'checkout_head' ) ); |
300
|
|
|
|
301
|
|
|
// No cache. |
302
|
|
|
Core_Util::no_cache(); |
303
|
|
|
|
304
|
|
|
require __DIR__ . '/../views/checkout.php'; |
305
|
|
|
|
306
|
|
|
exit; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Checkout head. |
311
|
|
|
* |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
public function checkout_head() { |
315
|
|
|
wp_print_styles( 'pronamic-pay-redirect' ); |
316
|
|
|
|
317
|
|
|
wp_print_scripts( 'pronamic-pay-adyen-checkout' ); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Update status of the specified payment. |
322
|
|
|
* |
323
|
|
|
* @param Payment $payment Payment. |
324
|
|
|
* |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
public function update_status( Payment $payment ) { |
328
|
|
|
// Process payload on return. |
329
|
|
|
if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
334
|
|
|
|
335
|
|
|
$payment_result_request = new PaymentResultRequest( $payload ); |
336
|
|
|
|
337
|
|
|
try { |
338
|
|
|
$payment_result_response = $this->client->get_payment_result( $payment_result_request ); |
339
|
|
|
|
340
|
|
|
PaymentResultHelper::update_payment( $payment, $payment_result_response ); |
341
|
|
|
} catch ( Exception $e ) { |
342
|
|
|
$note = sprintf( |
343
|
|
|
/* translators: %s: exception message */ |
344
|
|
|
__( 'Error getting payment result: %s', 'pronamic_ideal' ), |
345
|
|
|
$e->getMessage() |
346
|
|
|
); |
347
|
|
|
|
348
|
|
|
$payment->add_note( $note ); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get available payment methods. |
354
|
|
|
* |
355
|
|
|
* @see Core_Gateway::get_available_payment_methods() |
356
|
|
|
*/ |
357
|
|
|
public function get_available_payment_methods() { |
358
|
|
|
$core_payment_methods = array(); |
359
|
|
|
|
360
|
|
|
try { |
361
|
|
|
$payment_methods_response = $this->client->get_payment_methods(); |
362
|
|
|
} catch ( Exception $e ) { |
363
|
|
|
$this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
364
|
|
|
|
365
|
|
|
return $core_payment_methods; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) { |
369
|
|
|
$core_payment_method = PaymentMethodType::to_wp( $payment_method->get_type() ); |
370
|
|
|
|
371
|
|
|
$core_payment_methods[] = $core_payment_method; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
$core_payment_methods = array_filter( $core_payment_methods ); |
375
|
|
|
$core_payment_methods = array_unique( $core_payment_methods ); |
376
|
|
|
|
377
|
|
|
return $core_payment_methods; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Get issuers. |
382
|
|
|
* |
383
|
|
|
* @see Pronamic_WP_Pay_Gateway::get_issuers() |
384
|
|
|
* @return array |
385
|
|
|
*/ |
386
|
|
|
public function get_issuers() { |
387
|
|
|
$issuers = array(); |
388
|
|
|
|
389
|
|
|
try { |
390
|
|
|
$payment_methods_response = $this->client->get_payment_methods(); |
391
|
|
|
} catch ( Exception $e ) { |
392
|
|
|
$this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
393
|
|
|
|
394
|
|
|
return $issuers; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$payment_methods = $payment_methods_response->get_payment_methods(); |
398
|
|
|
|
399
|
|
|
// Limit to iDEAL payment methods. |
400
|
|
|
$payment_methods = array_filter( |
401
|
|
|
$payment_methods, |
402
|
|
|
/** |
403
|
|
|
* Check if payment method is iDEAL. |
404
|
|
|
* |
405
|
|
|
* @param PaymentMethod $payment_method Payment method. |
406
|
|
|
* @return boolean True if payment method is iDEAL, false otherwise. |
407
|
|
|
*/ |
408
|
|
|
function( $payment_method ) { |
409
|
|
|
return ( PaymentMethodType::IDEAL === $payment_method->get_type() ); |
410
|
|
|
} |
411
|
|
|
); |
412
|
|
|
|
413
|
|
|
foreach ( $payment_methods as $payment_method ) { |
414
|
|
|
$details = $payment_method->get_details(); |
415
|
|
|
|
416
|
|
|
if ( is_array( $details ) ) { |
417
|
|
|
foreach ( $details as $detail ) { |
418
|
|
|
if ( 'issuer' === $detail->key && 'select' === $detail->type ) { |
419
|
|
|
foreach ( $detail->items as $item ) { |
420
|
|
|
$issuers[ $item->id ] = $item->name; |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
if ( empty( $issuers ) ) { |
428
|
|
|
return $issuers; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return array( |
432
|
|
|
array( |
433
|
|
|
'options' => $issuers, |
434
|
|
|
), |
435
|
|
|
); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|