PluginProbe
PayPlug for WooCommerce (Official) / 1.2.5
PayPlug for WooCommerce (Official) v1.2.5
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.2.5, at src/Admin/Ajax.php

146 lines 3.7 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
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * PayPlug admin ajax handler.
17 *
18 * @package Payplug\PayplugWoocommerce\Admin
19 */
20 class Ajax {
21
22 /**
23 * @var PayplugPermissions
24 */
25 private $permissions;
26
27 const REFRESH_KEY_ACTION = 'payplug_refresh_keys';
28 const CHECK_LIVE_PERMISSIONS = 'check_live_permissions';
29
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 }
34
35 public function handle_refresh_keys() {
36
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 }
44
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 }
53
54 $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
55 $password = sanitize_text_field( wp_unslash( $_POST['password'] ) );
56
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 }
64
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 }
73
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 }
84
85 $success = $this->update_api_keys( $keys, $payplug_gateway );
86
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 }
94
95 if ( ! $success ) {
96 wp_send_json_error(
97 array(
98 'message' => __( 'Something went wrong.', 'payplug' ),
99 )
100 );
101 }
102
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 }
115
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 }
129
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 }
135
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 }
145 }
146