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 +679 -165 1.10.0 → 3.1.0 View file →
@@ -2,211 +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;
10 21 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
11 -use Payplug\Exception\PayplugException;
22 +use Payplug\PayplugWoocommerce\Traits\GatewayGetter;
23 +use WP_REST_Request;
12 24
13 -if ( ! defined( 'ABSPATH' ) ) {
14 - exit;
25 +if (!defined('ABSPATH')) {
26 + exit;
15 27 }
16 28
17 29 /**
18 30 * PayPlug admin ajax handler.
19 - *
20 - * @package Payplug\PayplugWoocommerce\Admin
21 31 */
22 -class Ajax {
32 +class Ajax
33 +{
34 + use GatewayGetter;
23 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 +
24 424 /**
25 - * @var PayplugPermissions
425 + * Update PayPlug api keys
426 + *&
427 + *
428 + * @param array $keys
429 + * @param PayplugGateway $payplug_gateway
430 + *
431 + * @return bool
26 432 */
27 - private $permissions;
433 + protected function update_api_keys($keys, $payplug_gateway)
434 + {
435 + if (empty($payplug_gateway->settings)) {
436 + $payplug_gateway->init_settings();
437 + }
28 438
29 - const REFRESH_KEY_ACTION = 'payplug_refresh_keys';
30 - const CHECK_LIVE_PERMISSIONS = 'check_live_permissions';
31 - const CHECK_BANCONTACT_PERMISSIONS = 'check_bancontact_permissions';
32 - const CHECK_APPLEPAY_PERMISSIONS = 'check_applepay_permissions';
33 - const CHECK_AMERICAN_EXPRESS_PERMISSIONS = 'check_american_express_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 + }
34 444
35 - public function __construct() {
36 - add_action( 'wp_ajax_' . self::REFRESH_KEY_ACTION, [ $this, 'handle_refresh_keys' ] );
37 - add_action( 'wp_ajax_' . self::CHECK_LIVE_PERMISSIONS, [ $this, 'check_live_permissions' ] );
38 - add_action( 'wp_ajax_' . self::CHECK_BANCONTACT_PERMISSIONS, [ $this, 'check_bancontact_permissions' ] );
39 - add_action( 'wp_ajax_' . self::CHECK_APPLEPAY_PERMISSIONS, [ $this, 'check_applepay_permissions' ] );
40 - add_action( 'wp_ajax_' . self::CHECK_AMERICAN_EXPRESS_PERMISSIONS, [ $this, 'check_american_express_permissions' ] );
41 - }
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 + }
42 454
43 - 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 + }
44 464
45 - if ( empty( $_POST['_wpnonce'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) ) {
46 - wp_send_json_error(
47 - array(
48 - 'message' => __( 'Invalid request.', 'payplug' ),
49 - )
50 - );
51 - }
465 + /**
466 + * Ajax payplug initialisation
467 + *
468 + * @return JSON
469 + */
470 + public function payplug_init()
471 + {
472 + $wp_nonce = wp_create_nonce();
52 473
53 - $action = sprintf( '%s_%s', wp_unslash( $_POST['email'] ), self::REFRESH_KEY_ACTION );
54 - if ( ! check_ajax_referer( $action ) ) {
55 - wp_send_json_error(
56 - array(
57 - 'message' => __( 'Invalid nonce.', 'payplug' ),
58 - )
59 - );
60 - }
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 + ];
61 481
62 - $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
63 - $password = sanitize_text_field( wp_unslash( $_POST['password'] ) );
482 + return wp_send_json_success([
483 + 'settings' => $wp,
484 + ] + (new Vue())->init());
485 + }
64 486
65 - if ( ! WC()->payment_gateways() ) {
66 - wp_send_json_error(
67 - array(
68 - 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
69 - )
70 - );
71 - }
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 + ];
72 497
73 - $payment_gateways = WC()->payment_gateways()->payment_gateways();
74 - if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) {
75 - wp_send_json_error(
76 - array(
77 - 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
78 - )
79 - );
80 - }
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 + }
81 506
82 - /* @var PayplugGateway $payplug_gateway */
83 - $payplug_gateway = $payment_gateways['payplug'];
84 - $keys = $payplug_gateway->retrieve_user_api_keys( $email, $password );
85 - if ( is_wp_error( $keys ) ) {
86 - wp_send_json_error(
87 - array(
88 - 'message' => $keys->get_error_message(),
89 - )
90 - );
91 - }
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 + }
92 518
93 - $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 + }
94 526
95 - if ( empty( $keys['live'] ) ) {
96 - wp_send_json_error(
97 - array(
98 - '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' ),
99 - )
100 - );
101 - }
527 + public function payplug_check_requirements(): void
528 + {
529 + wp_send_json_success([
530 + 'status' => (new Vue())->payplug_section_status(),
531 + ]);
532 + }
102 533
103 - if ( ! $success ) {
104 - wp_send_json_error(
105 - array(
106 - 'message' => __( 'Something went wrong.', 'payplug' ),
107 - )
108 - );
109 - }
534 + public function api_check_integrated_payment(WP_REST_Request $request)
535 + {
536 + $account = $this->generic_get_account($request, true);
110 537
111 - wp_send_json_success(
112 - array(
113 - 'message' => __( 'Your API keys has successfully been updated.', 'payplug' )
114 - )
115 - );
116 - }
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 + }
117 546
118 - public function check_live_permissions() {
119 - try{
120 - $account = Authentication::getAccount(new Payplug($_POST['livekey']));
121 - } catch (PayplugException $e){
122 - PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
123 - wp_send_json_error(["error" => $e->getMessage()]);
124 - return false;
125 - }
126 - PayplugWoocommerceHelper::set_transient_data($account);
127 - $permissions = $account['httpResponse']['permissions'];
128 - wp_send_json_success($permissions);
129 - }
547 + wp_send_json_success(true);
130 548
131 - public function check_bancontact_permissions() {
132 - try{
133 - $account = Authentication::getAccount(new Payplug($_POST['livekey']));
549 + return true;
550 + }
134 551
135 - } catch (PayplugException $e){
136 - PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
137 - wp_send_json_error(["error" => $e->getMessage()]);
138 - return false;
139 - }
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;
140 558
141 - PayplugWoocommerceHelper::set_transient_data($account);
142 - $bancontact = isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) ? $account['httpResponse']['payment_methods']['bancontact']['enabled']: false;
143 - wp_send_json_success($bancontact);
144 - }
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 + }
145 569
146 - public function check_applepay_permissions() {
147 - try{
148 - $account = Authentication::getAccount(new Payplug($_POST['livekey']));
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();
149 581
150 - } catch (PayplugException $e){
151 - PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
152 - wp_send_json_error(["error" => $e->getMessage()]);
153 - return false;
154 - }
582 + wp_send_json_success(['settings' => $user + $wp] + $vue);
583 + } catch (HttpException $e) {
584 + $this->login_wrong_credentials_error();
585 + }
586 + }
155 587
156 - PayplugWoocommerceHelper::set_transient_data($account);
157 - $applepay = false;
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 + }
158 594
159 - if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) {
160 - if (in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) {
161 - $applepay = true;
162 - }
595 + $payplug = new PayplugGateway();
596 + $options = $payplug->settings;
163 597
164 - }
165 - wp_send_json_success($applepay);
166 - }
598 + $data = json_decode(file_get_contents('php://input'), true);
167 599
168 - public function check_american_express_permissions() {
169 - try{
170 - $account = Authentication::getAccount(new Payplug($_POST['livekey']));
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;
171 604
172 - } catch (PayplugException $e){
173 - PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
174 - wp_send_json_error(["error" => $e->getMessage()]);
175 - return false;
176 - }
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 + }
177 625
178 - PayplugWoocommerceHelper::set_transient_data($account);
179 - $amex = isset($account['httpResponse']['payment_methods']['american_express']['enabled']) ? $account['httpResponse']['payment_methods']['american_express']['enabled']: false;
180 - wp_send_json_success($amex);
181 - }
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'];
182 632
183 - /**
184 - * Update PayPlug api keys
185 - *&
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 + ]);
186 648
187 - * @param array $keys
188 - * @param PayplugGateway $payplug_gateway
189 - *
190 - * @return bool
191 - */
192 - protected function update_api_keys( $keys, $payplug_gateway ) {
193 - if ( empty( $payplug_gateway->settings ) ) {
194 - $payplug_gateway->init_settings();
195 - }
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'];
196 657
197 - $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
198 - $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
199 - if ( ! empty( $keys['live'] ) ) {
200 - $payplug_gateway->settings['mode'] = 'yes';
201 - }
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 + }
202 679
203 - return update_option(
204 - $payplug_gateway->get_option_key(),
205 - apply_filters(
206 - 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
207 - $payplug_gateway->settings
208 - ),
209 - 'yes'
210 - );
211 - }
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 + }
212 726 }