1 | <?php |
||||||
2 | /** |
||||||
3 | * Payments controller |
||||||
4 | * |
||||||
5 | * @author Pronamic <[email protected]> |
||||||
6 | * @copyright 2005-2020 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 JsonSchema\Exception\ValidationException; |
||||||
14 | use Pronamic\WordPress\Pay\Core\Server; |
||||||
15 | use Pronamic\WordPress\Pay\Plugin; |
||||||
16 | use WP_REST_Request; |
||||||
17 | |||||||
18 | /** |
||||||
19 | * Payments result controller |
||||||
20 | * |
||||||
21 | * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete |
||||||
22 | * |
||||||
23 | * @author Reüel van der Steege |
||||||
24 | * @version 1.1.0 |
||||||
25 | * @since 1.1.0 |
||||||
26 | */ |
||||||
27 | class PaymentsController { |
||||||
28 | /** |
||||||
29 | * Setup. |
||||||
30 | * |
||||||
31 | * @return void |
||||||
32 | 1 | */ |
|||||
33 | 1 | public function setup() { |
|||||
34 | 1 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|||||
35 | } |
||||||
36 | |||||||
37 | /** |
||||||
38 | * REST API init. |
||||||
39 | * |
||||||
40 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||||||
41 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||||||
42 | * |
||||||
43 | * @return void |
||||||
44 | 10 | */ |
|||||
45 | public function rest_api_init() { |
||||||
46 | 10 | // Register REST route `/payments//{payment_id}`. |
|||||
47 | 10 | register_rest_route( |
|||||
48 | 10 | Integration::REST_ROUTE_NAMESPACE, |
|||||
49 | '/payments/(?P<payment_id>\d+)', |
||||||
50 | 10 | array( |
|||||
51 | 10 | 'methods' => 'POST', |
|||||
52 | 'callback' => array( $this, 'rest_api_adyen_payments' ), |
||||||
53 | 'args' => array( |
||||||
54 | 10 | 'payment_id' => array( |
|||||
55 | 10 | 'description' => __( 'Payment ID.', 'pronamic_ideal' ), |
|||||
56 | 'type' => 'integer', |
||||||
57 | ), |
||||||
58 | ), |
||||||
59 | ) |
||||||
60 | ); |
||||||
61 | |||||||
62 | 10 | // Register REST route `/payments/details/{payment_id}`. |
|||||
63 | 10 | register_rest_route( |
|||||
64 | 10 | Integration::REST_ROUTE_NAMESPACE, |
|||||
65 | '/payments/details/(?P<payment_id>\d+)', |
||||||
66 | 10 | array( |
|||||
67 | 10 | 'methods' => 'POST', |
|||||
68 | 'callback' => array( $this, 'rest_api_adyen_payment_details' ), |
||||||
69 | 'args' => array( |
||||||
70 | 10 | 'payment_id' => array( |
|||||
71 | 10 | 'description' => __( 'Payment ID.', 'pronamic_ideal' ), |
|||||
72 | 'type' => 'integer', |
||||||
73 | ), |
||||||
74 | ), |
||||||
75 | ) |
||||||
76 | 10 | ); |
|||||
77 | |||||||
78 | // Register REST route `/payments/applepay/merchant-validation/`. |
||||||
79 | register_rest_route( |
||||||
80 | Integration::REST_ROUTE_NAMESPACE, |
||||||
81 | '/payments/applepay/merchant-validation/(?P<payment_id>\d+)', |
||||||
82 | array( |
||||||
83 | 'methods' => 'POST', |
||||||
84 | 'callback' => array( $this, 'rest_api_applepay_merchant_validation' ), |
||||||
85 | 'args' => array( |
||||||
86 | 'payment_id' => array( |
||||||
87 | 'description' => __( 'Payment ID.', 'pronamic_ideal' ), |
||||||
88 | 'type' => 'integer', |
||||||
89 | ), |
||||||
90 | ), |
||||||
91 | ) |
||||||
92 | ); |
||||||
93 | } |
||||||
94 | |||||||
95 | /** |
||||||
96 | * REST API Adyen payments handler. |
||||||
97 | * |
||||||
98 | * @param WP_REST_Request $request Request. |
||||||
99 | * @return object |
||||||
100 | * @throws \Exception Throws exception on Adyen service exception response. |
||||||
101 | */ |
||||||
102 | public function rest_api_adyen_payments( WP_REST_Request $request ) { |
||||||
103 | $payment_id = $request->get_param( 'payment_id' ); |
||||||
104 | |||||||
105 | // Payment ID. |
||||||
106 | if ( null === $payment_id ) { |
||||||
107 | return new \WP_Error( |
||||||
108 | 'pronamic-pay-adyen-no-payment-id', |
||||||
109 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||||||
110 | ); |
||||||
111 | } |
||||||
112 | |||||||
113 | $payment = \get_pronamic_payment( $payment_id ); |
||||||
114 | |||||||
115 | if ( null === $payment ) { |
||||||
116 | return new \WP_Error( |
||||||
117 | 'pronamic-pay-adyen-payment-not-found', |
||||||
118 | sprintf( |
||||||
119 | /* translators: %s: payment ID */ |
||||||
120 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||||||
121 | $payment_id |
||||||
122 | ), |
||||||
123 | $payment_id |
||||||
124 | ); |
||||||
125 | } |
||||||
126 | |||||||
127 | // State data. |
||||||
128 | $data = \json_decode( $request->get_body() ); |
||||||
129 | |||||||
130 | if ( null === $data ) { |
||||||
131 | return new \WP_Error( |
||||||
132 | 'pronamic-pay-adyen-no-data', |
||||||
133 | __( 'No data given in request body.', 'pronamic_ideal' ) |
||||||
134 | ); |
||||||
135 | } |
||||||
136 | |||||||
137 | // Gateway. |
||||||
138 | $config_id = $payment->get_config_id(); |
||||||
139 | |||||||
140 | if ( null === $config_id ) { |
||||||
141 | return new \WP_Error( |
||||||
142 | 'pronamic-pay-adyen-no-config', |
||||||
143 | __( 'No gateway configuration ID given in payment.', 'pronamic_ideal' ) |
||||||
144 | ); |
||||||
145 | } |
||||||
146 | |||||||
147 | $gateway = Plugin::get_gateway( $config_id ); |
||||||
148 | |||||||
149 | if ( empty( $gateway ) ) { |
||||||
150 | return new \WP_Error( |
||||||
151 | 'pronamic-pay-adyen-gateway-not-found', |
||||||
152 | sprintf( |
||||||
153 | /* translators: %s: Gateway configuration ID */ |
||||||
154 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
155 | $config_id |
||||||
156 | ), |
||||||
157 | $config_id |
||||||
158 | ); |
||||||
159 | } |
||||||
160 | |||||||
161 | if ( ! isset( $gateway->client ) ) { |
||||||
162 | return new \WP_Error( |
||||||
163 | 'pronamic-pay-adyen-client-not-found', |
||||||
164 | sprintf( |
||||||
165 | /* translators: %s: Gateway configuration ID */ |
||||||
166 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
167 | $config_id |
||||||
168 | ), |
||||||
169 | $config_id |
||||||
170 | ); |
||||||
171 | } |
||||||
172 | |||||||
173 | // Create payment. |
||||||
174 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
||||||
175 | if ( ! isset( $data->paymentMethod->type ) ) { |
||||||
176 | return new \WP_Error( |
||||||
177 | 'pronamic-pay-adyen-no-payment-method', |
||||||
178 | __( 'No payment method given.', 'pronamic_ideal' ) |
||||||
179 | ); |
||||||
180 | } |
||||||
181 | |||||||
182 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
||||||
183 | $payment_method = PaymentMethod::from_object( $data->paymentMethod ); |
||||||
184 | |||||||
185 | try { |
||||||
186 | if ( ! \is_callable( array( $gateway, 'create_payment' ) ) ) { |
||||||
187 | return (object) array( |
||||||
188 | 'error' => __( 'Gateway does not support method to create payment.', 'pronamic_ideal' ), |
||||||
189 | ); |
||||||
190 | } |
||||||
191 | |||||||
192 | try { |
||||||
193 | $response = $gateway->create_payment( $payment, $payment_method ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
194 | } catch ( \Pronamic\WordPress\Pay\Gateways\Adyen\ServiceException $service_exception ) { |
||||||
195 | $message = $service_exception->getMessage(); |
||||||
196 | |||||||
197 | $error_code = $service_exception->get_error_code(); |
||||||
198 | |||||||
199 | if ( ! empty( $error_code ) ) { |
||||||
200 | $message = sprintf( |
||||||
201 | /* translators: 1: error message, 2: error code */ |
||||||
202 | __( '%1$s (error %2$s)', 'pronamic_ideal' ), |
||||||
203 | $service_exception->getMessage(), |
||||||
204 | $error_code |
||||||
205 | ); |
||||||
206 | } |
||||||
207 | |||||||
208 | throw new \Exception( $message ); |
||||||
209 | } |
||||||
210 | } catch ( \Exception $e ) { |
||||||
211 | $error = $e->getMessage(); |
||||||
212 | |||||||
213 | $error_code = $e->getCode(); |
||||||
214 | |||||||
215 | if ( ! empty( $error_code ) ) { |
||||||
216 | $error = sprintf( '%s - %s', $error_code, $e->getMessage() ); |
||||||
217 | } |
||||||
218 | |||||||
219 | return (object) array( 'error' => $error ); |
||||||
220 | } |
||||||
221 | |||||||
222 | // Update payment status based on response. |
||||||
223 | PaymentResponseHelper::update_payment( $payment, $response ); |
||||||
224 | |||||||
225 | $result = array( |
||||||
226 | 'resultCode' => $response->get_result_code(), |
||||||
227 | ); |
||||||
228 | |||||||
229 | // Return action if available. |
||||||
230 | $action = $response->get_action(); |
||||||
231 | |||||||
232 | if ( null !== $action ) { |
||||||
233 | $result['action'] = $action->get_json(); |
||||||
234 | } |
||||||
235 | |||||||
236 | // Return refusal reason if available. |
||||||
237 | $refusal_reason = $response->get_refusal_reason(); |
||||||
238 | |||||||
239 | if ( null !== $refusal_reason ) { |
||||||
240 | $result['refusalReason'] = $refusal_reason; |
||||||
241 | } |
||||||
242 | |||||||
243 | return (object) $result; |
||||||
244 | } |
||||||
245 | |||||||
246 | /** |
||||||
247 | * REST API Adyen payment details handler. |
||||||
248 | * |
||||||
249 | * @param WP_REST_Request $request Request. |
||||||
250 | * @return object |
||||||
251 | * @throws \Exception Throws exception on Adyen service exception response. |
||||||
252 | */ |
||||||
253 | public function rest_api_adyen_payment_details( WP_REST_Request $request ) { |
||||||
254 | $payment_id = $request->get_param( 'payment_id' ); |
||||||
255 | |||||||
256 | // Payment ID. |
||||||
257 | if ( null === $payment_id ) { |
||||||
258 | return new \WP_Error( |
||||||
259 | 'pronamic-pay-adyen-no-payment-id', |
||||||
260 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||||||
261 | ); |
||||||
262 | } |
||||||
263 | |||||||
264 | $payment = \get_pronamic_payment( $payment_id ); |
||||||
265 | |||||||
266 | if ( null === $payment ) { |
||||||
267 | return new \WP_Error( |
||||||
268 | 'pronamic-pay-adyen-payment-not-found', |
||||||
269 | sprintf( |
||||||
270 | /* translators: %s: payment ID */ |
||||||
271 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||||||
272 | $payment_id |
||||||
273 | ), |
||||||
274 | $payment_id |
||||||
275 | ); |
||||||
276 | } |
||||||
277 | |||||||
278 | // State data. |
||||||
279 | $data = \json_decode( $request->get_body() ); |
||||||
280 | |||||||
281 | if ( null === $data ) { |
||||||
282 | return new \WP_Error( |
||||||
283 | 'pronamic-pay-adyen-no-data', |
||||||
284 | __( 'No data given in request body.', 'pronamic_ideal' ) |
||||||
285 | ); |
||||||
286 | } |
||||||
287 | |||||||
288 | // Gateway. |
||||||
289 | $config_id = $payment->get_config_id(); |
||||||
290 | |||||||
291 | if ( null === $config_id ) { |
||||||
292 | return new \WP_Error( |
||||||
293 | 'pronamic-pay-adyen-no-config', |
||||||
294 | __( 'No gateway configuration ID given in payment.', 'pronamic_ideal' ) |
||||||
295 | ); |
||||||
296 | } |
||||||
297 | |||||||
298 | $gateway = Plugin::get_gateway( $config_id ); |
||||||
299 | |||||||
300 | if ( empty( $gateway ) ) { |
||||||
301 | return new \WP_Error( |
||||||
302 | 'pronamic-pay-adyen-gateway-not-found', |
||||||
303 | sprintf( |
||||||
304 | /* translators: %s: Gateway configuration ID */ |
||||||
305 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
306 | $config_id |
||||||
307 | ), |
||||||
308 | $config_id |
||||||
309 | ); |
||||||
310 | } |
||||||
311 | |||||||
312 | if ( ! isset( $gateway->client ) ) { |
||||||
313 | return new \WP_Error( |
||||||
314 | 'pronamic-pay-adyen-client-not-found', |
||||||
315 | sprintf( |
||||||
316 | /* translators: %s: Gateway configuration ID */ |
||||||
317 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
318 | $config_id |
||||||
319 | ), |
||||||
320 | $config_id |
||||||
321 | ); |
||||||
322 | } |
||||||
323 | |||||||
324 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
||||||
325 | if ( ! isset( $data->paymentMethod->type ) ) { |
||||||
326 | return new \WP_Error( |
||||||
327 | 'pronamic-pay-adyen-no-payment-method', |
||||||
328 | __( 'No payment method given.', 'pronamic_ideal' ) |
||||||
329 | ); |
||||||
330 | } |
||||||
331 | |||||||
332 | // Send additional payment details. |
||||||
333 | $payment_details_request = new PaymentDetailsRequest(); |
||||||
334 | |||||||
335 | // Set payment data from original payment response. |
||||||
336 | $payment_response = $payment->get_meta( 'adyen_payment_response' ); |
||||||
337 | |||||||
338 | if ( is_string( $payment_response ) && '' !== $payment_response ) { |
||||||
339 | $payment_response = \json_decode( $payment_response ); |
||||||
340 | |||||||
341 | $payment_response = PaymentResponse::from_object( $payment_response ); |
||||||
342 | |||||||
343 | $payment_data = $payment_response->get_payment_data(); |
||||||
344 | |||||||
345 | $payment_details_request->set_payment_data( $payment_data ); |
||||||
346 | } |
||||||
347 | |||||||
348 | try { |
||||||
349 | if ( ! \is_callable( array( $gateway, 'send_payment_details' ) ) ) { |
||||||
350 | return (object) array( |
||||||
351 | 'error' => __( 'Gateway does not support sending additional payment details.', 'pronamic_ideal' ), |
||||||
352 | ); |
||||||
353 | } |
||||||
354 | |||||||
355 | try { |
||||||
356 | $response = $gateway->send_payment_details( $payment_details_request ); |
||||||
0 ignored issues
–
show
The method
send_payment_details() does not exist on Pronamic\WordPress\Pay\Core\Gateway . It seems like you code against a sub-type of Pronamic\WordPress\Pay\Core\Gateway such as Pronamic\WordPress\Pay\G...ays\Adyen\DropInGateway .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
357 | } catch ( \Pronamic\WordPress\Pay\Gateways\Adyen\ServiceException $service_exception ) { |
||||||
358 | $message = $service_exception->getMessage(); |
||||||
359 | |||||||
360 | $error_code = $service_exception->get_error_code(); |
||||||
361 | |||||||
362 | if ( ! empty( $error_code ) ) { |
||||||
363 | $message = sprintf( |
||||||
364 | /* translators: 1: error message, 2: error code */ |
||||||
365 | __( '%1$s (error %2$s)', 'pronamic_ideal' ), |
||||||
366 | $service_exception->getMessage(), |
||||||
367 | $error_code |
||||||
368 | ); |
||||||
369 | } |
||||||
370 | |||||||
371 | throw new \Exception( $message ); |
||||||
372 | } |
||||||
373 | |||||||
374 | // Update payment status based on response. |
||||||
375 | PaymentResponseHelper::update_payment( $payment, $response ); |
||||||
376 | } catch ( \Exception $e ) { |
||||||
377 | $error = $e->getMessage(); |
||||||
378 | |||||||
379 | $error_code = $e->getCode(); |
||||||
380 | |||||||
381 | if ( ! empty( $error_code ) ) { |
||||||
382 | $error = sprintf( '%s - %s', $error_code, $e->getMessage() ); |
||||||
383 | } |
||||||
384 | |||||||
385 | return (object) array( 'error' => $error ); |
||||||
386 | } |
||||||
387 | |||||||
388 | $result = array( |
||||||
389 | 'resultCode' => $response->get_result_code(), |
||||||
390 | ); |
||||||
391 | |||||||
392 | // Return action if available. |
||||||
393 | $action = $response->get_action(); |
||||||
394 | |||||||
395 | if ( null !== $action ) { |
||||||
396 | $result['action'] = $action->get_json(); |
||||||
397 | } |
||||||
398 | |||||||
399 | // Return refusal reason if available. |
||||||
400 | $refusal_reason = $response->get_refusal_reason(); |
||||||
401 | |||||||
402 | if ( null !== $refusal_reason ) { |
||||||
403 | $result['refusalReason'] = $refusal_reason; |
||||||
404 | } |
||||||
405 | |||||||
406 | return (object) $result; |
||||||
407 | } |
||||||
408 | |||||||
409 | /** |
||||||
410 | * REST API Apple Pay merchant validation handler. |
||||||
411 | * |
||||||
412 | * @param WP_REST_Request $request Request. |
||||||
413 | * @return object |
||||||
414 | */ |
||||||
415 | public function rest_api_applepay_merchant_validation( WP_REST_Request $request ) { |
||||||
416 | $payment_id = $request->get_param( 'payment_id' ); |
||||||
417 | |||||||
418 | // Payment ID. |
||||||
419 | if ( null === $payment_id ) { |
||||||
420 | return new \WP_Error( |
||||||
421 | 'pronamic-pay-adyen-no-payment-id', |
||||||
422 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||||||
423 | ); |
||||||
424 | } |
||||||
425 | |||||||
426 | $payment = \get_pronamic_payment( $payment_id ); |
||||||
427 | |||||||
428 | if ( null === $payment ) { |
||||||
429 | return new \WP_Error( |
||||||
430 | 'pronamic-pay-adyen-payment-not-found', |
||||||
431 | sprintf( |
||||||
432 | /* translators: %s: payment ID */ |
||||||
433 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||||||
434 | $payment_id |
||||||
435 | ), |
||||||
436 | $payment_id |
||||||
437 | ); |
||||||
438 | } |
||||||
439 | |||||||
440 | // State data. |
||||||
441 | $data = \json_decode( $request->get_body() ); |
||||||
442 | |||||||
443 | if ( null === $data ) { |
||||||
444 | return new \WP_Error( |
||||||
445 | 'pronamic-pay-adyen-no-data', |
||||||
446 | __( 'No data given in request body.', 'pronamic_ideal' ) |
||||||
447 | ); |
||||||
448 | } |
||||||
449 | |||||||
450 | // Gateway. |
||||||
451 | $config_id = $payment->get_config_id(); |
||||||
452 | |||||||
453 | if ( null === $config_id ) { |
||||||
454 | return new \WP_Error( |
||||||
455 | 'pronamic-pay-adyen-no-config', |
||||||
456 | __( 'No gateway configuration ID given in payment.', 'pronamic_ideal' ) |
||||||
457 | ); |
||||||
458 | } |
||||||
459 | |||||||
460 | $gateway = Plugin::get_gateway( $config_id ); |
||||||
461 | |||||||
462 | if ( empty( $gateway ) ) { |
||||||
463 | return new \WP_Error( |
||||||
464 | 'pronamic-pay-adyen-gateway-not-found', |
||||||
465 | sprintf( |
||||||
466 | /* translators: %s: Gateway configuration ID */ |
||||||
467 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
468 | $config_id |
||||||
469 | ), |
||||||
470 | $config_id |
||||||
471 | ); |
||||||
472 | } |
||||||
473 | |||||||
474 | if ( ! isset( $gateway->client ) ) { |
||||||
475 | return new \WP_Error( |
||||||
476 | 'pronamic-pay-adyen-client-not-found', |
||||||
477 | sprintf( |
||||||
478 | /* translators: %s: Gateway configuration ID */ |
||||||
479 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||||||
480 | $config_id |
||||||
481 | ), |
||||||
482 | $config_id |
||||||
483 | ); |
||||||
484 | } |
||||||
485 | |||||||
486 | // Merchant identifier. |
||||||
487 | $integration = new Integration(); |
||||||
488 | |||||||
489 | $config = $integration->get_config( $config_id ); |
||||||
490 | |||||||
491 | $merchant_identifier = $config->get_apple_pay_merchant_id(); |
||||||
492 | |||||||
493 | if ( empty( $merchant_identifier ) ) { |
||||||
494 | return new \WP_Error( |
||||||
495 | 'pronamic-pay-adyen-applepay-no-merchant-identifier', |
||||||
496 | __( 'Apple Pay merchant identifier not configured in gateway settings.', 'pronamic_ideal' ) |
||||||
497 | ); |
||||||
498 | } |
||||||
499 | |||||||
500 | if ( ! isset( $data->validation_url ) ) { |
||||||
501 | return new \WP_Error( |
||||||
502 | 'pronamic-pay-adyen-applepay-no-validation-url', |
||||||
503 | __( 'No Apple Pay merchant validation URL given.', 'pronamic_ideal' ) |
||||||
504 | ); |
||||||
505 | } |
||||||
506 | |||||||
507 | /* |
||||||
508 | * Request an Apple Pay payment session. |
||||||
509 | * |
||||||
510 | * @link https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778021-onvalidatemerchant |
||||||
511 | * @link https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session |
||||||
512 | * @link https://docs.adyen.com/payment-methods/apple-pay/web-drop-in#show-apple-pay-in-your-payment-form |
||||||
513 | */ |
||||||
514 | $request = array( |
||||||
515 | 'merchantIdentifier' => $merchant_identifier, |
||||||
516 | 'displayName' => \get_bloginfo( 'name' ), |
||||||
517 | 'initiative' => 'web', |
||||||
518 | 'initiativeContext' => Server::get( 'HTTP_HOST', FILTER_SANITIZE_STRING ), |
||||||
519 | ); |
||||||
520 | |||||||
521 | try { |
||||||
522 | add_action( 'http_api_curl', array( $this, 'http_curl_applepay_merchant_validation' ), 10, 3 ); |
||||||
523 | |||||||
524 | $response = \wp_remote_request( |
||||||
525 | $data->validation_url, |
||||||
526 | array( |
||||||
527 | 'method' => 'POST', |
||||||
528 | 'headers' => array( |
||||||
529 | 'Content-Type' => 'application/json', |
||||||
530 | ), |
||||||
531 | 'body' => \wp_json_encode( (object) $request ), |
||||||
532 | 'adyen_applepay_key_password' => $config->get_apple_pay_merchant_id_private_key_password(), |
||||||
533 | ) |
||||||
534 | ); |
||||||
535 | |||||||
536 | $body = \wp_remote_retrieve_body( $response ); |
||||||
537 | |||||||
538 | $result = \json_decode( $body ); |
||||||
539 | } catch ( \Exception $e ) { |
||||||
540 | $error = $e->getMessage(); |
||||||
541 | |||||||
542 | $error_code = $e->getCode(); |
||||||
543 | |||||||
544 | if ( ! empty( $error_code ) ) { |
||||||
545 | $error = sprintf( '%s - %s', $error_code, $e->getMessage() ); |
||||||
546 | } |
||||||
547 | |||||||
548 | return (object) array( 'error' => $error ); |
||||||
549 | } |
||||||
550 | |||||||
551 | return (object) $result; |
||||||
552 | } |
||||||
553 | |||||||
554 | /** |
||||||
555 | * HTTP CURL options for Apple Pay merchant validation. |
||||||
556 | * |
||||||
557 | * @param resource $handle CURL handle. |
||||||
558 | * @param array $parsed_args Parsed arguments. |
||||||
559 | * @param string $url Request URL. |
||||||
560 | * @return void |
||||||
561 | */ |
||||||
562 | public function http_curl_applepay_merchant_validation( $handle, $parsed_args, $url ) { |
||||||
563 | if ( ! isset( $parsed_args['adyen_applepay_key_password'] ) ) { |
||||||
564 | return; |
||||||
565 | } |
||||||
566 | |||||||
567 | // @todo Upload Apple Pay Merchant Identity certificate and private key through gateway settings. |
||||||
568 | $certificate_path = \ABSPATH . 'applepay-merchant-identity-cert.pem'; |
||||||
569 | $private_key_path = \ABSPATH . 'applepay-merchant-identity-key.pem'; |
||||||
570 | $password = $parsed_args['adyen_applepay_key_password']; |
||||||
571 | |||||||
572 | // Set merchant identity certificate path. |
||||||
573 | if ( \file_exists( $certificate_path ) ) { |
||||||
574 | \curl_setopt( $handle, CURLOPT_SSLCERT, $certificate_path ); |
||||||
575 | } |
||||||
576 | |||||||
577 | // Set merchant identity private key path. |
||||||
578 | if ( \file_exists( $private_key_path ) ) { |
||||||
579 | \curl_setopt( $handle, CURLOPT_SSLKEY, $private_key_path ); |
||||||
580 | } |
||||||
581 | |||||||
582 | // Set merchant identity private key password. |
||||||
583 | if ( ! empty( $password ) ) { |
||||||
584 | \curl_setopt( $handle, CURLOPT_SSLKEYPASSWD, $password ); |
||||||
585 | } |
||||||
586 | } |
||||||
587 | } |
||||||
588 |