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