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