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 | // Create payment or payment session. |
||
185 | if ( $request instanceof PaymentRequest ) { |
||
186 | $result = $this->client->create_payment( $request ); |
||
187 | } elseif ( $request instanceof PaymentSessionRequest ) { |
||
188 | $result = $this->client->create_payment_session( $request ); |
||
189 | } |
||
190 | |||
191 | // Handle errors. |
||
192 | if ( ! $result ) { |
||
193 | $this->error = $this->client->get_error(); |
||
194 | |||
195 | return; |
||
196 | } |
||
197 | |||
198 | // Load checkout view for payment sessions. |
||
199 | if ( isset( $result->paymentSession ) ) { |
||
200 | wp_register_script( |
||
201 | 'pronamic-pay-adyen-checkout', |
||
202 | 'https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.6.3.min.js', |
||
203 | array(), |
||
204 | '1.6.3', |
||
205 | false |
||
206 | ); |
||
207 | |||
208 | // No cache. |
||
209 | Util::no_cache(); |
||
210 | |||
211 | $payment_session = $result->paymentSession; |
||
212 | |||
213 | $context = ( self::MODE_TEST === $this->config->mode ? 'test' : 'live' ); |
||
214 | |||
215 | require __DIR__ . '/../views/checkout.php'; |
||
216 | |||
217 | exit; |
||
218 | } |
||
219 | |||
220 | // Set transaction ID. |
||
221 | if ( isset( $result->pspReference ) ) { |
||
222 | $payment->set_transaction_id( $result->pspReference ); |
||
223 | } |
||
224 | |||
225 | // Set redirect URL. |
||
226 | if ( isset( $result->redirect->url ) ) { |
||
227 | $payment->set_action_url( $result->redirect->url ); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Update status of the specified payment. |
||
233 | * |
||
234 | * @param Payment $payment Payment. |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | public function update_status( Payment $payment ) { |
||
239 | // Maybe process stored webhook notification. |
||
240 | $this->maybe_handle_notification( $payment ); |
||
241 | |||
242 | // Process payload on return. |
||
243 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
244 | return; |
||
245 | } |
||
246 | |||
247 | $status = null; |
||
248 | |||
249 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
250 | |||
251 | switch ( $payment->get_method() ) { |
||
252 | case PaymentMethods::IDEAL: |
||
253 | case PaymentMethods::SOFORT: |
||
254 | $result = $this->client->get_payment_details( $payload ); |
||
255 | |||
256 | break; |
||
257 | default: |
||
258 | $result = $this->client->get_payment_result( $payload ); |
||
259 | } |
||
260 | |||
261 | if ( $result ) { |
||
262 | $status = ResultCode::transform( $result->resultCode ); |
||
263 | |||
264 | $psp_reference = $result->pspReference; |
||
265 | } |
||
266 | |||
267 | // Handle errors. |
||
268 | if ( empty( $status ) ) { |
||
269 | $payment->set_status( Core_Statuses::FAILURE ); |
||
270 | |||
271 | $this->error = $this->client->get_error(); |
||
272 | |||
273 | return; |
||
274 | } |
||
275 | |||
276 | // Update status. |
||
277 | $payment->set_status( $status ); |
||
278 | |||
279 | // Update transaction ID. |
||
280 | if ( isset( $psp_reference ) ) { |
||
281 | $payment->set_transaction_id( $psp_reference ); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Maybe handle notification. |
||
287 | * |
||
288 | * @param Payment $payment Payment. |
||
289 | */ |
||
290 | public function maybe_handle_notification( Payment $payment ) { |
||
291 | $notification = $payment->get_meta( 'adyen_notification' ); |
||
292 | |||
293 | if ( empty( $notification ) ) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | $notification = json_decode( $notification ); |
||
298 | |||
299 | if ( ! is_object( $notification ) ) { |
||
300 | return; |
||
301 | } |
||
302 | |||
303 | switch ( $notification->eventCode ) { |
||
304 | case EventCode::AUTHORIZATION: |
||
305 | $this->handle_authorization_event( $payment, $notification ); |
||
306 | |||
307 | break; |
||
308 | } |
||
309 | |||
310 | $payment->set_meta( 'adyen_notification', null ); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Handle authorization event. |
||
315 | * |
||
316 | * @param Payment $payment Payment. |
||
317 | * @param object $notification Notification. |
||
318 | */ |
||
319 | public function handle_authorization_event( Payment $payment, $notification ) { |
||
320 | if ( ! is_object( $notification ) ) { |
||
321 | return; |
||
322 | } |
||
323 | |||
324 | $success = $notification->success; |
||
325 | |||
326 | if ( 'true' === $success ) { |
||
327 | $status = Core_Statuses::SUCCESS; |
||
328 | } else { |
||
329 | $status = Core_Statuses::FAILURE; |
||
330 | |||
331 | // Add note. |
||
332 | $note = sprintf( |
||
333 | /* translators: %s: failure reason message */ |
||
334 | __( 'Failure reason: %s.', 'pronamic_ideal' ), |
||
335 | esc_html( $notification->reason ) |
||
336 | ); |
||
337 | |||
338 | $payment->add_note( $note ); |
||
339 | } |
||
340 | |||
341 | $payment->set_status( $status ); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Get available payment methods. |
||
346 | * |
||
347 | * @see Core_Gateway::get_available_payment_methods() |
||
348 | */ |
||
349 | public function get_available_payment_methods() { |
||
350 | $payment_methods = array(); |
||
351 | |||
352 | // Get active payment methods for Adyen account. |
||
353 | $methods = $this->client->get_payment_methods(); |
||
354 | |||
355 | if ( ! $methods ) { |
||
356 | $this->error = $this->client->get_error(); |
||
357 | |||
358 | return $payment_methods; |
||
359 | } |
||
360 | |||
361 | // Transform to WordPress payment methods. |
||
362 | foreach ( $methods as $method => $details ) { |
||
363 | $payment_method = PaymentMethodType::transform_gateway_method( $method ); |
||
364 | |||
365 | if ( $payment_method ) { |
||
366 | $payment_methods[] = $payment_method; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | $payment_methods = array_unique( $payment_methods ); |
||
371 | |||
372 | return $payment_methods; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get issuers. |
||
377 | * |
||
378 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
379 | */ |
||
380 | public function get_issuers() { |
||
381 | $groups = array(); |
||
382 | |||
383 | $payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL ); |
||
384 | |||
385 | $result = $this->client->get_issuers( $payment_method ); |
||
386 | |||
387 | if ( ! $result ) { |
||
388 | $this->error = $this->client->get_error(); |
||
389 | |||
390 | return $groups; |
||
391 | } |
||
392 | |||
393 | $groups[] = array( |
||
394 | 'options' => $result, |
||
395 | ); |
||
396 | |||
397 | return $groups; |
||
398 | } |
||
399 | } |
||
400 |
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.