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

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