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 | * Client. |
||
38 | * |
||
39 | * @var Client |
||
40 | */ |
||
41 | protected $client; |
||
42 | |||
43 | /** |
||
44 | * Constructs and initializes an Adyen gateway. |
||
45 | * |
||
46 | * @param Config $config Config. |
||
47 | */ |
||
48 | public function __construct( Config $config ) { |
||
49 | parent::__construct( $config ); |
||
50 | |||
51 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
52 | $this->set_slug( self::SLUG ); |
||
53 | |||
54 | $this->client = new Client( $config->api_key, $config->api_live_url_prefix ); |
||
55 | $this->client->set_merchant_account( $config->merchant_account ); |
||
56 | $this->client->set_mode( $config->mode ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get supported payment methods |
||
61 | * |
||
62 | * @see Core_Gateway::get_supported_payment_methods() |
||
63 | */ |
||
64 | public function get_supported_payment_methods() { |
||
65 | return array( |
||
66 | 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 | * @param Payment $payment Payment. |
||
80 | * |
||
81 | * @see Plugin::start() |
||
82 | */ |
||
83 | public function start( Payment $payment ) { |
||
84 | // Amount. |
||
85 | $amount = new Amount( |
||
86 | $payment->get_total_amount()->get_currency()->get_alphabetic_code(), |
||
87 | $payment->get_total_amount()->get_minor_units() |
||
0 ignored issues
–
show
|
|||
88 | ); |
||
89 | |||
90 | // Payment method. Take leap of faith for unknown payment methods. |
||
91 | $type = PaymentMethodType::transform( |
||
92 | $payment->get_method(), |
||
93 | $payment->get_method() |
||
94 | ); |
||
95 | |||
96 | $payment_method = new PaymentMethod( $type ); |
||
97 | |||
98 | switch ( $payment->get_method() ) { |
||
99 | case PaymentMethods::IDEAL: |
||
100 | $payment_method->issuer = $payment->get_issuer(); |
||
101 | |||
102 | break; |
||
103 | } |
||
104 | |||
105 | // Country. |
||
106 | $locale = get_locale(); |
||
107 | |||
108 | if ( null !== $payment->get_customer() ) { |
||
109 | $locale = $payment->get_customer()->get_locale(); |
||
110 | } |
||
111 | |||
112 | $locale = explode( '_', $locale ); |
||
113 | |||
114 | $country_code = strtoupper( substr( $locale[1], 0, 2 ) ); |
||
115 | |||
116 | // Create payment or payment session request. |
||
117 | switch ( $payment->get_method() ) { |
||
118 | case PaymentMethods::IDEAL: |
||
119 | case PaymentMethods::SOFORT: |
||
120 | // API integration. |
||
121 | $request = new PaymentRequest( |
||
122 | $amount, |
||
123 | $this->config->merchant_account, |
||
124 | $payment->get_id(), |
||
125 | $payment->get_return_url(), |
||
126 | $payment_method |
||
127 | ); |
||
128 | |||
129 | $request->set_country_code( $country_code ); |
||
130 | |||
131 | break; |
||
132 | default: |
||
133 | // Web SDK integration. |
||
134 | $request = new PaymentSessionRequest( |
||
135 | $amount, |
||
136 | $this->config->merchant_account, |
||
137 | $payment->get_id(), |
||
138 | $payment->get_return_url(), |
||
139 | $country_code |
||
140 | ); |
||
141 | |||
142 | $request->set_origin( home_url() ); |
||
143 | $request->set_sdk_version( '1.6.3' ); |
||
144 | |||
145 | // Set allowed payment methods. |
||
146 | $allowed_methods = array( $type ); |
||
147 | |||
148 | // Add all available payment methods if no payment method is given. |
||
149 | if ( empty( $type ) ) { |
||
150 | $allowed_methods = array(); |
||
151 | |||
152 | foreach ( $this->get_available_payment_methods() as $method ) { |
||
153 | $allowed_methods[] = PaymentMethodType::transform( $method ); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $request->set_allowed_payment_methods( $allowed_methods ); |
||
158 | } |
||
159 | |||
160 | // Channel. |
||
161 | $request->set_channel( 'Web' ); |
||
162 | |||
163 | // Shopper. |
||
164 | $request->set_shopper_statement( $payment->get_description() ); |
||
165 | |||
166 | if ( null !== $payment->get_customer() ) { |
||
167 | $request->set_shopper_ip( $payment->get_customer()->get_ip_address() ); |
||
168 | $request->set_shopper_statement( $payment->get_customer()->get_gender() ); |
||
169 | $request->set_shopper_locale( $payment->get_customer()->get_locale() ); |
||
170 | $request->set_shopper_reference( $payment->get_customer()->get_user_id() ); |
||
171 | $request->set_telephone_number( $payment->get_customer()->get_phone() ); |
||
172 | |||
173 | if ( null !== $payment->get_customer()->get_name() ) { |
||
174 | $shopper_name = new ShopperName( |
||
175 | $payment->get_customer()->get_name()->get_first_name(), |
||
176 | $payment->get_customer()->get_name()->get_middle_name(), |
||
177 | $payment->get_customer()->get_name()->get_last_name() |
||
178 | ); |
||
179 | |||
180 | $request->set_shopper_name( $shopper_name ); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // Lines. |
||
185 | $lines = $payment->get_lines(); |
||
186 | |||
187 | if ( null !== $lines ) { |
||
188 | $line_items = $request->new_items(); |
||
189 | |||
190 | $i = 1; |
||
191 | |||
192 | foreach ( $lines as $line ) { |
||
193 | /* translators: %s: item index */ |
||
194 | $name = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i ++ ); |
||
195 | |||
196 | if ( null !== $line->get_name() && '' !== $line->get_name() ) { |
||
197 | $name = $line->get_name(); |
||
198 | } |
||
199 | |||
200 | $item = $line_items->new_item( |
||
201 | DataHelper::shorten( $name, 50 ), |
||
202 | $line->get_quantity(), |
||
203 | // The amount in cents, including VAT, of the item each, see below for more details. |
||
204 | AmountTransformer::transform( $line->get_unit_price() ), |
||
205 | $line->get_type() |
||
206 | ); |
||
207 | |||
208 | $item->set_id( $line->get_id() ); |
||
209 | |||
210 | // Description. |
||
211 | $description = $line->get_description(); |
||
212 | |||
213 | if ( null !== $description ) { |
||
214 | $description = DataHelper::shorten( $description, 100 ); |
||
215 | } |
||
216 | |||
217 | $item->set_description( $description ); |
||
218 | |||
219 | $tax_amount = $line->get_unit_price()->get_tax_amount(); |
||
220 | |||
221 | if ( null !== $tax_amount ) { |
||
222 | // The VAT of the item each, see below for more details. |
||
223 | $item->set_tax( AmountTransformer::transform( $tax_amount ) ); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | // Create payment or payment session. |
||
229 | if ( $request instanceof PaymentRequest ) { |
||
230 | $result = $this->client->create_payment( $request ); |
||
231 | } else { |
||
232 | $result = $this->client->create_payment_session( $request ); |
||
233 | } |
||
234 | |||
235 | // Handle errors. |
||
236 | if ( ! $result ) { |
||
237 | $this->error = $this->client->get_error(); |
||
238 | |||
239 | return; |
||
240 | } |
||
241 | |||
242 | // Load checkout view for payment sessions. |
||
243 | if ( isset( $result->paymentSession ) ) { |
||
244 | wp_register_script( |
||
245 | 'pronamic-pay-adyen-checkout', |
||
246 | 'https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.6.3.min.js', |
||
247 | array(), |
||
248 | '1.6.3', |
||
249 | false |
||
250 | ); |
||
251 | |||
252 | // No cache. |
||
253 | Util::no_cache(); |
||
254 | |||
255 | $payment_session = $result->paymentSession; |
||
256 | |||
257 | $context = ( self::MODE_TEST === $this->config->mode ? 'test' : 'live' ); |
||
258 | |||
259 | require __DIR__ . '/../views/checkout.php'; |
||
260 | |||
261 | exit; |
||
262 | } |
||
263 | |||
264 | // Set transaction ID. |
||
265 | if ( isset( $result->pspReference ) ) { |
||
266 | $payment->set_transaction_id( $result->pspReference ); |
||
267 | } |
||
268 | |||
269 | // Set redirect URL. |
||
270 | if ( isset( $result->redirect->url ) ) { |
||
271 | $payment->set_action_url( $result->redirect->url ); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Update status of the specified payment. |
||
277 | * |
||
278 | * @param Payment $payment Payment. |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | public function update_status( Payment $payment ) { |
||
283 | // Maybe process stored webhook notification. |
||
284 | $this->maybe_handle_notification( $payment ); |
||
285 | |||
286 | // Process payload on return. |
||
287 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | $status = null; |
||
292 | |||
293 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
294 | |||
295 | switch ( $payment->get_method() ) { |
||
296 | case PaymentMethods::IDEAL: |
||
297 | case PaymentMethods::SOFORT: |
||
298 | $result = $this->client->get_payment_details( $payload ); |
||
299 | |||
300 | break; |
||
301 | default: |
||
302 | $result = $this->client->get_payment_result( $payload ); |
||
303 | } |
||
304 | |||
305 | if ( $result ) { |
||
306 | $status = ResultCode::transform( $result->resultCode ); |
||
307 | |||
308 | $psp_reference = $result->pspReference; |
||
309 | } |
||
310 | |||
311 | // Handle errors. |
||
312 | if ( empty( $status ) ) { |
||
313 | $payment->set_status( Core_Statuses::FAILURE ); |
||
314 | |||
315 | $this->error = $this->client->get_error(); |
||
316 | |||
317 | return; |
||
318 | } |
||
319 | |||
320 | // Update status. |
||
321 | $payment->set_status( $status ); |
||
322 | |||
323 | // Update transaction ID. |
||
324 | if ( isset( $psp_reference ) ) { |
||
325 | $payment->set_transaction_id( $psp_reference ); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Maybe handle notification. |
||
331 | * |
||
332 | * @param Payment $payment Payment. |
||
333 | */ |
||
334 | public function maybe_handle_notification( Payment $payment ) { |
||
335 | $notification = $payment->get_meta( 'adyen_notification' ); |
||
336 | |||
337 | if ( empty( $notification ) ) { |
||
338 | return; |
||
339 | } |
||
340 | |||
341 | $notification = json_decode( $notification ); |
||
342 | |||
343 | if ( ! is_object( $notification ) ) { |
||
344 | return; |
||
345 | } |
||
346 | |||
347 | switch ( $notification->eventCode ) { |
||
348 | case EventCode::AUTHORIZATION: |
||
349 | $this->handle_authorization_event( $payment, $notification ); |
||
350 | |||
351 | break; |
||
352 | } |
||
353 | |||
354 | $payment->set_meta( 'adyen_notification', null ); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Handle authorization event. |
||
359 | * |
||
360 | * @param Payment $payment Payment. |
||
361 | * @param object $notification Notification. |
||
362 | */ |
||
363 | public function handle_authorization_event( Payment $payment, $notification ) { |
||
364 | if ( ! is_object( $notification ) ) { |
||
365 | return; |
||
366 | } |
||
367 | |||
368 | $success = $notification->success; |
||
369 | |||
370 | if ( 'true' === $success ) { |
||
371 | $status = Core_Statuses::SUCCESS; |
||
372 | } else { |
||
373 | $status = Core_Statuses::FAILURE; |
||
374 | |||
375 | // Add note. |
||
376 | $note = sprintf( |
||
377 | /* translators: %s: failure reason message */ |
||
378 | __( 'Failure reason: %s.', 'pronamic_ideal' ), |
||
379 | esc_html( $notification->reason ) |
||
380 | ); |
||
381 | |||
382 | $payment->add_note( $note ); |
||
383 | } |
||
384 | |||
385 | $payment->set_status( $status ); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get available payment methods. |
||
390 | * |
||
391 | * @see Core_Gateway::get_available_payment_methods() |
||
392 | */ |
||
393 | public function get_available_payment_methods() { |
||
394 | $payment_methods = array(); |
||
395 | |||
396 | // Get active payment methods for Adyen account. |
||
397 | $methods = $this->client->get_payment_methods(); |
||
398 | |||
399 | if ( ! $methods ) { |
||
400 | $this->error = $this->client->get_error(); |
||
401 | |||
402 | return $payment_methods; |
||
403 | } |
||
404 | |||
405 | // Transform to WordPress payment methods. |
||
406 | foreach ( $methods as $method => $details ) { |
||
407 | $payment_method = PaymentMethodType::transform_gateway_method( $method ); |
||
408 | |||
409 | if ( $payment_method ) { |
||
410 | $payment_methods[] = $payment_method; |
||
411 | } |
||
412 | } |
||
413 | |||
414 | $payment_methods = array_unique( $payment_methods ); |
||
415 | |||
416 | return $payment_methods; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Get issuers. |
||
421 | * |
||
422 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
423 | */ |
||
424 | public function get_issuers() { |
||
425 | $groups = array(); |
||
426 | |||
427 | $payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL ); |
||
428 | |||
429 | $result = $this->client->get_issuers( $payment_method ); |
||
430 | |||
431 | if ( ! $result ) { |
||
432 | $this->error = $this->client->get_error(); |
||
433 | |||
434 | return $groups; |
||
435 | } |
||
436 | |||
437 | $groups[] = array( |
||
438 | 'options' => $result, |
||
439 | ); |
||
440 | |||
441 | return $groups; |
||
442 | } |
||
443 | } |
||
444 |
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.