PluginProbe
PayPlug for WooCommerce (Official) / 2.6.3
PayPlug for WooCommerce (Official) v2.6.3
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) 2.6.3, at src/Admin/Ajax.php

870 lines 26.9 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\Exception\HttpException;
7 use Payplug\Payplug;
8 use Payplug\Authentication;
9 use Payplug\PayplugWoocommerce\Admin\Vue;
10 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
11 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
12 use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
13 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
14 use Payplug\Exception\PayplugException;
15 use WP_REST_Request;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * PayPlug admin ajax handler.
23 *
24 * @package Payplug\PayplugWoocommerce\Admin
25 */
26 class Ajax {
27
28 public function __construct() {
29 $permission = ( current_user_can('editor') || current_user_can('administrator') );
30
31 add_action( 'rest_api_init', function () use ($permission) {
32 //Path to REST route and the callback function
33 register_rest_route( 'payplug_api', '/save/', array(
34 'methods' => 'POST',
35 'callback' => [ $this, 'payplug_save_data' ],
36 'permission_callback' => function () use ($permission) {return $permission ;},
37 'show_in_index' => false
38 ) );
39 register_rest_route( 'payplug_api', '/init/', array(
40 'methods' => 'POST',
41 'callback' => [ $this, 'payplug_init' ],
42 'permission_callback' => function () use ($permission) {return $permission ;},
43 'show_in_index' => false
44 ) );
45 register_rest_route( 'payplug_api', '/login/', array(
46 'methods' => 'POST',
47 'callback' => [ $this, 'payplug_login' ],
48 'permission_callback' => function () use ($permission) {return $permission ;},
49 'show_in_index' => false
50 ) );
51 register_rest_route( 'payplug_api', '/logout/', array(
52 'methods' => 'POST',
53 'callback' => [ $this, 'payplug_logout' ],
54 'permission_callback' => function () use ($permission) {return $permission ;},
55 'show_in_index' => false
56 ) );
57 register_rest_route( 'payplug_api', '/refresh_keys/', array(
58 'methods' => 'POST',
59 'callback' => [ $this, 'refresh_keys' ],
60 'permission_callback' => function () use ($permission) {return $permission ;},
61 'show_in_index' => false
62 ) );
63 register_rest_route( 'payplug_api', '/check_requirements/', array(
64 'methods' => 'POST',
65 'callback' => [ $this, 'payplug_check_requirements' ],
66 'permission_callback' => function () use ($permission) {return $permission ;},
67 'show_in_index' => false
68 ) );
69 register_rest_route( 'payplug_api', '/bancontact_permissions/', array(
70 'methods' => 'POST',
71 'callback' => [ $this, 'api_check_bancontact_permissions' ],
72 'permission_callback' => function () use ($permission) {return $permission ;},
73 'show_in_index' => false
74 ) );
75 register_rest_route( 'payplug_api', '/applepay_permissions/', array(
76 'methods' => 'POST',
77 'callback' => [ $this, 'api_check_applepay_permissions' ],
78 'permission_callback' => function () use ($permission) {return $permission ;},
79 'show_in_index' => false
80 ) );
81 register_rest_route( 'payplug_api', '/american_express_permissions/', array(
82 'methods' => 'POST',
83 'callback' => [ $this, 'api_check_american_express_permissions' ],
84 'permission_callback' => function () use ($permission) {return $permission ;},
85 'show_in_index' => false
86 ) );
87 register_rest_route( 'payplug_api', '/oney_permissions/', array(
88 'methods' => 'POST',
89 'callback' => [ $this, 'api_check_oney_permissions' ],
90 'permission_callback' => function () use ($permission) {return $permission ;},
91 'show_in_index' => false
92 ) );
93 register_rest_route( 'payplug_api', '/one_click_permission/', array(
94 'methods' => 'POST',
95 'callback' => [ $this, 'api_check_one_click_permission' ],
96 'permission_callback' => function () use ($permission) {return $permission ;},
97 'show_in_index' => false
98 ) );
99 register_rest_route( 'payplug_api', '/satispay_permissions/', array(
100 'methods' => 'POST',
101 'callback' => [ $this, 'api_check_satispay_permissions' ],
102 'permission_callback' => function () use ($permission) {return $permission ;},
103 'show_in_index' => false
104 ) );
105 register_rest_route( 'payplug_api', '/mybank_permissions/', array(
106 'methods' => 'POST',
107 'callback' => [ $this, 'api_check_mybank_permissions' ],
108 'permission_callback' => function () use ($permission) {return $permission ;},
109 'show_in_index' => false
110 ) );
111 register_rest_route( 'payplug_api', '/sofort_permissions/', array(
112 'methods' => 'POST',
113 'callback' => [ $this, 'api_check_sofort_permissions' ],
114 'permission_callback' => function () use ($permission) {return $permission ;},
115 'show_in_index' => false
116 ) );
117 register_rest_route( 'payplug_api', '/giropay_permissions/', array(
118 'methods' => 'POST',
119 'callback' => [ $this, 'api_check_giropay_permissions' ],
120 'permission_callback' => function () use ($permission) {return $permission ;},
121 'show_in_index' => false
122 ) );
123 register_rest_route( 'payplug_api', '/ideal_permissions/', array(
124 'methods' => 'POST',
125 'callback' => [ $this, 'api_check_ideal_permissions' ],
126 'permission_callback' => function () use ($permission) {return $permission ;},
127 'show_in_index' => false
128 ) );
129 register_rest_route( 'payplug_api', '/integrated_permissions/', array(
130 'methods' => 'POST',
131 'callback' => [ $this, 'api_check_integrated_payment' ],
132 'permission_callback' => function () use ($permission) { return $permission ; }
133 ) );
134
135 });
136
137
138
139 }
140
141 public function refresh_keys(WP_REST_Request $request) {
142 $data = $request->get_params();
143 $email = sanitize_text_field( wp_unslash( $data['payplug_email'] ) );
144 $password = base64_decode(wp_unslash($data['payplug_password']));
145
146 if ( empty( $email ) || empty( $password ) ) {
147 wp_send_json_error(
148 array(
149 'message' => __( 'Invalid request.', 'payplug' ),
150 )
151 );
152 }
153
154 if ( ! WC()->payment_gateways() ) {
155 wp_send_json_error(
156 array(
157 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
158 )
159 );
160 }
161
162 $payment_gateways = WC()->payment_gateways()->payment_gateways();
163 if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) {
164 wp_send_json_error(
165 array(
166 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
167 )
168 );
169 }
170
171 /* @var PayplugGateway $payplug_gateway */
172 $payplug_gateway = $payment_gateways['payplug'];
173 $keys = $payplug_gateway->retrieve_user_api_keys( $email, $password );
174 if ( is_wp_error( $keys ) ) {
175 wp_send_json_error(
176 array(
177 'message' => $keys->get_error_message(),
178 )
179 );
180 }
181
182 $success = $this->update_api_keys( $keys, $payplug_gateway );
183
184 if ( empty( $keys['live'] ) ) {
185 wp_send_json_error(
186 array(
187 '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' ),
188 'still_inactive' => true
189 )
190 );
191 }
192
193 if ( ! $success ) {
194 wp_send_json_error(
195 array(
196 'message' => __( 'Something went wrong.', 'payplug' ),
197 )
198 );
199 }
200
201 wp_send_json_success(
202 array(
203 'message' => __( 'Your API keys has successfully been updated.', 'payplug' )
204 )
205 );
206 }
207
208 public function api_check_one_click_permission(WP_REST_Request $request){
209 wp_send_json_success(true);
210 }
211
212 public function api_check_bancontact_permissions(WP_REST_Request $request) {
213 $data = $request->get_params();
214
215 if($data['env']) {
216 $this->optionUnnavailableInTestMode();
217 }
218
219 $this->accountIsNotValid();
220
221 try{
222 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
223
224 } catch (PayplugException $e){
225 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
226 wp_send_json_error(array(
227 "title" => __( 'payplug_enable_feature', 'payplug' ),
228 "msg" => $e->getMessage(),
229 "close" => __( 'payplug_ok', 'payplug' )
230 ));
231 return false;
232 }
233
234 if(isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) && $account['httpResponse']['payment_methods']['bancontact']['enabled']){
235 wp_send_json_success(true);
236 }
237
238 wp_send_json_error(array(
239 "title" => __( 'payplug_enable_feature', 'payplug' ),
240 "msg" => __( 'payplug_bancontact_access_error', 'payplug' ),
241 "close" => __( 'payplug_ok', 'payplug' )
242 ));
243
244 }
245
246 public function api_check_applepay_permissions(WP_REST_Request $request) {
247 $data = $request->get_params();
248
249 if($data['env']) {
250 $this->optionUnnavailableInTestMode();
251 }
252
253 $this->accountIsNotValid();
254
255 try{
256 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
257
258 } catch (PayplugException $e){
259 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
260 wp_send_json_error(array(
261 "title" => __( 'payplug_enable_feature', 'payplug' ),
262 "msg" => $e->getMessage(),
263 "close" => __( 'payplug_ok', 'payplug' )
264 ));
265 return false;
266 }
267
268 $applepay = false;
269
270 if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) {
271 if (in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) {
272 wp_send_json_success(true);
273 }
274
275 }
276
277
278 if(!$applepay){
279 wp_send_json_error(array(
280 "title" => __( 'payplug_enable_feature', 'payplug' ),
281 "msg" => __( 'payplug_applepay_access_error', 'payplug' ),
282 "close" => __( 'payplug_ok', 'payplug' )
283 ));
284 }
285
286 }
287
288 public function api_check_american_express_permissions(WP_REST_Request $request) {
289 $data = $request->get_params();
290
291 if($data['env']) {
292 $this->optionUnnavailableInTestMode();
293 }
294
295 $this->accountIsNotValid();
296
297 try{
298 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
299
300 } catch (PayplugException $e){
301 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
302 wp_send_json_error(array(
303 "title" => __( 'payplug_enable_feature', 'payplug' ),
304 "msg" => $e->getMessage(),
305 "close" => __( 'payplug_ok', 'payplug' )
306 ));
307 return false;
308 }
309
310 if(isset($account['httpResponse']['payment_methods']['american_express']['enabled']) && $account['httpResponse']['payment_methods']['american_express']['enabled']){
311 wp_send_json_success(true);
312 }
313
314 wp_send_json_error(array(
315 "title" => __( 'payplug_enable_feature', 'payplug' ),
316 "msg" => __( 'payplug_amex_access_error', 'payplug' ),
317 "close" => __( 'payplug_ok', 'payplug' )
318 ));
319
320 }
321
322 public function api_check_oney_permissions(WP_REST_Request $request) {
323 $data = $request->get_params();
324
325 // In Test mode Oney is available
326 if($data['env']) {
327 wp_send_json_success(true);
328 return;
329 }
330
331 $this->accountIsNotValid();
332
333 // Checking in Live Mode
334 try{
335 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
336
337 } catch (PayplugException $e){
338 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
339 wp_send_json_error(["error" => $e->getMessage()]);
340 return false;
341 }
342
343 if(isset($account['httpResponse']['permissions']['can_use_oney']) && $account['httpResponse']['permissions']['can_use_oney']){
344 wp_send_json_success(true);
345 }
346
347 $oney = isset($account['httpResponse']['permissions']['can_use_oney']) ? $account['httpResponse']['permissions']['can_use_oney']: false;
348
349 if(!$oney){
350 $anchor_text = __( 'payplug_oney_error_link', 'payplug' );
351 $anchor_url = "https://portal.payplug.com/login";
352 $anchor = sprintf( ' <a href="%s" target="_blank">%s</a>', $anchor_url, $anchor_text );
353 $message = __( 'payplug_oney_error_description', 'payplug' ) . $anchor;
354 wp_send_json_error(array(
355 "title" => __( 'payplug_oney_error_title', 'payplug' ),
356 "msg" => $message,
357 "close" => __( 'payplug_ok', 'payplug' )
358 ));
359 }
360
361 wp_send_json_success(true);
362 }
363
364 public function api_check_satispay_permissions(WP_REST_Request $request) {
365 $data = $request->get_params();
366
367 if($data['env']) {
368 $this->optionUnnavailableInTestMode();
369 }
370
371 $this->accountIsNotValid();
372
373 try{
374 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
375
376 } catch (PayplugException $e){
377 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
378 wp_send_json_error(array(
379 "title" => __( 'payplug_enable_feature', 'payplug' ),
380 "msg" => $e->getMessage(),
381 "close" => __( 'payplug_ok', 'payplug' )
382 ));
383 return false;
384 }
385
386 $enabled = isset($account['httpResponse']['payment_methods']['satispay']['enabled']) ? $account['httpResponse']['payment_methods']['satispay']['enabled']: false;
387 if(!$enabled){
388 wp_send_json_error(array(
389 "title" => __( 'payplug_enable_feature', 'payplug' ),
390 "msg" => __( 'payplug_satispay_access_error', 'payplug' ),
391 "close" => __( 'payplug_ok', 'payplug' )
392 ));
393 }
394
395 wp_send_json_success($enabled);
396 }
397
398 public function api_check_mybank_permissions(WP_REST_Request $request) {
399 $data = $request->get_params();
400
401 if($data['env']) {
402 $this->optionUnnavailableInTestMode();
403 }
404
405 $this->accountIsNotValid();
406
407 try{
408 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
409
410 } catch (PayplugException $e){
411 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
412 wp_send_json_error(array(
413 "title" => __( 'payplug_enable_feature', 'payplug' ),
414 "msg" => $e->getMessage(),
415 "close" => __( 'payplug_ok', 'payplug' )
416 ));
417 return false;
418 }
419
420 $enabled = isset($account['httpResponse']['payment_methods']['mybank']['enabled']) ? $account['httpResponse']['payment_methods']['mybank']['enabled']: false;
421 if(!$enabled){
422 wp_send_json_error(array(
423 "title" => __( 'payplug_enable_feature', 'payplug' ),
424 "msg" => __( 'payplug_mybank_access_error', 'payplug' ),
425 "close" => __( 'payplug_ok', 'payplug' )
426 ));
427 }
428
429 wp_send_json_success($enabled);
430 }
431
432 public function api_check_sofort_permissions(WP_REST_Request $request) {
433 $data = $request->get_params();
434
435 if($data['env']) {
436 $this->optionUnnavailableInTestMode();
437 }
438
439 $this->accountIsNotValid();
440
441 try{
442 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
443
444 } catch (PayplugException $e){
445 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
446 wp_send_json_error(array(
447 "title" => __( 'payplug_enable_feature', 'payplug' ),
448 "msg" => $e->getMessage(),
449 "close" => __( 'payplug_ok', 'payplug' )
450 ));
451 return false;
452 }
453
454 $enabled = isset($account['httpResponse']['payment_methods']['sofort']['enabled']) ? $account['httpResponse']['payment_methods']['sofort']['enabled']: false;
455 if(!$enabled){
456 wp_send_json_error(array(
457 "title" => __( 'payplug_enable_feature', 'payplug' ),
458 "msg" => __( 'payplug_sofort_access_error', 'payplug' ),
459 "close" => __( 'payplug_ok', 'payplug' )
460 ));
461 }
462
463 wp_send_json_success($enabled);
464 }
465
466 public function api_check_giropay_permissions(WP_REST_Request $request) {
467 $data = $request->get_params();
468
469 if($data['env']) {
470 $this->optionUnnavailableInTestMode();
471 }
472
473 $this->accountIsNotValid();
474
475 try{
476 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
477
478 } catch (PayplugException $e){
479 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
480 wp_send_json_error(array(
481 "title" => __( 'payplug_enable_feature', 'payplug' ),
482 "msg" => $e->getMessage(),
483 "close" => __( 'payplug_ok', 'payplug' )
484 ));
485 return false;
486 }
487
488 $enabled = isset($account['httpResponse']['payment_methods']['giropay']['enabled']) ? $account['httpResponse']['payment_methods']['giropay']['enabled']: false;
489 if(!$enabled){
490 wp_send_json_error(array(
491 "title" => __( 'payplug_enable_feature', 'payplug' ),
492 "msg" => __( 'payplug_giropay_access_error', 'payplug' ),
493 "close" => __( 'payplug_ok', 'payplug' )
494 ));
495 }
496
497 wp_send_json_success($enabled);
498 }
499
500 public function api_check_ideal_permissions(WP_REST_Request $request) {
501 $data = $request->get_params();
502
503 if($data['env']) {
504 $this->optionUnnavailableInTestMode();
505 }
506
507 $this->accountIsNotValid();
508
509 try{
510 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
511
512 } catch (PayplugException $e){
513 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
514 wp_send_json_error(array(
515 "title" => __( 'payplug_enable_feature', 'payplug' ),
516 "msg" => $e->getMessage(),
517 "close" => __( 'payplug_ok', 'payplug' )
518 ));
519 return false;
520 }
521
522 $enabled = isset($account['httpResponse']['payment_methods']['ideal']['enabled']) ? $account['httpResponse']['payment_methods']['ideal']['enabled']: false;
523 if(!$enabled){
524 wp_send_json_error(array(
525 "title" => __( 'payplug_enable_feature', 'payplug' ),
526 "msg" => __( 'payplug_ideal_access_error', 'payplug' ),
527 "close" => __( 'payplug_ok', 'payplug' )
528 ));
529 }
530
531 wp_send_json_success($enabled);
532 }
533
534 //TODO:: WTF, why repetition?
535 private function getAccount($test_mode){
536 // Checking in Live Mode
537 try{
538 // In case the account is inactive use the test key instead of live key
539 $key = $test_mode ? PayplugWoocommerceHelper::get_test_key() : PayplugWoocommerceHelper::get_live_key();
540 $account = Authentication::getAccount(new Payplug($key));
541
542 } catch (PayplugException $e){
543 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
544 wp_send_json_error(["error" => $e->getMessage()]);
545 return false;
546 }
547
548 return $account;
549 }
550
551
552 /**
553 * Update PayPlug api keys
554 *&
555
556 * @param array $keys
557 * @param PayplugGateway $payplug_gateway
558 *
559 * @return bool
560 */
561 protected function update_api_keys( $keys, $payplug_gateway ) {
562 if ( empty( $payplug_gateway->settings ) ) {
563 $payplug_gateway->init_settings();
564 }
565
566 $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
567 $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
568 if ( ! empty( $keys['live'] ) ) {
569 $payplug_gateway->settings['mode'] = 'yes';
570 }
571
572 return update_option(
573 $payplug_gateway->get_option_key(),
574 apply_filters(
575 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
576 $payplug_gateway->settings
577 ),
578 'yes'
579 );
580 }
581
582
583 /**
584 *
585 * Ajax paypal login
586 *
587 * @return JSON
588 */
589 public function payplug_login(WP_REST_Request $request) {
590
591 $data = $request->get_params();
592 $email = sanitize_email($data['payplug_email']);
593 $password = base64_decode(wp_unslash($data['payplug_password']));
594 $wp_nonce = $data['_wpnonce'];
595
596 delete_option( 'woocommerce_payplug_settings' );
597 delete_site_option( 'woocommerce_payplug_settings' );
598
599 try {
600 $response = Authentication::getKeysByLogin($email, $password);
601 if (empty($response) || !isset($response)) {
602 http_response_code(401);
603 return wp_send_json_error(array(
604 'message' => __( 'payplug_error_wrong_credentials.', 'payplug' ),
605 ));
606 }
607 $payplug = new PayplugGateway();
608 $form_fields = $payplug->get_form_fields();
609
610 $api_keys = $payplug->retrieve_user_api_keys($email, $password);
611
612 $merchant_id = isset($api_keys['test']) ? $payplug->retrieve_merchant_id($api_keys['test']) : '';
613
614 foreach ($form_fields as $key => $field) {
615 if (in_array($field['type'], ['title', 'login'])) {
616 continue;
617 }
618
619 switch ($key) {
620 case 'enabled':
621 $val = 'yes';
622 break;
623 case 'mode':
624 $val = 'no';
625 break;
626 case 'payplug_test_key':
627 $val = !empty($api_keys['test']) ? esc_attr($api_keys['test']) : null;
628 break;
629 case 'payplug_live_key':
630 $val = !empty($api_keys['live']) ? esc_attr($api_keys['live']) : null;
631 break;
632 case 'payplug_merchant_id':
633 $val = esc_attr($merchant_id);
634 break;
635 case 'email':
636 $val = esc_html($email);
637 break;
638 default:
639 $val = $payplug->get_option($key);
640 }
641
642 $data[$key] = $val;
643 }
644
645 $payplug->set_post_data($data);
646 update_option(
647 $payplug->get_option_key(),
648 apply_filters('woocommerce_settings_api_sanitized_fields_' . $payplug->id, $data)
649 );
650
651 $user = [
652 "logged" => true,
653 "email" => $email,
654 "mode" => PayplugWoocommerceHelper::check_mode() ? 0 : 1
655 ];
656 $wp = [
657 "WP" => [
658 "_wpnonce" => $wp_nonce,
659 ]
660 ];
661
662 return wp_send_json_success( ["settings" => $user + $wp] + ( new Vue )->init() );
663 } catch (HttpException $e) {
664
665 //TODO:: error handler, Authentication::getPermissionsByLogin comes here
666 http_response_code(401);
667 $error = __("payplug_error_wrong_credentials", "payplug");
668 return wp_send_json_error(array('message' => $error));
669
670 }
671 }
672
673
674 /**
675 *
676 * Ajax payplug initialisation
677 *
678 * @return JSON
679 */
680 public function payplug_init() {
681
682 $wp_nonce = wp_create_nonce();
683
684 $wp = [
685 "logged" => PayplugWoocommerceHelper::user_logged_in(),
686 "mode" => PayplugWoocommerceHelper::check_mode() ? 0 : 1,
687 "WP" => [
688 "_wpnonce" => $wp_nonce,
689 ]
690 ];
691
692 return wp_send_json_success([
693 "settings" => $wp
694 ] + ( new Vue )->init() );
695
696 }
697
698 /**
699 * @return bool|null
700 */
701 public function payplug_logout() {
702
703 $payplug = new PayplugGateway();
704
705 if (PayplugWoocommerceHelper::payplug_logout($payplug)) {
706 $wp = [
707 "logged" => PayplugWoocommerceHelper::user_logged_in(),
708 "mode" => PayplugWoocommerceHelper::check_mode() ? 0 : 1
709 ];
710
711 http_response_code(200);
712 return wp_send_json_success(array(
713 "message" => __('Successfully logged out.', 'payplug'),
714 "status" => ( new Vue )->payplug_section_status(),
715 "settings" => $wp,
716 "subscribe" => ( new Vue )->payplug_section_subscribe() // When Logging out the Status Block needs to be updated
717 ));
718 } else {
719 http_response_code(400);
720 return wp_send_json_error(__('Already logged out.', 'payplug'));
721 }
722
723 }
724
725
726 private function accountIsNotValid(){
727 $live_key = PayplugWoocommerceHelper::get_live_key();
728 if(empty($live_key)){
729 wp_send_json_error(array(
730 "title" => __( 'payplug_enable_feature', 'payplug' ),
731 "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'),
732 "close" => __( 'payplug_ok', 'payplug' )
733 ));
734 }
735 }
736
737 private function optionUnnavailableInTestMode(){
738 wp_send_json_error(array(
739 "title" => __( 'payplug_enable_feature', 'payplug' ),
740 "msg" => __( 'payplug_unavailable_testmode_description', 'payplug' )
741 ));
742 }
743
744 /**
745 *
746 * Save data from request
747 *
748 * @return null
749 */
750 public function payplug_save_data( WP_REST_Request $request ) {
751
752 $payplug = new PayplugGateway();
753
754 if ($payplug->user_logged_in()) {
755
756 $data = $request->get_params();
757 $options = get_option('woocommerce_payplug_settings', []);
758
759 $options['enabled'] = Validator::enabled($data['payplug_enable']);
760 $options['mode'] = Validator::mode($data['payplug_sandbox']);
761
762 //TODO:: add validation for mode
763 $test_mode = $options['mode'] === 'yes' ? false : true;
764
765 $options['title'] = trim(wp_strip_all_tags($data['standard_payment_title']));
766 $options['description'] = trim(wp_strip_all_tags($data['standard_payment_description']));
767 $options['payment_method'] = (Validator::payment_method($data['payplug_embeded'])) ? $data['payplug_embeded'] : $options['payplug_embeded'];
768 $options['oneclick'] = Validator::oneclick($data['enable_one_click']);
769
770 $options['oney'] = Validator::oney($data['enable_oney']);
771 $options['bancontact'] = Validator::genericPaymentGateway($data['enable_bancontact'], "Bancontact", $test_mode);
772 $options['apple_pay'] = Validator::genericPaymentGateway($data['enable_applepay'], "Apple Pay", $test_mode);
773 $options['american_express'] = Validator::genericPaymentGateway($data['enable_american_express'],"American Express", $test_mode);
774 $options['satispay'] = Validator::genericPaymentGateway($data['enable_satispay'], "Satispay", $test_mode);
775 $options['sofort'] = Validator::genericPaymentGateway($data['enable_sofort'], "Sofort", $test_mode);
776 $options['ideal'] = Validator::genericPaymentGateway($data['enable_ideal'], "iDEAL", $test_mode);
777 $options['mybank'] = Validator::genericPaymentGateway($data['enable_mybank'], "Mybank", $test_mode);
778 $options['giropay'] = Validator::genericPaymentGateway($data['enable_giropay'], "Giropay", $test_mode);
779
780 //TODO:: add validation for oney -> needed to add modal for error msg
781
782 $options['oney_type'] = (Validator::oney_type($data['payplug_oney'])) ? $data['payplug_oney'] : 'with_fees';
783 $thresholds = (Validator::oney_thresholds($data['oney_min_amounts'], $data['oney_max_amounts']));
784 $options['oney_thresholds_min'] = $thresholds['min'];
785 $options['oney_thresholds_max'] = $thresholds['max'];
786 $options['oney_product_animation'] = Validator::oney_product_animation($data['enable_oney_product_animation']);
787 $options['debug'] = Validator::debug($data['enable_debug']);
788
789 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
790 http_response_code(200);
791
792 //TODO:: do we need this?
793 $account = $this->getAccount($test_mode);
794 PayplugWoocommerceHelper::set_transient_data($account);
795
796 wp_send_json_success( array(
797 "title" => null,
798 "msg" => __( 'payplug_save_success_message', 'payplug' ),
799 "close" => __( 'payplug_ok', 'payplug' )
800 ));
801 } else {
802 http_response_code(403);
803 wp_send_json_error("You are not logged in !");
804 }
805
806
807 }
808
809 public function payplug_check_requirements() {
810 wp_send_json_success(array(
811 "status" => ( new Vue )->payplug_section_status()
812 ));
813 }
814
815 public function check_integrated_payment($live_key) {
816 try{
817 $account = Authentication::getAccount(new Payplug($live_key));
818
819 } catch (PayplugException $e){
820 PayplugGateway::log('Error while fetching account : ' . $e->getMessage(), 'error');
821 wp_send_json_error(["error" => $e->getMessage()]);
822 return false;
823 }
824
825 if(isset($account['httpResponse']['permissions']['can_use_integrated_payments']) && ($account['httpResponse']['permissions']['can_use_integrated_payments'] == true)){
826 return true;
827 }
828
829 return false;
830 }
831
832 public function api_check_integrated_payment(WP_REST_Request $request)
833 {
834 $data = $request->get_params();
835
836 if($data['env']) {
837 wp_send_json_success(true);
838 return;
839 }
840
841 $this->accountIsNotValid();
842
843 try{
844 $account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key()));
845
846 } catch (PayplugException $e){
847 PayplugGateway::log('Error while getting account data : ' . $e->getMessage(), 'error');
848 wp_send_json_error(array(
849 "title" => __( 'payplug_enable_feature', 'payplug' ),
850 "msg" => $e->getMessage(),
851 "close" => __( 'payplug_ok', 'payplug' )
852 ));
853 return false;
854 }
855
856 if( ! isset($account['httpResponse']['permissions']['can_use_integrated_payments'])
857 || ! $account['httpResponse']['permissions']['can_use_integrated_payments'] ) {
858 wp_send_json_error(array(
859 "title" => __( 'payplug_enable_feature', 'payplug' ),
860 "msg" => __( 'payplug_integrated_access_error', 'payplug' ),
861 "close" => __( 'payplug_ok', 'payplug' )
862 ));
863 }
864
865 wp_send_json_success(true);
866 return true;
867
868 }
869 }
870