PluginProbe
PayPlug for WooCommerce (Official) / 1.0.17
PayPlug for WooCommerce (Official) v1.0.17
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
payplug / src / Admin / Ajax.php

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

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