PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.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 All 102 releases
← All changes | src/Admin/Ajax.php +693 -112 1.2.2 → 3.1.0 View file →
@@ -2,144 +2,725 @@
2 2
3 3 namespace Payplug\PayplugWoocommerce\Admin;
4 4
5 5 // Exit if accessed directly
6 +use Payplug\Authentication;
7 +use Payplug\Exception\HttpException;
8 +use Payplug\Exception\PayplugException;
6 9 use Payplug\Payplug;
7 -use Payplug\Authentication;
10 +use Payplug\PayplugWoocommerce\Controller\ApplePay;
11 +use Payplug\PayplugWoocommerce\Gateway\AmericanExpress;
12 +use Payplug\PayplugWoocommerce\Gateway\Bancontact;
8 13 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
9 -use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
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;
10 24
11 -if ( ! defined( 'ABSPATH' ) ) {
12 - exit;
25 +if (!defined('ABSPATH')) {
26 + exit;
13 27 }
14 28
15 29 /**
16 30 * PayPlug admin ajax handler.
17 - *
18 - * @package Payplug\PayplugWoocommerce\Admin
19 31 */
20 -class Ajax {
32 +class Ajax
33 +{
34 + use GatewayGetter;
21 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 +
22 424 /**
23 - * @var PayplugPermissions
425 + * Update PayPlug api keys
426 + *&
427 + *
428 + * @param array $keys
429 + * @param PayplugGateway $payplug_gateway
430 + *
431 + * @return bool
24 432 */
25 - private $permissions;
433 + protected function update_api_keys($keys, $payplug_gateway)
434 + {
435 + if (empty($payplug_gateway->settings)) {
436 + $payplug_gateway->init_settings();
437 + }
26 438
27 - const REFRESH_KEY_ACTION = 'payplug_refresh_keys';
28 - const CHECK_LIVE_PERMISSIONS = 'check_live_permissions';
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 + }
29 444
30 - public function __construct() {
31 - add_action( 'wp_ajax_' . self::REFRESH_KEY_ACTION, [ $this, 'handle_refresh_keys' ] );
32 - add_action( 'wp_ajax_' . self::CHECK_LIVE_PERMISSIONS, [ $this, 'check_live_permissions' ] );
33 - }
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 + }
34 454
35 - public function handle_refresh_keys() {
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 + }
36 464
37 - if ( empty( $_POST['_wpnonce'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) ) {
38 - wp_send_json_error(
39 - array(
40 - 'message' => __( 'Invalid request.', 'payplug' ),
41 - )
42 - );
43 - }
465 + /**
466 + * Ajax payplug initialisation
467 + *
468 + * @return JSON
469 + */
470 + public function payplug_init()
471 + {
472 + $wp_nonce = wp_create_nonce();
44 473
45 - $action = sprintf( '%s_%s', wp_unslash( $_POST['email'] ), self::REFRESH_KEY_ACTION );
46 - if ( ! check_ajax_referer( $action ) ) {
47 - wp_send_json_error(
48 - array(
49 - 'message' => __( 'Invalid nonce.', 'payplug' ),
50 - )
51 - );
52 - }
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 + ];
53 481
54 - $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
55 - $password = sanitize_text_field( wp_unslash( $_POST['password'] ) );
482 + return wp_send_json_success([
483 + 'settings' => $wp,
484 + ] + (new Vue())->init());
485 + }
56 486
57 - if ( ! WC()->payment_gateways() ) {
58 - wp_send_json_error(
59 - array(
60 - 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
61 - )
62 - );
63 - }
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 + ];
64 497
65 - $payment_gateways = WC()->payment_gateways()->payment_gateways();
66 - if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) {
67 - wp_send_json_error(
68 - array(
69 - 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
70 - )
71 - );
72 - }
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 + }
73 506
74 - /* @var PayplugGateway $payplug_gateway */
75 - $payplug_gateway = $payment_gateways['payplug'];
76 - $keys = $payplug_gateway->retrieve_user_api_keys( $email, $password );
77 - if ( is_wp_error( $keys ) ) {
78 - wp_send_json_error(
79 - array(
80 - 'message' => $keys->get_error_message(),
81 - )
82 - );
83 - }
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 + }
84 518
85 - $success = $this->update_api_keys( $keys, $payplug_gateway );
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 + }
86 526
87 - if ( empty( $keys['live'] ) ) {
88 - wp_send_json_error(
89 - array(
90 - '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' ),
91 - )
92 - );
93 - }
527 + public function payplug_check_requirements(): void
528 + {
529 + wp_send_json_success([
530 + 'status' => (new Vue())->payplug_section_status(),
531 + ]);
532 + }
94 533
95 - if ( ! $success ) {
96 - wp_send_json_error(
97 - array(
98 - 'message' => __( 'Something went wrong.', 'payplug' ),
99 - )
100 - );
101 - }
534 + public function api_check_integrated_payment(WP_REST_Request $request)
535 + {
536 + $account = $this->generic_get_account($request, true);
102 537
103 - wp_send_json_success(
104 - array(
105 - 'message' => __( 'Your API keys has successfully been updated.', 'payplug' )
106 - )
107 - );
108 - }
109 -
110 - public function check_live_permissions() {
111 - $account = Authentication::getAccount(new Payplug($_POST['livekey']));
112 - $permissions = $account['httpResponse']['permissions'];
113 - wp_send_json_success($permissions);
114 - }
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 + }
115 546
116 - /**
117 - * Update PayPlug api keys
118 - *&
119 -
120 - * @param array $keys
121 - * @param PayplugGateway $payplug_gateway
122 - *
123 - * @return bool
124 - */
125 - protected function update_api_keys( $keys, $payplug_gateway ) {
126 - if ( empty( $payplug_gateway->settings ) ) {
127 - $payplug_gateway->init_settings();
128 - }
547 + wp_send_json_success(true);
129 548
130 - $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
131 - $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
132 - if ( ! empty( $keys['live'] ) ) {
133 - $payplug_gateway->settings['mode'] = 'yes';
134 - }
549 + return true;
550 + }
135 551
136 - return update_option(
137 - $payplug_gateway->get_option_key(),
138 - apply_filters(
139 - 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
140 - $payplug_gateway->settings
141 - ),
142 - 'yes'
143 - );
144 - }
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 + // scalapay payment - the thresholds widget isn't necessarily rendered (a stale admin
659 + // JS bundle right after a deploy, for instance). Its absence must not reach
660 + // Validator::scalapay_thresholds(), which would read the missing values as 0, fail
661 + // the "below the account minimum" check and abort the whole request via
662 + // wp_send_json_error() - discarding every other setting in this save, since
663 + // update_options() only runs at the end of this method.
664 + if (array_key_exists('scalapay_min_amounts', $data) && array_key_exists('scalapay_max_amounts', $data)) {
665 + [$scalapay_authorized_min, $scalapay_authorized_max] = Scalapay::authorized_bounds(
666 + $options['payment_methods']['configuration']['scalapay'] ?? [],
667 + $options['payment_methods']['permissions']['scalapay']['amounts'] ?? '{}'
668 + );
669 + // round() before the cast: the submitted values are euros, so a bare (int) cast
670 + // would bind tighter than the * 100 and truncate the cents (49.99 -> 4900).
671 + $scalapay_thresholds = Validator::scalapay_thresholds(
672 + (int) round((float) $data['scalapay_min_amounts'] * 100),
673 + (int) round((float) $data['scalapay_max_amounts'] * 100),
674 + $scalapay_authorized_min,
675 + $scalapay_authorized_max
676 + );
677 + $options['payment_methods']['configuration']['scalapay']['custom_amounts'] = json_encode($scalapay_thresholds);
678 + }
679 +
680 + // Force-disable methods unavailable in test mode: submitted 'active' flags are
681 + // trusted as-is, so one enabled while in live mode stays active after just
682 + // switching to test unless we re-check it here.
683 + if (!$options['mode']) {
684 + $enable_on_test_mode = [
685 + 'oney' => PayplugGatewayOney3x::ENABLE_ON_TEST_MODE,
686 + 'apple_pay' => ApplePay::ENABLE_ON_TEST_MODE,
687 + 'payplug' => PayplugGateway::ENABLE_ON_TEST_MODE,
688 + 'american_express' => AmericanExpress::ENABLE_ON_TEST_MODE,
689 + 'bancontact' => Bancontact::ENABLE_ON_TEST_MODE,
690 + 'satispay' => Satispay::ENABLE_ON_TEST_MODE,
691 + 'mybank' => Mybank::ENABLE_ON_TEST_MODE,
692 + 'ideal' => Ideal::ENABLE_ON_TEST_MODE,
693 + 'wero' => Wero::ENABLE_ON_TEST_MODE,
694 + 'bizum' => Bizum::ENABLE_ON_TEST_MODE,
695 + 'scalapay' => Scalapay::ENABLE_ON_TEST_MODE,
696 + ];
697 + foreach ($enable_on_test_mode as $name => $allowed) {
698 + if (!$allowed && isset($options['payment_methods']['configuration'][$name]['active'])) {
699 + $options['payment_methods']['configuration'][$name]['active'] = false;
700 + }
701 + }
702 + }
703 +
704 + //
705 + $update = $payplug->get_service('configuration')->update_options($options);
706 +
707 + if ($update) {
708 + //delete the transient, so it refresh the permissions on the init
709 + $transient_key = PayplugWoocommerceHelper::get_transient_key($options);
710 + delete_transient($transient_key);
711 +
712 + http_response_code(200);
713 + wp_send_json_success([
714 + 'title' => null,
715 + 'msg' => __('payplug_save_success_message', 'payplug'),
716 + 'close' => __('payplug_ok', 'payplug'),
717 + ]);
718 + } else {
719 + http_response_code(200);
720 + wp_send_json_error([
721 + 'title' => null,
722 + 'close' => __('payplug_ok', 'payplug'),
723 + 'msg' => 'These settings are already saved !', ]);
724 + }
725 + }
145 726 }