| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Admin; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
use Payplug\Exception\HttpException; |
| 7 |
use Payplug\Payplug; |
| 8 |
use Payplug\Authentication; |
| 9 |
use Payplug\PayplugWoocommerce\Admin\Vue; |
| 10 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 11 |
use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions; |
| 12 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 13 |
use Payplug\Exception\PayplugException; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* PayPlug admin ajax handler. |
| 22 |
* |
| 23 |
* @package Payplug\PayplugWoocommerce\Admin |
| 24 |
*/ |
| 25 |
class Ajax { |
| 26 |
|
| 27 |
/** |
| 28 |
* @var PayplugPermissions |
| 29 |
*/ |
| 30 |
private $permissions; |
| 31 |
|
| 32 |
const CHECK_LIVE_PERMISSIONS = 'check_live_permissions'; |
| 33 |
const CHECK_BANCONTACT_PERMISSIONS = 'check_bancontact_permissions'; |
| 34 |
const CHECK_APPLEPAY_PERMISSIONS = 'check_applepay_permissions'; |
| 35 |
const CHECK_AMERICAN_EXPRESS_PERMISSIONS = 'check_american_express_permissions'; |
| 36 |
|
| 37 |
public function __construct() { |
| 38 |
add_action( 'wp_ajax_' . self::CHECK_LIVE_PERMISSIONS, [ $this, 'check_live_permissions' ] ); |
| 39 |
add_action( 'wp_ajax_' . self::CHECK_BANCONTACT_PERMISSIONS, [ $this, 'check_bancontact_permissions' ] ); |
| 40 |
add_action( 'wp_ajax_' . self::CHECK_APPLEPAY_PERMISSIONS, [ $this, 'check_applepay_permissions' ] ); |
| 41 |
add_action( 'wp_ajax_' . self::CHECK_AMERICAN_EXPRESS_PERMISSIONS, [ $this, 'check_american_express_permissions' ] ); |
| 42 |
add_action( 'rest_api_init', function () { |
| 43 |
//Path to REST route and the callback function |
| 44 |
register_rest_route( 'payplug_api', '/save/', array( |
| 45 |
'methods' => 'POST', |
| 46 |
'callback' => [ $this, 'payplug_save_data' ], |
| 47 |
'permission_callback' => '__return_true' |
| 48 |
) ); |
| 49 |
register_rest_route( 'payplug_api', '/init/', array( |
| 50 |
'methods' => 'POST', |
| 51 |
'callback' => [ $this, 'payplug_init' ], |
| 52 |
'permission_callback' => '__return_true' |
| 53 |
) ); |
| 54 |
register_rest_route( 'payplug_api', '/login/', array( |
| 55 |
'methods' => 'POST', |
| 56 |
'callback' => [ $this, 'payplug_login' ], |
| 57 |
'permission_callback' => '__return_true' |
| 58 |
) ); |
| 59 |
register_rest_route( 'payplug_api', '/logout/', array( |
| 60 |
'methods' => 'POST', |
| 61 |
'callback' => [ $this, 'payplug_logout' ], |
| 62 |
'permission_callback' => '__return_true' |
| 63 |
) ); |
| 64 |
register_rest_route( 'payplug_api', '/refresh_keys/', array( |
| 65 |
'methods' => 'POST', |
| 66 |
'callback' => [ $this, 'refresh_keys' ], |
| 67 |
'permission_callback' => '__return_true' |
| 68 |
) ); |
| 69 |
register_rest_route( 'payplug_api', '/check_requirements/', array( |
| 70 |
'methods' => 'POST', |
| 71 |
'callback' => [ $this, 'payplug_check_requirements' ], |
| 72 |
'permission_callback' => '__return_true' |
| 73 |
) ); |
| 74 |
register_rest_route( 'payplug_api', '/bancontact_permissions/', array( |
| 75 |
'methods' => 'POST', |
| 76 |
'callback' => [ $this, 'api_check_bancontact_permissions' ], |
| 77 |
'permission_callback' => '__return_true' |
| 78 |
) ); |
| 79 |
register_rest_route( 'payplug_api', '/applepay_permissions/', array( |
| 80 |
'methods' => 'POST', |
| 81 |
'callback' => [ $this, 'api_check_applepay_permissions' ], |
| 82 |
'permission_callback' => '__return_true' |
| 83 |
) ); |
| 84 |
register_rest_route( 'payplug_api', '/american_express_permissions/', array( |
| 85 |
'methods' => 'POST', |
| 86 |
'callback' => [ $this, 'api_check_american_express_permissions' ], |
| 87 |
'permission_callback' => '__return_true' |
| 88 |
) ); |
| 89 |
register_rest_route( 'payplug_api', '/oney_permissions/', array( |
| 90 |
'methods' => 'POST', |
| 91 |
'callback' => [ $this, 'api_check_oney_permissions' ], |
| 92 |
'permission_callback' => '__return_true' |
| 93 |
) ); |
| 94 |
}); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
public function refresh_keys(WP_REST_Request $request) { |
| 99 |
$data = $request->get_params(); |
| 100 |
$email = sanitize_text_field( wp_unslash( $data['payplug_email'] ) ); |
| 101 |
$password = sanitize_text_field( wp_unslash( $data['payplug_password'] ) ); |
| 102 |
|
| 103 |
if ( empty( $email ) || empty( $password ) ) { |
| 104 |
wp_send_json_error( |
| 105 |
array( |
| 106 |
'message' => __( 'Invalid request.', 'payplug' ), |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! WC()->payment_gateways() ) { |
| 112 |
wp_send_json_error( |
| 113 |
array( |
| 114 |
'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ), |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
$payment_gateways = WC()->payment_gateways()->payment_gateways(); |
| 120 |
if ( empty( $payment_gateways ) || ! isset( $payment_gateways['payplug'] ) ) { |
| 121 |
wp_send_json_error( |
| 122 |
array( |
| 123 |
'message' => __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ), |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/* @var PayplugGateway $payplug_gateway */ |
| 129 |
$payplug_gateway = $payment_gateways['payplug']; |
| 130 |
$keys = $payplug_gateway->retrieve_user_api_keys( $email, $password ); |
| 131 |
if ( is_wp_error( $keys ) ) { |
| 132 |
wp_send_json_error( |
| 133 |
array( |
| 134 |
'message' => $keys->get_error_message(), |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
$success = $this->update_api_keys( $keys, $payplug_gateway ); |
| 140 |
|
| 141 |
if ( empty( $keys['live'] ) ) { |
| 142 |
wp_send_json_error( |
| 143 |
array( |
| 144 |
'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' ), |
| 145 |
'still_inactive' => true |
| 146 |
) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
if ( ! $success ) { |
| 151 |
wp_send_json_error( |
| 152 |
array( |
| 153 |
'message' => __( 'Something went wrong.', 'payplug' ), |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
wp_send_json_success( |
| 159 |
array( |
| 160 |
'message' => __( 'Your API keys has successfully been updated.', 'payplug' ) |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
public function check_live_permissions() { |
| 166 |
try{ |
| 167 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 168 |
} catch (PayplugException $e){ |
| 169 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 170 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 171 |
return false; |
| 172 |
} |
| 173 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 174 |
$permissions = $account['httpResponse']['permissions']; |
| 175 |
wp_send_json_success($permissions); |
| 176 |
} |
| 177 |
|
| 178 |
public function api_check_bancontact_permissions(WP_REST_Request $request) { |
| 179 |
$data = $request->get_params(); |
| 180 |
|
| 181 |
if($data['env']) { |
| 182 |
$this->optionUnnavailableInTestMode(); |
| 183 |
} |
| 184 |
|
| 185 |
$this->accountIsNotValid(); |
| 186 |
|
| 187 |
try{ |
| 188 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 189 |
|
| 190 |
} catch (PayplugException $e){ |
| 191 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 192 |
wp_send_json_error(array( |
| 193 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 194 |
"msg" => $e->getMessage(), |
| 195 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 196 |
)); |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 201 |
|
| 202 |
if(isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) && $account['httpResponse']['payment_methods']['bancontact']['enabled']){ |
| 203 |
wp_send_json_success(true); |
| 204 |
} |
| 205 |
|
| 206 |
wp_send_json_error(array( |
| 207 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 208 |
"msg" => __( 'payplug_bancontact_access_error', 'payplug' ), |
| 209 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 210 |
)); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
public function api_check_applepay_permissions(WP_REST_Request $request) { |
| 215 |
$data = $request->get_params(); |
| 216 |
|
| 217 |
if($data['env']) { |
| 218 |
$this->optionUnnavailableInTestMode(); |
| 219 |
} |
| 220 |
|
| 221 |
$this->accountIsNotValid(); |
| 222 |
|
| 223 |
try{ |
| 224 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 225 |
|
| 226 |
} catch (PayplugException $e){ |
| 227 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 228 |
wp_send_json_error(array( |
| 229 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 230 |
"msg" => $e->getMessage(), |
| 231 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 232 |
)); |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 237 |
$applepay = false; |
| 238 |
|
| 239 |
if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) { |
| 240 |
if (in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) { |
| 241 |
wp_send_json_success(true); |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if(!$applepay){ |
| 248 |
wp_send_json_error(array( |
| 249 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 250 |
"msg" => __( 'payplug_applepay_access_error', 'payplug' ), |
| 251 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 252 |
)); |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
public function api_check_american_express_permissions(WP_REST_Request $request) { |
| 258 |
$data = $request->get_params(); |
| 259 |
|
| 260 |
if($data['env']) { |
| 261 |
$this->optionUnnavailableInTestMode(); |
| 262 |
} |
| 263 |
|
| 264 |
$this->accountIsNotValid(); |
| 265 |
|
| 266 |
try{ |
| 267 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 268 |
|
| 269 |
} catch (PayplugException $e){ |
| 270 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 271 |
wp_send_json_error(array( |
| 272 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 273 |
"msg" => $e->getMessage(), |
| 274 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 275 |
)); |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 280 |
|
| 281 |
if(isset($account['httpResponse']['payment_methods']['american_express']['enabled']) && $account['httpResponse']['payment_methods']['american_express']['enabled']){ |
| 282 |
wp_send_json_success(true); |
| 283 |
} |
| 284 |
|
| 285 |
$amex = isset($account['httpResponse']['payment_methods']['american_express']['enabled']) ? $account['httpResponse']['payment_methods']['american_express']['enabled']: false; |
| 286 |
|
| 287 |
if(!$amex){ |
| 288 |
wp_send_json_error(array( |
| 289 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 290 |
"msg" => __( 'payplug_amex_access_error', 'payplug' ), |
| 291 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 292 |
)); |
| 293 |
} |
| 294 |
|
| 295 |
wp_send_json_success($amex); |
| 296 |
} |
| 297 |
|
| 298 |
public function api_check_oney_permissions(WP_REST_Request $request) { |
| 299 |
$data = $request->get_params(); |
| 300 |
|
| 301 |
// In Test mode Oney is available |
| 302 |
if($data['env']) { |
| 303 |
wp_send_json_success(true); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
$this->accountIsNotValid(); |
| 308 |
|
| 309 |
// Checking in Live Mode |
| 310 |
try{ |
| 311 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 312 |
|
| 313 |
} catch (PayplugException $e){ |
| 314 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 315 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 320 |
|
| 321 |
if(isset($account['httpResponse']['permissions']['can_use_oney']) && $account['httpResponse']['permissions']['can_use_oney']){ |
| 322 |
wp_send_json_success(true); |
| 323 |
} |
| 324 |
|
| 325 |
$oney = isset($account['httpResponse']['permissions']['can_use_oney']) ? $account['httpResponse']['permissions']['can_use_oney']: false; |
| 326 |
|
| 327 |
if(!$oney){ |
| 328 |
$anchor_text = __( 'payplug_oney_error_link', 'payplug' ); |
| 329 |
$anchor_url = "https://portal.payplug.com/login"; |
| 330 |
$anchor = sprintf( ' <a href="%s" target="_blank">%s</a>', $anchor_url, $anchor_text ); |
| 331 |
$message = __( 'payplug_oney_error_description', 'payplug' ) . $anchor; |
| 332 |
wp_send_json_error(array( |
| 333 |
"title" => __( 'payplug_oney_error_title', 'payplug' ), |
| 334 |
"msg" => $message, |
| 335 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 336 |
)); |
| 337 |
} |
| 338 |
|
| 339 |
wp_send_json_success(true); |
| 340 |
} |
| 341 |
|
| 342 |
private function getAccount(){ |
| 343 |
// Checking in Live Mode |
| 344 |
try{ |
| 345 |
// In case the account is inactive use the test key instead of live key |
| 346 |
$key = PayplugWoocommerceHelper::get_live_key() ? PayplugWoocommerceHelper::get_live_key() : PayplugWoocommerceHelper::get_test_key(); |
| 347 |
$account = Authentication::getAccount(new Payplug($key)); |
| 348 |
|
| 349 |
} catch (PayplugException $e){ |
| 350 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 351 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
return $account; |
| 356 |
} |
| 357 |
|
| 358 |
private function check_oney($account, $test_mode){ |
| 359 |
|
| 360 |
if($test_mode) |
| 361 |
return true; |
| 362 |
|
| 363 |
$this->accountIsNotValid(); |
| 364 |
|
| 365 |
if(isset($account['httpResponse']['permissions']['can_use_oney']) && $account['httpResponse']['permissions']['can_use_oney']){ |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
$oney = isset($account['httpResponse']['permissions']['can_use_oney']) ? $account['httpResponse']['permissions']['can_use_oney']: false; |
| 370 |
|
| 371 |
if(!$oney){ |
| 372 |
$anchor_text = __( 'payplug_oney_error_link', 'payplug' ); |
| 373 |
$anchor_url = "https://portal.payplug.com/login"; |
| 374 |
$anchor = sprintf( ' <a href="%s" target="_blank">%s</a>', $anchor_url, $anchor_text ); |
| 375 |
$message = __( 'payplug_oney_error_description', 'payplug' ) . $anchor; |
| 376 |
return array( |
| 377 |
"title" => __( 'payplug_oney_error_title', 'payplug' ), |
| 378 |
"msg" => $message, |
| 379 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
return true; |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
public function check_bancontact_permissions() { |
| 388 |
try{ |
| 389 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 390 |
|
| 391 |
} catch (PayplugException $e){ |
| 392 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 393 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 398 |
$bancontact = isset($account['httpResponse']['payment_methods']['bancontact']['enabled']) ? $account['httpResponse']['payment_methods']['bancontact']['enabled']: false; |
| 399 |
wp_send_json_success($bancontact); |
| 400 |
} |
| 401 |
|
| 402 |
public function check_applepay_permissions() { |
| 403 |
try{ |
| 404 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 405 |
|
| 406 |
} catch (PayplugException $e){ |
| 407 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 408 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 413 |
$applepay = false; |
| 414 |
|
| 415 |
if ($account['httpResponse']['payment_methods']['apple_pay']['enabled']) { |
| 416 |
if (in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['httpResponse']['payment_methods']['apple_pay']['allowed_domain_names'])) { |
| 417 |
$applepay = true; |
| 418 |
} |
| 419 |
|
| 420 |
} |
| 421 |
wp_send_json_success($applepay); |
| 422 |
} |
| 423 |
|
| 424 |
public function check_american_express_permissions() { |
| 425 |
try{ |
| 426 |
$account = Authentication::getAccount(new Payplug(PayplugWoocommerceHelper::get_live_key())); |
| 427 |
|
| 428 |
} catch (PayplugException $e){ |
| 429 |
PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error'); |
| 430 |
wp_send_json_error(["error" => $e->getMessage()]); |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 435 |
$amex = isset($account['httpResponse']['payment_methods']['american_express']['enabled']) ? $account['httpResponse']['payment_methods']['american_express']['enabled']: false; |
| 436 |
wp_send_json_success($amex); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Update PayPlug api keys |
| 441 |
*& |
| 442 |
|
| 443 |
* @param array $keys |
| 444 |
* @param PayplugGateway $payplug_gateway |
| 445 |
* |
| 446 |
* @return bool |
| 447 |
*/ |
| 448 |
protected function update_api_keys( $keys, $payplug_gateway ) { |
| 449 |
if ( empty( $payplug_gateway->settings ) ) { |
| 450 |
$payplug_gateway->init_settings(); |
| 451 |
} |
| 452 |
|
| 453 |
$payplug_gateway->settings['payplug_test_key'] = $keys['test']; |
| 454 |
$payplug_gateway->settings['payplug_live_key'] = $keys['live']; |
| 455 |
if ( ! empty( $keys['live'] ) ) { |
| 456 |
$payplug_gateway->settings['mode'] = 'yes'; |
| 457 |
} |
| 458 |
|
| 459 |
return update_option( |
| 460 |
$payplug_gateway->get_option_key(), |
| 461 |
apply_filters( |
| 462 |
'woocommerce_settings_api_sanitized_fields_' . $payplug_gateway->id, |
| 463 |
$payplug_gateway->settings |
| 464 |
), |
| 465 |
'yes' |
| 466 |
); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
/** |
| 471 |
* |
| 472 |
* Ajax paypal login |
| 473 |
* |
| 474 |
* @return JSON |
| 475 |
*/ |
| 476 |
public function payplug_login(WP_REST_Request $request) { |
| 477 |
|
| 478 |
$data = $request->get_params(); |
| 479 |
$email = sanitize_email($data['payplug_email']); |
| 480 |
$password = wp_unslash($data['payplug_password']); |
| 481 |
$wp_nonce = $data['_wpnonce']; |
| 482 |
|
| 483 |
|
| 484 |
try { |
| 485 |
$response = Authentication::getKeysByLogin($email, $password); |
| 486 |
if (empty($response) || !isset($response)) { |
| 487 |
http_response_code(401); |
| 488 |
return wp_send_json_error(array( |
| 489 |
'message' => __( 'payplug_error_wrong_credentials.', 'payplug' ), |
| 490 |
)); |
| 491 |
} |
| 492 |
$payplug = new PayplugGateway(); |
| 493 |
$form_fields = $payplug->get_form_fields(); |
| 494 |
|
| 495 |
$api_keys = $payplug->retrieve_user_api_keys($email, $password); |
| 496 |
|
| 497 |
$merchant_id = isset($api_keys['test']) ? $payplug->retrieve_merchant_id($api_keys['test']) : ''; |
| 498 |
|
| 499 |
foreach ($form_fields as $key => $field) { |
| 500 |
if (in_array($field['type'], ['title', 'login'])) { |
| 501 |
continue; |
| 502 |
} |
| 503 |
|
| 504 |
switch ($key) { |
| 505 |
case 'enabled': |
| 506 |
$val = 'yes'; |
| 507 |
break; |
| 508 |
case 'mode': |
| 509 |
$val = 'no'; |
| 510 |
break; |
| 511 |
case 'payplug_test_key': |
| 512 |
$val = !empty($api_keys['test']) ? esc_attr($api_keys['test']) : null; |
| 513 |
break; |
| 514 |
case 'payplug_live_key': |
| 515 |
$val = !empty($api_keys['live']) ? esc_attr($api_keys['live']) : null; |
| 516 |
break; |
| 517 |
case 'payplug_merchant_id': |
| 518 |
$val = esc_attr($merchant_id); |
| 519 |
break; |
| 520 |
case 'email': |
| 521 |
$val = esc_html($email); |
| 522 |
break; |
| 523 |
default: |
| 524 |
$val = $payplug->get_option($key); |
| 525 |
} |
| 526 |
|
| 527 |
$data[$key] = $val; |
| 528 |
} |
| 529 |
|
| 530 |
$payplug->set_post_data($data); |
| 531 |
update_option( |
| 532 |
$payplug->get_option_key(), |
| 533 |
apply_filters('woocommerce_settings_api_sanitized_fields_' . $payplug->id, $data) |
| 534 |
); |
| 535 |
|
| 536 |
$user = [ |
| 537 |
"logged" => true, |
| 538 |
"email" => $email, |
| 539 |
"mode" => PayplugWoocommerceHelper::check_mode() |
| 540 |
]; |
| 541 |
$wp = [ |
| 542 |
"WP" => [ |
| 543 |
"_wpnonce" => $wp_nonce, |
| 544 |
] |
| 545 |
]; |
| 546 |
|
| 547 |
return wp_send_json_success( ["settings" => $user + $response + $wp] + ( new Vue )->init() ); |
| 548 |
} catch (HttpException $e) { |
| 549 |
|
| 550 |
//TODO:: error handler, Authentication::getPermissionsByLogin comes here |
| 551 |
http_response_code(401); |
| 552 |
$error = __("payplug_error_wrong_credentials", "payplug"); |
| 553 |
return wp_send_json_error(array('message' => $error)); |
| 554 |
|
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
/** |
| 560 |
* |
| 561 |
* Ajax payplug initialisation |
| 562 |
* |
| 563 |
* @return JSON |
| 564 |
*/ |
| 565 |
public function payplug_init() { |
| 566 |
|
| 567 |
$wp_nonce = wp_create_nonce(); |
| 568 |
|
| 569 |
$wp = [ |
| 570 |
"logged" => PayplugWoocommerceHelper::user_logged_in(), |
| 571 |
"mode" => PayplugWoocommerceHelper::check_mode() ? 0 : 1, |
| 572 |
"WP" => [ |
| 573 |
"_wpnonce" => $wp_nonce, |
| 574 |
] |
| 575 |
]; |
| 576 |
|
| 577 |
return wp_send_json_success([ |
| 578 |
"settings" => $wp |
| 579 |
] + ( new Vue )->init() ); |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* @return bool|null |
| 585 |
*/ |
| 586 |
public function payplug_logout() { |
| 587 |
|
| 588 |
$payplug = new PayplugGateway(); |
| 589 |
|
| 590 |
if (PayplugWoocommerceHelper::payplug_logout($payplug)) { |
| 591 |
http_response_code(200); |
| 592 |
return wp_send_json_success(__('Successfully logged out.', 'payplug')); |
| 593 |
} else { |
| 594 |
http_response_code(400); |
| 595 |
return wp_send_json_error(__('Already logged out.', 'payplug')); |
| 596 |
} |
| 597 |
|
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
private function accountIsNotValid(){ |
| 602 |
$live_key = PayplugWoocommerceHelper::get_live_key(); |
| 603 |
if(empty($live_key)){ |
| 604 |
wp_send_json_error(array( |
| 605 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 606 |
"msg" => __('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'), |
| 607 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 608 |
)); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
private function optionUnnavailableInTestMode(){ |
| 613 |
wp_send_json_error(array( |
| 614 |
"title" => __( 'payplug_enable_feature', 'payplug' ), |
| 615 |
"msg" => __( 'payplug_unavailable_testmode_description', 'payplug' ) |
| 616 |
)); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* |
| 621 |
* Save data from request |
| 622 |
* |
| 623 |
* @return null |
| 624 |
*/ |
| 625 |
public function payplug_save_data( WP_REST_Request $request ) { |
| 626 |
|
| 627 |
$payplug = new PayplugGateway(); |
| 628 |
|
| 629 |
if ($payplug->user_logged_in()) { |
| 630 |
|
| 631 |
$data = $request->get_params(); |
| 632 |
$options = get_option('woocommerce_payplug_settings', []); |
| 633 |
$account = $this->getAccount(); |
| 634 |
PayplugWoocommerceHelper::set_transient_data($account); |
| 635 |
|
| 636 |
$options['enabled'] = Validator::enabled($data['payplug_enable']); |
| 637 |
$options['mode'] = Validator::mode($data['payplug_sandbox']); |
| 638 |
|
| 639 |
//TODO:: add validation for mode |
| 640 |
$test_mode = $options['mode'] === 'yes' ? false : true; |
| 641 |
|
| 642 |
$options['title'] = wp_strip_all_tags($data['standard_payment_title']); |
| 643 |
$options['description'] = wp_strip_all_tags($data['standard_payment_description']); |
| 644 |
$options['payment_method'] = (Validator::payment_method($data['payplug_embeded'])) ? $data['payplug_embeded'] : $options['payplug_embeded']; |
| 645 |
$options['oneclick'] = Validator::oneclick($data['enable_one_click']); |
| 646 |
|
| 647 |
//TODO:: add validation for payment methods |
| 648 |
$options['bancontact'] = Validator::bancontact($data['enable_bancontact'], $test_mode); |
| 649 |
$options['apple_pay'] = Validator::apple_pay($data['enable_applepay'], $test_mode); |
| 650 |
$options['american_express'] = Validator::american_express($data['enable_american_express'], $test_mode); |
| 651 |
$options['oney'] = Validator::oney($data['enable_oney']); |
| 652 |
//TODO:: add validation for oney -> needed to add modal for error msg |
| 653 |
/*if($options['oney'] === 'yes'){ |
| 654 |
$oney = $this->check_oney($account, $test_mode); |
| 655 |
if(is_array($oney)){ |
| 656 |
wp_send_json_error($oney); |
| 657 |
} |
| 658 |
}*/ |
| 659 |
|
| 660 |
$options['oney_type'] = (Validator::oney_type($data['payplug_oney'])) ? $data['payplug_oney'] : 'with_fees'; |
| 661 |
$thresholds = (Validator::oney_thresholds($data['oney_min_amounts'], $data['oney_max_amounts'])); |
| 662 |
$options['oney_thresholds_min'] = $thresholds['min']; |
| 663 |
$options['oney_thresholds_max'] = $thresholds['max']; |
| 664 |
$options['oney_product_animation'] = Validator::oney_product_animation($data['enable_oney_product_animation']); |
| 665 |
$options['debug'] = Validator::debug($data['enable_debug']); |
| 666 |
|
| 667 |
update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) ); |
| 668 |
http_response_code(200); |
| 669 |
|
| 670 |
wp_send_json_success( array( |
| 671 |
"title" => null, |
| 672 |
"msg" => __( 'payplug_save_success_message', 'payplug' ), |
| 673 |
"close" => __( 'payplug_ok', 'payplug' ) |
| 674 |
)); |
| 675 |
} else { |
| 676 |
http_response_code(403); |
| 677 |
wp_send_json_error("You are not logged in !"); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
} |
| 682 |
|
| 683 |
public function payplug_check_requirements() { |
| 684 |
wp_send_json_success(array( |
| 685 |
"status" => ( new Vue )->payplug_section_status() |
| 686 |
)); |
| 687 |
} |
| 688 |
|
| 689 |
} |
| 690 |
|