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 | * Slug of this gateway. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const SLUG = 'adyen'; |
||
39 | |||
40 | /** |
||
41 | * Web SDK version. |
||
42 | * |
||
43 | * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | const SDK_VERSION = '1.9.2'; |
||
48 | |||
49 | /** |
||
50 | * Client. |
||
51 | * |
||
52 | * @var Client |
||
53 | */ |
||
54 | public $client; |
||
55 | |||
56 | /** |
||
57 | * Constructs and initializes an Adyen gateway. |
||
58 | * |
||
59 | * @param Config $config Config. |
||
60 | */ |
||
61 | 2 | public function __construct( Config $config ) { |
|
62 | 2 | parent::__construct( $config ); |
|
63 | |||
64 | 2 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
|
65 | 2 | $this->set_slug( self::SLUG ); |
|
66 | |||
67 | 2 | $this->client = new Client( $config ); |
|
68 | 2 | } |
|
69 | |||
70 | /** |
||
71 | * Get supported payment methods |
||
72 | * |
||
73 | * @see Core_Gateway::get_supported_payment_methods() |
||
74 | */ |
||
75 | 1 | public function get_supported_payment_methods() { |
|
76 | return array( |
||
77 | 1 | PaymentMethods::BANCONTACT, |
|
78 | 1 | PaymentMethods::CREDIT_CARD, |
|
79 | 1 | PaymentMethods::DIRECT_DEBIT, |
|
80 | 1 | PaymentMethods::GIROPAY, |
|
81 | 1 | PaymentMethods::IDEAL, |
|
82 | 1 | PaymentMethods::MAESTRO, |
|
83 | 1 | PaymentMethods::SOFORT, |
|
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Start. |
||
89 | * |
||
90 | * @see Plugin::start() |
||
91 | * |
||
92 | * @param Payment $payment Payment. |
||
93 | * @return void |
||
94 | */ |
||
95 | public function start( Payment $payment ) { |
||
96 | // Amount. |
||
97 | try { |
||
98 | $amount = AmountTransformer::transform( $payment->get_total_amount() ); |
||
99 | } catch ( InvalidArgumentException $e ) { |
||
100 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
101 | |||
102 | return; |
||
103 | } |
||
104 | |||
105 | // Payment method type. |
||
106 | $payment_method_type = PaymentMethodType::transform( $payment->get_method() ); |
||
107 | |||
108 | // Country. |
||
109 | $locale = get_locale(); |
||
110 | |||
111 | $customer = $payment->get_customer(); |
||
112 | |||
113 | if ( null !== $customer ) { |
||
114 | $locale = $customer->get_locale(); |
||
115 | } |
||
116 | |||
117 | $locale = strval( $locale ); |
||
118 | |||
119 | $country_code = Locale::getRegion( $locale ); |
||
120 | |||
121 | /* |
||
122 | * API Integration |
||
123 | * |
||
124 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments |
||
125 | */ |
||
126 | $api_integration_payment_method_types = array( |
||
127 | PaymentMethodType::IDEAL, |
||
128 | PaymentMethodType::DIRECT_EBANKING, |
||
129 | ); |
||
130 | |||
131 | if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) { |
||
132 | $payment_method = new PaymentMethod( $payment_method_type ); |
||
133 | |||
134 | if ( PaymentMethodType::IDEAL === $payment_method_type ) { |
||
135 | $payment_method = new PaymentMethodIDeal( $payment_method_type, (string) $payment->get_issuer() ); |
||
136 | } |
||
137 | |||
138 | // API integration. |
||
139 | $payment_request = new PaymentRequest( |
||
140 | $amount, |
||
141 | $this->config->get_merchant_account(), |
||
142 | strval( $payment->get_id() ), |
||
143 | $payment->get_return_url(), |
||
144 | $payment_method |
||
145 | ); |
||
146 | |||
147 | $payment_request->set_country_code( $country_code ); |
||
148 | |||
149 | PaymentRequestHelper::complement( $payment, $payment_request ); |
||
150 | |||
151 | try { |
||
152 | $payment_response = $this->client->create_payment( $payment_request ); |
||
153 | } catch ( Exception $e ) { |
||
154 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
155 | |||
156 | return; |
||
157 | } |
||
158 | |||
159 | $payment->set_transaction_id( $payment_response->get_psp_reference() ); |
||
160 | |||
161 | $redirect = $payment_response->get_redirect(); |
||
162 | |||
163 | if ( null !== $redirect ) { |
||
164 | $payment->set_action_url( $redirect->get_url() ); |
||
165 | } |
||
166 | |||
167 | // Return early so SDK integration code will not be executed for API integration. |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | /* |
||
172 | * SDK Integration |
||
173 | * |
||
174 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession |
||
175 | */ |
||
176 | $payment_session_request = new PaymentSessionRequest( |
||
177 | $amount, |
||
178 | $this->config->get_merchant_account(), |
||
179 | strval( $payment->get_id() ), |
||
180 | $payment->get_return_url(), |
||
181 | $country_code |
||
182 | ); |
||
183 | |||
184 | PaymentRequestHelper::complement( $payment, $payment_session_request ); |
||
185 | |||
186 | $origin = sprintf( |
||
187 | '%s://%s', |
||
188 | wp_parse_url( home_url(), PHP_URL_SCHEME ), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
189 | wp_parse_url( home_url(), PHP_URL_HOST ) |
||
190 | ); |
||
191 | |||
192 | $payment_session_request->set_origin( $origin ); |
||
193 | $payment_session_request->set_sdk_version( self::SDK_VERSION ); |
||
194 | |||
195 | if ( null !== $payment_method_type ) { |
||
196 | $payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) ); |
||
197 | } |
||
198 | |||
199 | try { |
||
200 | $payment_session_response = $this->client->create_payment_session( $payment_session_request ); |
||
201 | } catch ( Exception $e ) { |
||
202 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
203 | |||
204 | return; |
||
205 | } |
||
206 | |||
207 | $payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION ); |
||
208 | $payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() ); |
||
209 | |||
210 | $payment->set_action_url( $payment->get_pay_redirect_url() ); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Payment redirect. |
||
215 | * |
||
216 | * @param Payment $payment Payment. |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | public function payment_redirect( Payment $payment ) { |
||
221 | $sdk_version = $payment->get_meta( 'adyen_sdk_version' ); |
||
222 | $payment_session = $payment->get_meta( 'adyen_payment_session' ); |
||
223 | |||
224 | if ( empty( $sdk_version ) || empty( $payment_session ) ) { |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | $url = sprintf( |
||
229 | 'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js', |
||
230 | ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
231 | $sdk_version |
||
232 | ); |
||
233 | |||
234 | wp_register_script( |
||
235 | 'pronamic-pay-adyen-checkout', |
||
236 | $url, |
||
237 | array( |
||
238 | 'jquery', |
||
239 | ), |
||
240 | $sdk_version, |
||
241 | false |
||
242 | ); |
||
243 | |||
244 | wp_localize_script( |
||
245 | 'pronamic-pay-adyen-checkout', |
||
246 | 'pronamicPayAdyenCheckout', |
||
247 | array( |
||
248 | 'paymentsResultUrl' => rest_url( Integration::REST_ROUTE_NAMESPACE . '/payments/result/' . $payment->config_id ), |
||
249 | 'paymentReturnUrl' => $payment->get_return_url(), |
||
250 | 'paymentSession' => $payment_session, |
||
251 | 'configObject' => array( |
||
252 | 'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
253 | ), |
||
254 | ) |
||
255 | ); |
||
256 | |||
257 | // No cache. |
||
258 | Core_Util::no_cache(); |
||
259 | |||
260 | require __DIR__ . '/../views/checkout.php'; |
||
261 | |||
262 | exit; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Update status of the specified payment. |
||
267 | * |
||
268 | * @param Payment $payment Payment. |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function update_status( Payment $payment ) { |
||
273 | // Process payload on return. |
||
274 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
275 | return; |
||
276 | } |
||
277 | |||
278 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
279 | |||
280 | $payment_result_request = new PaymentResultRequest( $payload ); |
||
281 | |||
282 | try { |
||
283 | $payment_result_response = $this->client->get_payment_result( $payment_result_request ); |
||
284 | |||
285 | PaymentResultHelper::update_payment( $payment, $payment_result_response ); |
||
286 | } catch ( Exception $e ) { |
||
287 | $note = sprintf( |
||
288 | /* translators: %s: exception message */ |
||
289 | __( 'Error getting payment result: %s', 'pronamic_ideal' ), |
||
290 | $e->getMessage() |
||
291 | ); |
||
292 | |||
293 | $payment->add_note( $note ); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get available payment methods. |
||
299 | * |
||
300 | * @see Core_Gateway::get_available_payment_methods() |
||
301 | */ |
||
302 | public function get_available_payment_methods() { |
||
303 | $core_payment_methods = array(); |
||
304 | |||
305 | try { |
||
306 | $payment_methods_response = $this->client->get_payment_methods(); |
||
307 | } catch ( Exception $e ) { |
||
308 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
309 | |||
310 | return $core_payment_methods; |
||
311 | } |
||
312 | |||
313 | foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) { |
||
314 | $core_payment_method = PaymentMethodType::to_wp( $payment_method->get_type() ); |
||
315 | |||
316 | $core_payment_methods[] = $core_payment_method; |
||
317 | } |
||
318 | |||
319 | $core_payment_methods = array_filter( $core_payment_methods ); |
||
320 | $core_payment_methods = array_unique( $core_payment_methods ); |
||
321 | |||
322 | return $core_payment_methods; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Get issuers. |
||
327 | * |
||
328 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
329 | * @return array |
||
330 | */ |
||
331 | public function get_issuers() { |
||
332 | $issuers = array(); |
||
333 | |||
334 | try { |
||
335 | $payment_methods_response = $this->client->get_payment_methods(); |
||
336 | } catch ( Exception $e ) { |
||
337 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
338 | |||
339 | return $issuers; |
||
340 | } |
||
341 | |||
342 | $payment_methods = $payment_methods_response->get_payment_methods(); |
||
343 | |||
344 | // Limit to iDEAL payment methods. |
||
345 | $payment_methods = array_filter( |
||
346 | $payment_methods, |
||
347 | /** |
||
348 | * Check if payment method is iDEAL. |
||
349 | * |
||
350 | * @param PaymentMethod $payment_method Payment method. |
||
351 | * @return boolean True if payment method is iDEAL, false otherwise. |
||
352 | */ |
||
353 | function( $payment_method ) { |
||
354 | return ( PaymentMethodType::IDEAL === $payment_method->get_type() ); |
||
355 | } |
||
356 | ); |
||
357 | |||
358 | foreach ( $payment_methods as $payment_method ) { |
||
359 | $details = $payment_method->get_details(); |
||
360 | |||
361 | if ( is_array( $details ) ) { |
||
362 | foreach ( $details as $detail ) { |
||
363 | if ( 'issuer' === $detail->key && 'select' === $detail->type ) { |
||
364 | foreach ( $detail->items as $item ) { |
||
365 | $issuers[ $item->id ] = $item->name; |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | |||
372 | if ( empty( $issuers ) ) { |
||
373 | return $issuers; |
||
374 | } |
||
375 | |||
376 | return array( |
||
377 | array( |
||
378 | 'options' => $issuers, |
||
379 | ), |
||
380 | ); |
||
381 | } |
||
382 | } |
||
383 |