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