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 Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
14 | use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses; |
||
15 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
16 | use Pronamic\WordPress\Pay\Core\Util; |
||
17 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
18 | use Pronamic\WordPress\Pay\Plugin; |
||
19 | |||
20 | /** |
||
21 | * Gateway |
||
22 | * |
||
23 | * @author Remco Tolsma |
||
24 | * @version 1.0.0 |
||
25 | * @since 1.0.0 |
||
26 | * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
||
27 | */ |
||
28 | class Gateway extends Core_Gateway { |
||
29 | /** |
||
30 | * Slug of this gateway. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | const SLUG = 'adyen'; |
||
35 | |||
36 | /** |
||
37 | * Web SDK version. |
||
38 | * |
||
39 | * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | const SDK_VERSION = '1.9.2'; |
||
44 | |||
45 | /** |
||
46 | * Client. |
||
47 | * |
||
48 | * @var Client |
||
49 | */ |
||
50 | protected $client; |
||
51 | |||
52 | /** |
||
53 | * Constructs and initializes an Adyen gateway. |
||
54 | * |
||
55 | * @param Config $config Config. |
||
56 | */ |
||
57 | public function __construct( Config $config ) { |
||
58 | parent::__construct( $config ); |
||
59 | |||
60 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
61 | $this->set_slug( self::SLUG ); |
||
62 | |||
63 | $this->client = new Client( $config->api_key, $config->api_live_url_prefix ); |
||
64 | $this->client->set_merchant_account( $config->merchant_account ); |
||
65 | $this->client->set_mode( $config->mode ); |
||
66 | |||
67 | $this->supports = array( |
||
68 | 'payment_redirect', |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get supported payment methods |
||
74 | * |
||
75 | * @see Core_Gateway::get_supported_payment_methods() |
||
76 | */ |
||
77 | public function get_supported_payment_methods() { |
||
78 | return array( |
||
79 | PaymentMethods::BANCONTACT, |
||
80 | PaymentMethods::CREDIT_CARD, |
||
81 | PaymentMethods::DIRECT_DEBIT, |
||
82 | PaymentMethods::GIROPAY, |
||
83 | PaymentMethods::IDEAL, |
||
84 | PaymentMethods::MAESTRO, |
||
85 | PaymentMethods::SOFORT, |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Start. |
||
91 | * |
||
92 | * @param Payment $payment Payment. |
||
93 | * |
||
94 | * @see Plugin::start() |
||
95 | */ |
||
96 | public function start( Payment $payment ) { |
||
97 | // Amount. |
||
98 | $amount = new Amount( |
||
99 | $payment->get_total_amount()->get_currency()->get_alphabetic_code(), |
||
100 | $payment->get_total_amount()->get_minor_units() |
||
0 ignored issues
–
show
|
|||
101 | ); |
||
102 | |||
103 | // Payment method. Take leap of faith for unknown payment methods. |
||
104 | $type = PaymentMethodType::transform( |
||
105 | $payment->get_method(), |
||
106 | $payment->get_method() |
||
107 | ); |
||
108 | |||
109 | $payment_method = new PaymentMethod( $type ); |
||
110 | |||
111 | switch ( $payment->get_method() ) { |
||
112 | case PaymentMethods::IDEAL: |
||
113 | $payment_method->issuer = $payment->get_issuer(); |
||
114 | |||
115 | break; |
||
116 | } |
||
117 | |||
118 | // Country. |
||
119 | $locale = get_locale(); |
||
120 | |||
121 | if ( null !== $payment->get_customer() ) { |
||
122 | $locale = $payment->get_customer()->get_locale(); |
||
123 | } |
||
124 | |||
125 | $locale = explode( '_', $locale ); |
||
126 | |||
127 | $country_code = strtoupper( substr( $locale[1], 0, 2 ) ); |
||
128 | |||
129 | // Create payment or payment session request. |
||
130 | switch ( $payment->get_method() ) { |
||
131 | case PaymentMethods::IDEAL: |
||
132 | case PaymentMethods::SOFORT: |
||
133 | // API integration. |
||
134 | $request = new PaymentRequest( |
||
135 | $amount, |
||
136 | $this->config->merchant_account, |
||
137 | $payment->get_id(), |
||
138 | $payment->get_return_url(), |
||
139 | $payment_method |
||
140 | ); |
||
141 | |||
142 | $request->set_country_code( $country_code ); |
||
143 | |||
144 | break; |
||
145 | default: |
||
146 | // Web SDK integration. |
||
147 | $request = new PaymentSessionRequest( |
||
148 | $amount, |
||
149 | $this->config->merchant_account, |
||
150 | $payment->get_id(), |
||
151 | $payment->get_return_url(), |
||
152 | $country_code |
||
153 | ); |
||
154 | |||
155 | $request->set_origin( home_url() ); |
||
156 | $request->set_sdk_version( self::SDK_VERSION ); |
||
157 | |||
158 | // Set allowed payment methods. |
||
159 | $allowed_methods = array( $type ); |
||
160 | |||
161 | // Add all available payment methods if no payment method is given. |
||
162 | if ( empty( $type ) ) { |
||
163 | $allowed_methods = array(); |
||
164 | |||
165 | foreach ( $this->get_available_payment_methods() as $method ) { |
||
166 | $allowed_methods[] = PaymentMethodType::transform( $method ); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | $request->set_allowed_payment_methods( $allowed_methods ); |
||
171 | } |
||
172 | |||
173 | // Channel. |
||
174 | $request->set_channel( 'Web' ); |
||
175 | |||
176 | // Shopper. |
||
177 | $request->set_shopper_statement( $payment->get_description() ); |
||
178 | |||
179 | if ( null !== $payment->get_customer() ) { |
||
180 | $customer = $payment->get_customer(); |
||
181 | |||
182 | $request->set_shopper_ip( $customer->get_ip_address() ); |
||
183 | $request->set_shopper_locale( $customer->get_locale() ); |
||
184 | $request->set_shopper_reference( $customer->get_user_id() ); |
||
185 | $request->set_telephone_number( $customer->get_phone() ); |
||
186 | |||
187 | // Shopper name. |
||
188 | if ( null !== $customer->get_name() ) { |
||
189 | $shopper_name = new Name( |
||
190 | $customer->get_name()->get_first_name(), |
||
191 | $customer->get_name()->get_last_name(), |
||
192 | GenderTransformer::transform( $customer->get_gender() ) |
||
193 | ); |
||
194 | |||
195 | $request->set_shopper_name( $shopper_name ); |
||
196 | } |
||
197 | |||
198 | // Date of birth. |
||
199 | if ( null !== $customer->get_birth_date() ) { |
||
200 | $request->set_date_of_birth( $customer->get_birth_date()->format( 'YYYY-MM-DD' ) ); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | // Billing address. |
||
205 | if ( null !== $payment->get_billing_address() ) { |
||
206 | $address = $payment->get_billing_address(); |
||
207 | |||
208 | $billing_address = new Address( $address->get_country_code() ); |
||
209 | |||
210 | $billing_address->set_city( $address->get_city() ); |
||
211 | $billing_address->set_house_number_or_name( $address->get_house_number() ); |
||
212 | $billing_address->set_postal_code( $address->get_postal_code() ); |
||
213 | $billing_address->set_state_or_province( $address->get_region() ); |
||
214 | $billing_address->set_street( $address->get_street_name() ); |
||
215 | |||
216 | $request->set_billing_address( $billing_address ); |
||
217 | } |
||
218 | |||
219 | // Delivery address. |
||
220 | if ( null !== $payment->get_shipping_address() ) { |
||
221 | $address = $payment->get_shipping_address(); |
||
222 | |||
223 | $delivery_address = new Address( $address->get_country_code() ); |
||
224 | |||
225 | $delivery_address->set_city( $address->get_city() ); |
||
226 | $delivery_address->set_house_number_or_name( sprintf( '%s %s', $address->get_house_number(), $address->get_house_number_addition() ) ); |
||
227 | $delivery_address->set_postal_code( $address->get_postal_code() ); |
||
228 | $delivery_address->set_state_or_province( $address->get_region() ); |
||
229 | $delivery_address->set_street( $address->get_street_name() ); |
||
230 | |||
231 | $request->set_delivery_address( $delivery_address ); |
||
232 | } |
||
233 | |||
234 | // Lines. |
||
235 | $lines = $payment->get_lines(); |
||
236 | |||
237 | if ( null !== $lines ) { |
||
238 | $line_items = $request->new_items(); |
||
239 | |||
240 | $i = 1; |
||
241 | |||
242 | foreach ( $lines as $line ) { |
||
243 | // Description. |
||
244 | $description = $line->get_description(); |
||
245 | |||
246 | // Use line item name as fallback for description. |
||
247 | if ( null === $description ) { |
||
248 | /* translators: %s: item index */ |
||
249 | $description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i ++ ); |
||
250 | |||
251 | if ( null !== $line->get_name() && '' !== $line->get_name() ) { |
||
252 | $description = $line->get_name(); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | $item = $line_items->new_item( |
||
257 | $description, |
||
258 | $line->get_quantity(), |
||
259 | $line->get_total_amount()->get_including_tax()->get_minor_units() |
||
260 | ); |
||
261 | |||
262 | $item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() ); |
||
263 | |||
264 | $item->set_id( $line->get_id() ); |
||
265 | |||
266 | // Tax amount. |
||
267 | $tax_amount = $line->get_unit_price()->get_tax_amount(); |
||
268 | |||
269 | if ( null !== $tax_amount ) { |
||
270 | $item->set_tax_amount( $line->get_total_amount()->get_tax_amount()->get_minor_units() ); |
||
271 | $item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 ); |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | // Create payment or payment session. |
||
277 | if ( $request instanceof PaymentRequest ) { |
||
278 | $result = $this->client->create_payment( $request ); |
||
279 | } else { |
||
280 | $result = $this->client->create_payment_session( $request ); |
||
281 | } |
||
282 | |||
283 | // Handle errors. |
||
284 | if ( ! $result ) { |
||
285 | $this->error = $this->client->get_error(); |
||
286 | |||
287 | return; |
||
288 | } |
||
289 | |||
290 | // Set checkout meta for Web SDK redirect. |
||
291 | if ( isset( $result->paymentSession ) ) { |
||
292 | $payment->set_meta( |
||
293 | 'adyen_checkout', |
||
294 | array( |
||
295 | 'sdk_version' => self::SDK_VERSION, |
||
296 | 'payload' => $result->paymentSession, |
||
297 | ) |
||
298 | ); |
||
299 | } |
||
300 | |||
301 | // Set transaction ID. |
||
302 | if ( isset( $result->pspReference ) ) { |
||
303 | $payment->set_transaction_id( $result->pspReference ); |
||
304 | } |
||
305 | |||
306 | // Set action URL. |
||
307 | $action_url = $payment->get_pay_redirect_url(); |
||
308 | |||
309 | if ( isset( $result->redirect->url ) ) { |
||
310 | $action_url = $result->redirect->url; |
||
311 | } |
||
312 | |||
313 | $payment->set_action_url( $action_url ); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Payment redirect. |
||
318 | * |
||
319 | * @param Payment $payment Payment. |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function payment_redirect( Payment $payment ) { |
||
324 | $checkout = $payment->get_meta( 'adyen_checkout' ); |
||
325 | |||
326 | if ( empty( $checkout ) ) { |
||
327 | return; |
||
328 | } |
||
329 | |||
330 | $url = sprintf( |
||
331 | 'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js', |
||
332 | ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
333 | $checkout['sdk_version'] |
||
334 | ); |
||
335 | |||
336 | wp_register_script( |
||
337 | 'pronamic-pay-adyen-checkout', |
||
338 | $url, |
||
339 | array(), |
||
340 | $checkout['sdk_version'], |
||
341 | false |
||
342 | ); |
||
343 | |||
344 | // No cache. |
||
345 | Util::no_cache(); |
||
346 | |||
347 | $context = ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ); |
||
348 | |||
349 | require __DIR__ . '/../views/checkout.php'; |
||
350 | |||
351 | exit; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Update status of the specified payment. |
||
356 | * |
||
357 | * @param Payment $payment Payment. |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | public function update_status( Payment $payment ) { |
||
362 | // Maybe process stored webhook notification. |
||
363 | $this->maybe_handle_notification( $payment ); |
||
364 | |||
365 | // Process payload on return. |
||
366 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
367 | return; |
||
368 | } |
||
369 | |||
370 | $status = null; |
||
371 | |||
372 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
373 | |||
374 | switch ( $payment->get_method() ) { |
||
375 | case PaymentMethods::IDEAL: |
||
376 | case PaymentMethods::SOFORT: |
||
377 | $result = $this->client->get_payment_details( $payload ); |
||
378 | |||
379 | break; |
||
380 | default: |
||
381 | $result = $this->client->get_payment_result( $payload ); |
||
382 | } |
||
383 | |||
384 | if ( $result ) { |
||
385 | $status = ResultCode::transform( $result->resultCode ); |
||
386 | |||
387 | $psp_reference = $result->pspReference; |
||
388 | } |
||
389 | |||
390 | // Handle errors. |
||
391 | if ( empty( $status ) ) { |
||
392 | $payment->set_status( Core_Statuses::FAILURE ); |
||
393 | |||
394 | $this->error = $this->client->get_error(); |
||
395 | |||
396 | return; |
||
397 | } |
||
398 | |||
399 | // Update status. |
||
400 | $payment->set_status( $status ); |
||
401 | |||
402 | // Update transaction ID. |
||
403 | if ( isset( $psp_reference ) ) { |
||
404 | $payment->set_transaction_id( $psp_reference ); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Maybe handle notification. |
||
410 | * |
||
411 | * @param Payment $payment Payment. |
||
412 | */ |
||
413 | public function maybe_handle_notification( Payment $payment ) { |
||
414 | $notification = $payment->get_meta( 'adyen_notification' ); |
||
415 | |||
416 | if ( empty( $notification ) ) { |
||
417 | return; |
||
418 | } |
||
419 | |||
420 | $notification = json_decode( $notification ); |
||
421 | |||
422 | if ( ! is_object( $notification ) ) { |
||
423 | return; |
||
424 | } |
||
425 | |||
426 | switch ( $notification->eventCode ) { |
||
427 | case EventCode::AUTHORIZATION: |
||
428 | $this->handle_authorization_event( $payment, $notification ); |
||
429 | |||
430 | break; |
||
431 | } |
||
432 | |||
433 | $payment->set_meta( 'adyen_notification', null ); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Handle authorization event. |
||
438 | * |
||
439 | * @param Payment $payment Payment. |
||
440 | * @param object $notification Notification. |
||
441 | */ |
||
442 | public function handle_authorization_event( Payment $payment, $notification ) { |
||
443 | if ( ! is_object( $notification ) ) { |
||
444 | return; |
||
445 | } |
||
446 | |||
447 | $success = $notification->success; |
||
448 | |||
449 | if ( 'true' === $success ) { |
||
450 | $status = Core_Statuses::SUCCESS; |
||
451 | } else { |
||
452 | $status = Core_Statuses::FAILURE; |
||
453 | |||
454 | // Add note. |
||
455 | $note = sprintf( |
||
456 | /* translators: %s: failure reason message */ |
||
457 | __( 'Failure reason: %s.', 'pronamic_ideal' ), |
||
458 | esc_html( $notification->reason ) |
||
459 | ); |
||
460 | |||
461 | $payment->add_note( $note ); |
||
462 | } |
||
463 | |||
464 | $payment->set_status( $status ); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Get available payment methods. |
||
469 | * |
||
470 | * @see Core_Gateway::get_available_payment_methods() |
||
471 | */ |
||
472 | public function get_available_payment_methods() { |
||
473 | $payment_methods = array(); |
||
474 | |||
475 | // Get active payment methods for Adyen account. |
||
476 | $methods = $this->client->get_payment_methods(); |
||
477 | |||
478 | if ( ! $methods ) { |
||
479 | $this->error = $this->client->get_error(); |
||
480 | |||
481 | return $payment_methods; |
||
482 | } |
||
483 | |||
484 | // Transform to WordPress payment methods. |
||
485 | foreach ( $methods as $method => $details ) { |
||
486 | $payment_method = PaymentMethodType::transform_gateway_method( $method ); |
||
487 | |||
488 | if ( $payment_method ) { |
||
489 | $payment_methods[] = $payment_method; |
||
490 | } |
||
491 | } |
||
492 | |||
493 | $payment_methods = array_unique( $payment_methods ); |
||
494 | |||
495 | return $payment_methods; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get issuers. |
||
500 | * |
||
501 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
502 | */ |
||
503 | public function get_issuers() { |
||
504 | $groups = array(); |
||
505 | |||
506 | $payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL ); |
||
507 | |||
508 | $result = $this->client->get_issuers( $payment_method ); |
||
509 | |||
510 | if ( ! $result ) { |
||
511 | $this->error = $this->client->get_error(); |
||
512 | |||
513 | return $groups; |
||
514 | } |
||
515 | |||
516 | $groups[] = array( |
||
517 | 'options' => $result, |
||
518 | ); |
||
519 | |||
520 | return $groups; |
||
521 | } |
||
522 | } |
||
523 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.