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

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

155 lines 4.1 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\Payplug;
7 use Payplug\Authentication;
8 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
9 use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
10 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
11 use Payplug\Exception\PayplugException;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * PayPlug admin ajax handler.
19 *
20 * @package Payplug\PayplugWoocommerce\Admin
21 */
22 class Ajax {
23
24 /**
25 * @var PayplugPermissions
26 */
27 private $permissions;
28
29 const REFRESH_KEY_ACTION = 'payplug_refresh_keys';
30 const CHECK_LIVE_PERMISSIONS = 'check_live_permissions';
31
32 public function __construct() {
33 add_action( 'wp_ajax_' . self::REFRESH_KEY_ACTION, [ $this, 'handle_refresh_keys' ] );
34 add_action( 'wp_ajax_' . self::CHECK_LIVE_PERMISSIONS, [ $this, 'check_live_permissions' ] );
35 }
36
37 public function handle_refresh_keys() {
38
39 if ( empty( $_POST['_wpnonce'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) ) {
40 wp_send_json_error(
41 array(
42 'message' => __( 'Invalid request.', 'payplug' ),
43 )
44 );
45 }
46
47 $action = sprintf( '%s_%s', wp_unslash( $_POST['email'] ), self::REFRESH_KEY_ACTION );
48 if ( ! check_ajax_referer( $action ) ) {
49 wp_send_json_error(
50 array(
51 'message' => __( 'Invalid nonce.', 'payplug' ),
52 )
53 );
54 }
55
56 $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
57 $password = sanitize_text_field( wp_unslash( $_POST['password'] ) );
58
59 if ( ! WC()->payment_gateways() ) {
60 wp_send_json_error(
61 array(
62 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
63 )
64 );
65 }
66
67 $payment_gateways = WC()->payment_gateways()->payment_gateways();
68 if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) {
69 wp_send_json_error(
70 array(
71 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
72 )
73 );
74 }
75
76 /* @var PayplugGateway $payplug_gateway */
77 $payplug_gateway = $payment_gateways['payplug'];
78 $keys = $payplug_gateway->retrieve_user_api_keys( $email, $password );
79 if ( is_wp_error( $keys ) ) {
80 wp_send_json_error(
81 array(
82 'message' => $keys->get_error_message(),
83 )
84 );
85 }
86
87 $success = $this->update_api_keys( $keys, $payplug_gateway );
88
89 if ( empty( $keys['live'] ) ) {
90 wp_send_json_error(
91 array(
92 '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' ),
93 )
94 );
95 }
96
97 if ( ! $success ) {
98 wp_send_json_error(
99 array(
100 'message' => __( 'Something went wrong.', 'payplug' ),
101 )
102 );
103 }
104
105 wp_send_json_success(
106 array(
107 'message' => __( 'Your API keys has successfully been updated.', 'payplug' )
108 )
109 );
110 }
111
112 public function check_live_permissions() {
113 try{
114 $account = Authentication::getAccount(new Payplug($_POST['livekey']));
115 } catch (PayplugException $e){
116 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
117 wp_send_json_error(["error" => $e->getMessage()]);
118 return false;
119 }
120 PayplugWoocommerceHelper::set_transient_data($account);
121 $permissions = $account['httpResponse']['permissions'];
122 wp_send_json_success($permissions);
123 }
124
125 /**
126 * Update PayPlug api keys
127 *&
128
129 * @param array $keys
130 * @param PayplugGateway $payplug_gateway
131 *
132 * @return bool
133 */
134 protected function update_api_keys( $keys, $payplug_gateway ) {
135 if ( empty( $payplug_gateway->settings ) ) {
136 $payplug_gateway->init_settings();
137 }
138
139 $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
140 $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
141 if ( ! empty( $keys['live'] ) ) {
142 $payplug_gateway->settings['mode'] = 'yes';
143 }
144
145 return update_option(
146 $payplug_gateway->get_option_key(),
147 apply_filters(
148 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
149 $payplug_gateway->settings
150 ),
151 'yes'
152 );
153 }
154 }
155