PluginProbe
PayPlug for WooCommerce (Official) / 3.0.0
PayPlug for WooCommerce (Official) v3.0.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Admin / Ajax.php

Ajax.php in PayPlug for WooCommerce (Official) 3.0.0, at src/Admin/Ajax.php

705 lines 27.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Admin;
4
5 // Exit if accessed directly
6 use Payplug\Authentication;
7 use Payplug\Exception\HttpException;
8 use Payplug\Exception\PayplugException;
9 use Payplug\Payplug;
10 use Payplug\PayplugWoocommerce\Controller\ApplePay;
11 use Payplug\PayplugWoocommerce\Gateway\AmericanExpress;
12 use Payplug\PayplugWoocommerce\Gateway\Bancontact;
13 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
14 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
15 use Payplug\PayplugWoocommerce\Gateway\PPRO\Bizum;
16 use Payplug\PayplugWoocommerce\Gateway\PPRO\Ideal;
17 use Payplug\PayplugWoocommerce\Gateway\PPRO\Mybank;
18 use Payplug\PayplugWoocommerce\Gateway\PPRO\Satispay;
19 use Payplug\PayplugWoocommerce\Gateway\PPRO\Scalapay;
20 use Payplug\PayplugWoocommerce\Gateway\PPRO\Wero;
21 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
22 use Payplug\PayplugWoocommerce\Traits\GatewayGetter;
23 use WP_REST_Request;
24
25 if (!defined('ABSPATH')) {
26 exit;
27 }
28
29 /**
30 * PayPlug admin ajax handler.
31 */
32 class Ajax
33 {
34 use GatewayGetter;
35
36 public function __construct()
37 {
38 $permission = (current_user_can('editor') || current_user_can('administrator'));
39
40 add_action('rest_api_init', function () use ($permission): void {
41 //Path to REST route and the callback function
42 register_rest_route('payplug_api', '/save/', [
43 'methods' => ['GET', 'POST'],
44 'callback' => [$this, 'save_action'],
45 'permission_callback' => fn () => $permission,
46 'show_in_index' => false,
47 ]);
48 register_rest_route('payplug_api', '/init/', [
49 'methods' => ['GET', 'POST'],
50 'callback' => [$this, 'payplug_init'],
51 'permission_callback' => fn () => $permission,
52 'show_in_index' => false,
53 ]);
54 register_rest_route('payplug_api', '/login/', [
55 'methods' => ['GET', 'POST'],
56 'callback' => [$this, 'login_action'],
57 'permission_callback' => fn () => $permission,
58 'show_in_index' => false,
59 ]);
60 register_rest_route('payplug_api', '/logout/', [
61 'methods' => ['GET', 'POST'],
62 'callback' => [$this, 'payplug_logout'],
63 'permission_callback' => fn () => $permission,
64 'show_in_index' => false,
65 ]);
66 register_rest_route('payplug_api', '/refresh_keys/', [
67 'methods' => ['GET', 'POST'],
68 'callback' => [$this, 'refresh_keys'],
69 'permission_callback' => fn () => $permission,
70 'show_in_index' => false,
71 ]);
72 register_rest_route('payplug_api', '/check_requirements/', [
73 'methods' => ['GET', 'POST'],
74 'callback' => [$this, 'payplug_check_requirements'],
75 'permission_callback' => fn () => $permission,
76 'show_in_index' => false,
77 ]);
78 register_rest_route('payplug_api', '/bancontact_permissions/', [
79 'methods' => ['GET', 'POST'],
80 'callback' => [$this, 'api_check_bancontact_permissions'],
81 'permission_callback' => fn () => $permission,
82 'show_in_index' => false,
83 ]);
84 register_rest_route('payplug_api', '/applepay_permissions/', [
85 'methods' => ['GET', 'POST'],
86 'callback' => [$this, 'api_check_applepay_permissions'],
87 'permission_callback' => fn () => $permission,
88 'show_in_index' => false,
89 ]);
90 register_rest_route('payplug_api', '/american_express_permissions/', [
91 'methods' => ['GET', 'POST'],
92 'callback' => [$this, 'api_check_american_express_permissions'],
93 'permission_callback' => fn () => $permission,
94 'show_in_index' => false,
95 ]);
96 register_rest_route('payplug_api', '/oney_permissions/', [
97 'methods' => ['GET', 'POST'],
98 'callback' => [$this, 'api_check_oney_permissions'],
99 'permission_callback' => fn () => $permission,
100 'show_in_index' => false,
101 ]);
102 register_rest_route('payplug_api', '/one_click_permissions/', [
103 'methods' => ['GET', 'POST'],
104 'callback' => [$this, 'api_check_one_click_permission'],
105 'permission_callback' => fn () => $permission,
106 'show_in_index' => false,
107 ]);
108 register_rest_route('payplug_api', '/satispay_permissions/', [
109 'methods' => ['GET', 'POST'],
110 'callback' => [$this, 'api_check_satispay_permissions'],
111 'permission_callback' => fn () => $permission,
112 'show_in_index' => false,
113 ]);
114 register_rest_route('payplug_api', '/mybank_permissions/', [
115 'methods' => ['GET', 'POST'],
116 'callback' => [$this, 'api_check_mybank_permissions'],
117 'permission_callback' => fn () => $permission,
118 'show_in_index' => false,
119 ]);
120 register_rest_route('payplug_api', '/ideal_permissions/', [
121 'methods' => ['GET', 'POST'],
122 'callback' => [$this, 'api_check_ideal_permissions'],
123 'permission_callback' => fn () => $permission,
124 'show_in_index' => false,
125 ]);
126 register_rest_route('payplug_api', '/integrated_permissions/', [
127 'methods' => ['GET', 'POST'],
128 'callback' => [$this, 'api_check_integrated_payment'],
129 'permission_callback' => fn () => $permission,
130 ]);
131 register_rest_route('payplug_api', '/wero_permissions/', [
132 'methods' => 'POST',
133 'callback' => [$this, 'api_check_wero_permissions'],
134 'permission_callback' => fn () => $permission,
135 'show_in_index' => false,
136 ]);
137 register_rest_route('payplug_api', '/bizum_permissions/', [
138 'methods' => 'POST',
139 'callback' => [$this, 'api_check_bizum_permissions'],
140 'permission_callback' => fn () => $permission,
141 'show_in_index' => false,
142 ]);
143 register_rest_route('payplug_api', '/scalapay_permissions/', [
144 'methods' => 'POST',
145 'callback' => [$this, 'api_check_scalapay_permissions'],
146 'permission_callback' => fn () => $permission,
147 'show_in_index' => false,
148 ]);
149 });
150 }
151
152 public function refresh_keys(WP_REST_Request $request): void
153 {
154 $data = $request->get_params();
155 $email = sanitize_text_field(wp_unslash($data['payplug_email']));
156 $password = base64_decode(wp_unslash($data['payplug_password']));
157
158 if (empty($email) || empty($password)) {
159 wp_send_json_error(
160 [
161 'message' => __('Invalid request.', 'payplug'),
162 ]
163 );
164 }
165
166 if (!WC()->payment_gateways()) {
167 wp_send_json_error(
168 [
169 'message' => __('An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug'),
170 ]
171 );
172 }
173
174 $payment_gateways = WC()->payment_gateways()->payment_gateways();
175 if (empty($payment_gateways) || !isset($payment_gateways['payplug'])) {
176 wp_send_json_error(
177 [
178 'message' => __('An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug'),
179 ]
180 );
181 }
182
183 /* @var PayplugGateway $payplug_gateway */
184 $payplug_gateway = $payment_gateways['payplug'];
185 $keys = $payplug_gateway->retrieve_user_api_keys($email, $password);
186 if (is_wp_error($keys)) {
187 wp_send_json_error(
188 [
189 'message' => $keys->get_error_message(),
190 ]
191 );
192 }
193
194 $success = $this->update_api_keys($keys, $payplug_gateway);
195
196 if (empty($keys['live'])) {
197 wp_send_json_error(
198 [
199 'message' => __('Your account does not support LIVE mode at the moment, it must be validated first. If your account has already been validated, please log out and log in again.', 'payplug'),
200 'still_inactive' => true,
201 ]
202 );
203 }
204
205 if (!$success) {
206 wp_send_json_error(
207 [
208 'message' => __('Something went wrong.', 'payplug'),
209 ]
210 );
211 }
212
213 wp_send_json_success(
214 [
215 'message' => __('Your API keys has successfully been updated.', 'payplug'),
216 ]
217 );
218 }
219
220 public function api_check_one_click_permission(WP_REST_Request $request): void
221 {
222 wp_send_json_success(true);
223 }
224
225 public function api_check_bancontact_permissions(WP_REST_Request $request): void
226 {
227 $account = $this->generic_get_account($request, Bancontact::ENABLE_ON_TEST_MODE);
228
229 if (isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) && $account['httpResponse']['payment_methods']['bancontact']['enabled']) {
230 wp_send_json_success(true);
231 }
232
233 wp_send_json_error([
234 'title' => __('payplug_enable_feature', 'payplug'),
235 'msg' => __('payplug_bancontact_access_error', 'payplug'),
236 'close' => __('payplug_ok', 'payplug'),
237 ]);
238 }
239
240 public function api_check_applepay_permissions(WP_REST_Request $request): void
241 {
242 $account = $this->generic_get_account($request, ApplePay::ENABLE_ON_TEST_MODE);
243
244 if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) {
245 if (in_array($_SERVER['HTTP_HOST'], $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) {
246 wp_send_json_success(true);
247 }
248 }
249
250 wp_send_json_error([
251 'title' => __('payplug_enable_feature', 'payplug'),
252 'msg' => __('payplug_applepay_access_error', 'payplug'),
253 'close' => __('payplug_ok', 'payplug'),
254 ]);
255 }
256
257 public function api_check_american_express_permissions(WP_REST_Request $request): void
258 {
259 $account = $this->generic_get_account($request, AmericanExpress::ENABLE_ON_TEST_MODE);
260
261 if (isset($account['httpResponse']['payment_methods']['american_express']['enabled']) && $account['httpResponse']['payment_methods']['american_express']['enabled']) {
262 wp_send_json_success(true);
263 }
264
265 wp_send_json_error([
266 'title' => __('payplug_enable_feature', 'payplug'),
267 'msg' => __('payplug_amex_access_error', 'payplug'),
268 'close' => __('payplug_ok', 'payplug'),
269 ]);
270 }
271
272 public function api_check_oney_permissions(WP_REST_Request $request): void
273 {
274 $account = $this->generic_get_account($request, PayplugGatewayOney3x::ENABLE_ON_TEST_MODE);
275
276 if (isset($account['httpResponse']['permissions']['can_use_oney']) && $account['httpResponse']['permissions']['can_use_oney']) {
277 wp_send_json_success(true);
278 }
279
280 $oney = isset($account['httpResponse']['permissions']['can_use_oney']) ? $account['httpResponse']['permissions']['can_use_oney'] : false;
281
282 if (!$oney) {
283 $anchor_text = __('payplug_oney_error_link', 'payplug');
284 $anchor_url = 'https://portal.payplug.com/login';
285 $anchor = sprintf(' <a href="%s" target="_blank">%s</a>', $anchor_url, $anchor_text);
286 $message = __('payplug_oney_error_description', 'payplug') . $anchor;
287 wp_send_json_error([
288 'title' => __('payplug_oney_error_title', 'payplug'),
289 'msg' => $message,
290 'close' => __('payplug_ok', 'payplug'),
291 ]);
292 }
293
294 wp_send_json_success(true);
295 }
296
297 public function api_check_satispay_permissions(WP_REST_Request $request): void
298 {
299 $account = $this->generic_get_account($request, Satispay::ENABLE_ON_TEST_MODE);
300
301 $enabled = isset($account['httpResponse']['payment_methods']['satispay']['enabled']) ? $account['httpResponse']['payment_methods']['satispay']['enabled'] : false;
302 if (!$enabled) {
303 wp_send_json_error([
304 'title' => __('payplug_enable_feature', 'payplug'),
305 'msg' => __('payplug_satispay_access_error', 'payplug'),
306 'close' => __('payplug_ok', 'payplug'),
307 ]);
308 }
309
310 wp_send_json_success($enabled);
311 }
312
313 public function api_check_mybank_permissions(WP_REST_Request $request): void
314 {
315 $account = $this->generic_get_account($request, Mybank::ENABLE_ON_TEST_MODE);
316
317 $enabled = isset($account['httpResponse']['payment_methods']['mybank']['enabled']) ? $account['httpResponse']['payment_methods']['mybank']['enabled'] : false;
318 if (!$enabled) {
319 wp_send_json_error([
320 'title' => __('payplug_enable_feature', 'payplug'),
321 'msg' => __('payplug_mybank_access_error', 'payplug'),
322 'close' => __('payplug_ok', 'payplug'),
323 ]);
324 }
325
326 wp_send_json_success($enabled);
327 }
328
329 public function api_check_ideal_permissions(WP_REST_Request $request): void
330 {
331 $account = $this->generic_get_account($request, Ideal::ENABLE_ON_TEST_MODE);
332
333 $enabled = isset($account['httpResponse']['payment_methods']['ideal']['enabled']) ? $account['httpResponse']['payment_methods']['ideal']['enabled'] : false;
334 if (!$enabled) {
335 wp_send_json_error([
336 'title' => __('payplug_enable_feature', 'payplug'),
337 'msg' => __('payplug_ideal_access_error', 'payplug'),
338 'close' => __('payplug_ok', 'payplug'),
339 ]);
340 }
341
342 wp_send_json_success($enabled);
343 }
344
345 public function api_check_wero_permissions(WP_REST_Request $request): void
346 {
347 $account = $this->generic_get_account($request, Wero::ENABLE_ON_TEST_MODE);
348
349 $enabled = isset($account['httpResponse']['payment_methods']['wero']['enabled']) ? $account['httpResponse']['payment_methods']['wero']['enabled'] : false;
350 if (!$enabled) {
351 wp_send_json_error([
352 'title' => __('payplug_enable_feature', 'payplug'),
353 'msg' => __('payplug_wero_access_error', 'payplug'),
354 'close' => __('payplug_ok', 'payplug'),
355 ]);
356 }
357
358 wp_send_json_success($enabled);
359 }
360
361 public function api_check_bizum_permissions(WP_REST_Request $request): void
362 {
363 $account = $this->generic_get_account($request, Bizum::ENABLE_ON_TEST_MODE);
364
365 $enabled = isset($account['httpResponse']['payment_methods']['bizum']['enabled']) ? $account['httpResponse']['payment_methods']['bizum']['enabled'] : false;
366 if (!$enabled) {
367 wp_send_json_error([
368 'title' => __('payplug_enable_feature', 'payplug'),
369 'msg' => __('payplug_bizum_access_error', 'payplug'),
370 'close' => __('payplug_ok', 'payplug'),
371 ]);
372 }
373
374 wp_send_json_success($enabled);
375 }
376
377 public function api_check_scalapay_permissions(WP_REST_Request $request): void
378 {
379 $account = $this->generic_get_account($request, Scalapay::ENABLE_ON_TEST_MODE);
380
381 $enabled = isset($account['httpResponse']['payment_methods']['scalapay']['enabled']) ? $account['httpResponse']['payment_methods']['scalapay']['enabled'] : false;
382 if (!$enabled) {
383 wp_send_json_error([
384 'title' => __('payplug_enable_feature', 'payplug'),
385 'msg' => __('payplug_scalapay_access_error', 'payplug'),
386 'close' => __('payplug_ok', 'payplug'),
387 ]);
388 }
389
390 wp_send_json_success($enabled);
391 }
392
393 private function generic_get_account($request, $enable_on_test_mode)
394 {
395 $data = $request->get_params();
396
397 if (isset($data['env']) && $data['env']) {
398 if ($enable_on_test_mode) {
399 wp_send_json_success(true);
400 } else {
401 $this->optionUnnavailableInTestMode();
402 }
403 }
404
405 $this->accountIsNotValid();
406
407 $live_key = PayplugWoocommerceHelper::get_live_key();
408
409 if (empty($live_key)) {
410 return false;
411 }
412
413 try {
414 $account = Authentication::getAccount(new Payplug($live_key));
415 } catch (PayplugException $e) {
416 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
417
418 return false;
419 }
420
421 return $account;
422 }
423
424 /**
425 * Update PayPlug api keys
426 *&
427 *
428 * @param array $keys
429 * @param PayplugGateway $payplug_gateway
430 *
431 * @return bool
432 */
433 protected function update_api_keys($keys, $payplug_gateway)
434 {
435 if (empty($payplug_gateway->settings)) {
436 $payplug_gateway->init_settings();
437 }
438
439 $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
440 $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
441 if (!empty($keys['live'])) {
442 $payplug_gateway->settings['mode'] = true;
443 }
444
445 return update_option(
446 $payplug_gateway->get_option_key(),
447 apply_filters(
448 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
449 $payplug_gateway->settings
450 ),
451 'yes'
452 );
453 }
454
455 /**
456 * Response Json Error WP Format for wrong credentials
457 *
458 * @return void
459 */
460 private function login_wrong_credentials_error(): void
461 {
462 wp_send_json_error(['message' => __('payplug_error_wrong_credentials', 'payplug')]);
463 }
464
465 /**
466 * Ajax payplug initialisation
467 *
468 * @return JSON
469 */
470 public function payplug_init()
471 {
472 $wp_nonce = wp_create_nonce();
473
474 $wp = [
475 'logged' => $this->get_gateway('account')->is_logged(),
476 'mode' => PayplugWoocommerceHelper::check_mode() ? 0 : 1,
477 'WP' => [
478 '_wpnonce' => $wp_nonce,
479 ],
480 ];
481
482 return wp_send_json_success([
483 'settings' => $wp,
484 ] + (new Vue())->init());
485 }
486
487 /**
488 * @return bool|null
489 */
490 public function payplug_logout()
491 {
492 PayplugWoocommerceHelper::payplug_logout();
493 $wp = [
494 'logged' => $this->get_gateway('account')->is_logged(),
495 'mode' => PayplugWoocommerceHelper::check_mode() ? 0 : 1,
496 ];
497
498 http_response_code(200);
499 wp_send_json_success([
500 'message' => __('Successfully logged out.', 'payplug'),
501 'status' => (new Vue())->payplug_section_status(),
502 'settings' => $wp,
503 'subscribe' => (new Vue())->payplug_section_subscribe(), // When Logging out the Status Block needs to be updated
504 ]);
505 }
506
507 private function accountIsNotValid(): void
508 {
509 $live_key = PayplugWoocommerceHelper::get_live_key();
510 if (empty($live_key)) {
511 wp_send_json_error([
512 'title' => __('payplug_enable_feature', 'payplug'),
513 'msg' => __('Your account does not support LIVE mode at the moment, it must be validated first. If your account has already been validated, please log out and log in again.', 'payplug'),
514 'close' => __('payplug_ok', 'payplug'),
515 ]);
516 }
517 }
518
519 private function optionUnnavailableInTestMode(): void
520 {
521 wp_send_json_error([
522 'title' => __('payplug_enable_feature', 'payplug'),
523 'msg' => __('payplug_unavailable_testmode_description', 'payplug'),
524 ]);
525 }
526
527 public function payplug_check_requirements(): void
528 {
529 wp_send_json_success([
530 'status' => (new Vue())->payplug_section_status(),
531 ]);
532 }
533
534 public function api_check_integrated_payment(WP_REST_Request $request)
535 {
536 $account = $this->generic_get_account($request, true);
537
538 if (!isset($account['httpResponse']['permissions']['can_use_integrated_payments'])
539 || !$account['httpResponse']['permissions']['can_use_integrated_payments']) {
540 wp_send_json_error([
541 'title' => __('payplug_enable_feature', 'payplug'),
542 'msg' => __('payplug_integrated_access_error', 'payplug'),
543 'close' => __('payplug_ok', 'payplug'),
544 ]);
545 }
546
547 wp_send_json_success(true);
548
549 return true;
550 }
551
552 public function login_action(): void
553 {
554 $data = json_decode(file_get_contents('php://input'), true);
555 $email = sanitize_email($data['payplug_email']);
556 $password = base64_decode(wp_unslash($data['payplug_password']));
557 $wp_nonce = !empty($data['_wpnonce']) ? $data['_wpnonce'] : null;
558
559 // use AccountGateway
560 try {
561 $account_gateway = $this->get_gateway('account');
562 $register = $account_gateway->register((string) $email, (string) $password);
563 if (!empty($register['multiple_users'])) {
564 wp_send_json_error(['message' => __('payplug_error_multiple_users', 'payplug')]);
565 }
566 if (empty($register)) {
567 $this->login_wrong_credentials_error();
568 }
569
570 $user = [
571 'logged' => true,
572 'email' => $email,
573 'mode' => (bool) $register['mode'] ? 0 : 1,
574 ];
575 $wp = [
576 'WP' => [
577 '_wpnonce' => $wp_nonce,
578 ],
579 ];
580 $vue = (new Vue())->init();
581
582 wp_send_json_success(['settings' => $user + $wp] + $vue);
583 } catch (HttpException $e) {
584 $this->login_wrong_credentials_error();
585 }
586 }
587
588 public function save_action(WP_REST_Request $request): void
589 {
590 if (!$this->get_gateway('account')->is_logged()) {
591 http_response_code(403);
592 wp_send_json_error('You are not logged in !');
593 }
594
595 $payplug = new PayplugGateway();
596 $options = $payplug->settings;
597
598 $data = json_decode(file_get_contents('php://input'), true);
599
600 // global configuration
601 $options['enabled'] = (bool) $data['payplug_enable'];
602 $options['debug'] = (bool) $data['enable_debug'];
603 $options['mode'] = (bool) $data['payplug_sandbox'] ? false : true;
604
605 // payment configuration
606 $payment_methods = [
607 'oney',
608 'apple_pay',
609 'payplug',
610 'american_express',
611 'bancontact',
612 'satispay',
613 'mybank',
614 'ideal',
615 'wero',
616 'bizum',
617 'scalapay',
618 ];
619 foreach ($payment_methods as $name) {
620 if (!isset($options['payment_methods']['configuration'][$name])) {
621 continue;
622 }
623 $options['payment_methods']['configuration'][$name]['active'] = (bool) $data['enable_' . $name];
624 }
625
626 // standard payment
627 $options['payment_methods']['configuration']['payplug']['active'] = (bool) $data['enable_standard'];
628 $options['payment_methods']['configuration']['payplug']['title'] = (string) $data['standard_payment_title'];
629 $options['payment_methods']['configuration']['payplug']['description'] = (string) $data['standard_payment_description'];
630 $options['payment_methods']['configuration']['payplug']['embedded_mode'] = (string) $data['payplug_embeded'];
631 $options['payment_methods']['configuration']['payplug']['save_card'] = (bool) $data['enable_one_click'];
632
633 // applepay payment
634 $applepay_active = (bool) $data['enable_applepay'];
635 $applepay_carriers = $data['applepay_carriers'];
636 $applepay_cart = (bool) $data['enable_applepay_cart'];
637 $applepay_checkout = (bool) $data['enable_applepay_checkout'];
638 $applepay_product = (bool) $data['enable_applepay_product'];
639 // if product or cart options are checked but no carriers selected, return error
640 $applepay_active = $applepay_active && Validator::applePayPaymentGatewayOptions($applepay_active, $applepay_cart, $applepay_product, $applepay_checkout, $applepay_carriers);
641 $options['payment_methods']['configuration']['apple_pay']['active'] = $applepay_active;
642 $options['payment_methods']['configuration']['apple_pay']['carriers'] = json_encode($applepay_carriers);
643 $options['payment_methods']['configuration']['apple_pay']['display'] = json_encode([
644 'cart' => $applepay_cart,
645 'checkout' => $applepay_checkout,
646 'product' => $applepay_product,
647 ]);
648
649 // oney payment
650 $options['payment_methods']['configuration']['oney']['active'] = (bool) $data['enable_oney'];
651 $options['payment_methods']['configuration']['oney']['cta_product'] = (bool) $data['enable_oney_product_animation'];
652 $options['payment_methods']['configuration']['oney']['custom_amounts'] = json_encode([
653 'min' => (int) $data['oney_min_amounts'] * 100,
654 'max' => (int) $data['oney_max_amounts'] * 100,
655 ]);
656 $options['payment_methods']['configuration']['oney']['with_fees'] = 'with_fees' == (string) $data['payplug_oney'];
657
658 // Force-disable methods unavailable in test mode: submitted 'active' flags are
659 // trusted as-is, so one enabled while in live mode stays active after just
660 // switching to test unless we re-check it here.
661 if (!$options['mode']) {
662 $enable_on_test_mode = [
663 'oney' => PayplugGatewayOney3x::ENABLE_ON_TEST_MODE,
664 'apple_pay' => ApplePay::ENABLE_ON_TEST_MODE,
665 'payplug' => PayplugGateway::ENABLE_ON_TEST_MODE,
666 'american_express' => AmericanExpress::ENABLE_ON_TEST_MODE,
667 'bancontact' => Bancontact::ENABLE_ON_TEST_MODE,
668 'satispay' => Satispay::ENABLE_ON_TEST_MODE,
669 'mybank' => Mybank::ENABLE_ON_TEST_MODE,
670 'ideal' => Ideal::ENABLE_ON_TEST_MODE,
671 'wero' => Wero::ENABLE_ON_TEST_MODE,
672 'bizum' => Bizum::ENABLE_ON_TEST_MODE,
673 'scalapay' => Scalapay::ENABLE_ON_TEST_MODE,
674 ];
675 foreach ($enable_on_test_mode as $name => $allowed) {
676 if (!$allowed && isset($options['payment_methods']['configuration'][$name]['active'])) {
677 $options['payment_methods']['configuration'][$name]['active'] = false;
678 }
679 }
680 }
681
682 //
683 $update = $payplug->get_service('configuration')->update_options($options);
684
685 if ($update) {
686 //delete the transient, so it refresh the permissions on the init
687 $transient_key = PayplugWoocommerceHelper::get_transient_key($options);
688 delete_transient($transient_key);
689
690 http_response_code(200);
691 wp_send_json_success([
692 'title' => null,
693 'msg' => __('payplug_save_success_message', 'payplug'),
694 'close' => __('payplug_ok', 'payplug'),
695 ]);
696 } else {
697 http_response_code(200);
698 wp_send_json_error([
699 'title' => null,
700 'close' => __('payplug_ok', 'payplug'),
701 'msg' => 'These settings are already saved !', ]);
702 }
703 }
704 }
705