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

213 lines 6.5 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 const CHECK_BANCONTACT_PERMISSIONS = 'check_bancontact_permissions';
32 const CHECK_APPLEPAY_PERMISSIONS = 'check_applepay_permissions';
33 const CHECK_AMERICAN_EXPRESS_PERMISSIONS = 'check_american_express_permissions';
34
35 public function __construct() {
36 add_action( 'wp_ajax_' . self::REFRESH_KEY_ACTION, [ $this, 'handle_refresh_keys' ] );
37 add_action( 'wp_ajax_' . self::CHECK_LIVE_PERMISSIONS, [ $this, 'check_live_permissions' ] );
38 add_action( 'wp_ajax_' . self::CHECK_BANCONTACT_PERMISSIONS, [ $this, 'check_bancontact_permissions' ] );
39 add_action( 'wp_ajax_' . self::CHECK_APPLEPAY_PERMISSIONS, [ $this, 'check_applepay_permissions' ] );
40 add_action( 'wp_ajax_' . self::CHECK_AMERICAN_EXPRESS_PERMISSIONS, [ $this, 'check_american_express_permissions' ] );
41 }
42
43 public function handle_refresh_keys() {
44
45 if ( empty( $_POST['_wpnonce'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) ) {
46 wp_send_json_error(
47 array(
48 'message' => __( 'Invalid request.', 'payplug' ),
49 )
50 );
51 }
52
53 $action = sprintf( '%s_%s', wp_unslash( $_POST['email'] ), self::REFRESH_KEY_ACTION );
54 if ( ! check_ajax_referer( $action ) ) {
55 wp_send_json_error(
56 array(
57 'message' => __( 'Invalid nonce.', 'payplug' ),
58 )
59 );
60 }
61
62 $email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
63 $password = sanitize_text_field( wp_unslash( $_POST['password'] ) );
64
65 if ( ! WC()->payment_gateways() ) {
66 wp_send_json_error(
67 array(
68 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
69 )
70 );
71 }
72
73 $payment_gateways = WC()->payment_gateways()->payment_gateways();
74 if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) {
75 wp_send_json_error(
76 array(
77 'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ),
78 )
79 );
80 }
81
82 /* @var PayplugGateway $payplug_gateway */
83 $payplug_gateway = $payment_gateways['payplug'];
84 $keys = $payplug_gateway->retrieve_user_api_keys( $email, $password );
85 if ( is_wp_error( $keys ) ) {
86 wp_send_json_error(
87 array(
88 'message' => $keys->get_error_message(),
89 )
90 );
91 }
92
93 $success = $this->update_api_keys( $keys, $payplug_gateway );
94
95 if ( empty( $keys['live'] ) ) {
96 wp_send_json_error(
97 array(
98 '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' ),
99 )
100 );
101 }
102
103 if ( ! $success ) {
104 wp_send_json_error(
105 array(
106 'message' => __( 'Something went wrong.', 'payplug' ),
107 )
108 );
109 }
110
111 wp_send_json_success(
112 array(
113 'message' => __( 'Your API keys has successfully been updated.', 'payplug' )
114 )
115 );
116 }
117
118 public function check_live_permissions() {
119 try{
120 $account = Authentication::getAccount(new Payplug($_POST['livekey']));
121 } catch (PayplugException $e){
122 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
123 wp_send_json_error(["error" => $e->getMessage()]);
124 return false;
125 }
126 PayplugWoocommerceHelper::set_transient_data($account);
127 $permissions = $account['httpResponse']['permissions'];
128 wp_send_json_success($permissions);
129 }
130
131 public function check_bancontact_permissions() {
132 try{
133 $account = Authentication::getAccount(new Payplug($_POST['livekey']));
134
135 } catch (PayplugException $e){
136 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
137 wp_send_json_error(["error" => $e->getMessage()]);
138 return false;
139 }
140
141 PayplugWoocommerceHelper::set_transient_data($account);
142 $bancontact = isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) ? $account['httpResponse']['payment_methods']['bancontact']['enabled']: false;
143 wp_send_json_success($bancontact);
144 }
145
146 public function check_applepay_permissions() {
147 try{
148 $account = Authentication::getAccount(new Payplug($_POST['livekey']));
149
150 } catch (PayplugException $e){
151 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
152 wp_send_json_error(["error" => $e->getMessage()]);
153 return false;
154 }
155
156 PayplugWoocommerceHelper::set_transient_data($account);
157 $applepay = false;
158
159 if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) {
160 if (in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) {
161 $applepay = true;
162 }
163
164 }
165 wp_send_json_success($applepay);
166 }
167
168 public function check_american_express_permissions() {
169 try{
170 $account = Authentication::getAccount(new Payplug($_POST['livekey']));
171
172 } catch (PayplugException $e){
173 PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
174 wp_send_json_error(["error" => $e->getMessage()]);
175 return false;
176 }
177
178 PayplugWoocommerceHelper::set_transient_data($account);
179 $amex = isset($account['httpResponse']['payment_methods']['american_express']['enabled']) ? $account['httpResponse']['payment_methods']['american_express']['enabled']: false;
180 wp_send_json_success($amex);
181 }
182
183 /**
184 * Update PayPlug api keys
185 *&
186
187 * @param array $keys
188 * @param PayplugGateway $payplug_gateway
189 *
190 * @return bool
191 */
192 protected function update_api_keys( $keys, $payplug_gateway ) {
193 if ( empty( $payplug_gateway->settings ) ) {
194 $payplug_gateway->init_settings();
195 }
196
197 $payplug_gateway->settings['payplug_test_key'] = $keys['test'];
198 $payplug_gateway->settings['payplug_live_key'] = $keys['live'];
199 if ( ! empty( $keys['live'] ) ) {
200 $payplug_gateway->settings['mode'] = 'yes';
201 }
202
203 return update_option(
204 $payplug_gateway->get_option_key(),
205 apply_filters(
206 'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id,
207 $payplug_gateway->settings
208 ),
209 'yes'
210 );
211 }
212 }
213