| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
use Payplug\Authentication; |
| 11 |
use Payplug\Exception\ForbiddenException; |
| 12 |
use Payplug\Payplug; |
| 13 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 14 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x; |
| 15 |
use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions; |
| 16 |
use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| 17 |
use Payplug\Resource\APIResource; |
| 18 |
use WC_Blocks_Utils; |
| 19 |
use WC_Subscriptions; |
| 20 |
|
| 21 |
/** |
| 22 |
* Helper class. |
| 23 |
*/ |
| 24 |
class PayplugWoocommerceHelper |
| 25 |
{ |
| 26 |
use ServiceGetter; |
| 27 |
|
| 28 |
/** |
| 29 |
* Check if current WooCommerce version is below 3.0.0 |
| 30 |
* |
| 31 |
* @return bool |
| 32 |
*/ |
| 33 |
public static function is_pre_30() |
| 34 |
{ |
| 35 |
$wc = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; |
| 36 |
|
| 37 |
return version_compare($wc->version, '3.0.0', '<'); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get a URL to the PayPlug gateway settings page. |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public static function get_setting_link() |
| 46 |
{ |
| 47 |
$use_id_as_section = function_exists('WC') ? version_compare(WC()->version, '2.6', '>=') : false; |
| 48 |
$section_slug = $use_id_as_section ? 'payplug' : strtolower('PayplugGateway'); |
| 49 |
|
| 50 |
return admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get all country code supported by PayPlug. |
| 55 |
* |
| 56 |
* Those are ISO 3166-1 alpha-2. You can find more information on https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public static function get_supported_countries() |
| 61 |
{ |
| 62 |
return [ |
| 63 |
'AD', |
| 64 |
'AO', |
| 65 |
'AX', |
| 66 |
'BG', |
| 67 |
'BO', |
| 68 |
'BY', |
| 69 |
'CH', |
| 70 |
'CR', |
| 71 |
'DE', |
| 72 |
'EE', |
| 73 |
'FI', |
| 74 |
'GB', |
| 75 |
'GL', |
| 76 |
'GT', |
| 77 |
'HR', |
| 78 |
'IN', |
| 79 |
'JM', |
| 80 |
'KM', |
| 81 |
'KZ', |
| 82 |
'LS', |
| 83 |
'MD', |
| 84 |
'MM', |
| 85 |
'MT', |
| 86 |
'NA', |
| 87 |
'NO', |
| 88 |
'PE', |
| 89 |
'PN', |
| 90 |
'RE', |
| 91 |
'SC', |
| 92 |
'SK', |
| 93 |
'ST', |
| 94 |
'TF', |
| 95 |
'TN', |
| 96 |
'UA', |
| 97 |
'VC', |
| 98 |
'WS', |
| 99 |
'AE', |
| 100 |
'AQ', |
| 101 |
'AZ', |
| 102 |
'BH', |
| 103 |
'BQ', |
| 104 |
'BZ', |
| 105 |
'CI', |
| 106 |
'CU', |
| 107 |
'DJ', |
| 108 |
'EG', |
| 109 |
'FJ', |
| 110 |
'GD', |
| 111 |
'GM', |
| 112 |
'GU', |
| 113 |
'HT', |
| 114 |
'IO', |
| 115 |
'JO', |
| 116 |
'KN', |
| 117 |
'LA', |
| 118 |
'LT', |
| 119 |
'ME', |
| 120 |
'MN', |
| 121 |
'MU', |
| 122 |
'NC', |
| 123 |
'NP', |
| 124 |
'PF', |
| 125 |
'PR', |
| 126 |
'RO', |
| 127 |
'SD', |
| 128 |
'SL', |
| 129 |
'SV', |
| 130 |
'TG', |
| 131 |
'TO', |
| 132 |
'UG', |
| 133 |
'VE', |
| 134 |
'YE', |
| 135 |
'AM', |
| 136 |
'AW', |
| 137 |
'BF', |
| 138 |
'BN', |
| 139 |
'BW', |
| 140 |
'CG', |
| 141 |
'CO', |
| 142 |
'AF', |
| 143 |
'CZ', |
| 144 |
'EC', |
| 145 |
'EU', |
| 146 |
'GA', |
| 147 |
'GI', |
| 148 |
'GS', |
| 149 |
'HN', |
| 150 |
'IM', |
| 151 |
'JE', |
| 152 |
'KI', |
| 153 |
'KY', |
| 154 |
'LR', |
| 155 |
'MC', |
| 156 |
'ML', |
| 157 |
'MS', |
| 158 |
'MZ', |
| 159 |
'NL', |
| 160 |
'PA', |
| 161 |
'PM', |
| 162 |
'QA', |
| 163 |
'SB', |
| 164 |
'SJ', |
| 165 |
'SS', |
| 166 |
'TD', |
| 167 |
'TM', |
| 168 |
'TZ', |
| 169 |
'VA', |
| 170 |
'WF', |
| 171 |
'AR', |
| 172 |
'BA', |
| 173 |
'BI', |
| 174 |
'BR', |
| 175 |
'CA', |
| 176 |
'CK', |
| 177 |
'CV', |
| 178 |
'DK', |
| 179 |
'EH', |
| 180 |
'FK', |
| 181 |
'GE', |
| 182 |
'GN', |
| 183 |
'GW', |
| 184 |
'HU', |
| 185 |
'IQ', |
| 186 |
'JP', |
| 187 |
'KP', |
| 188 |
'LB', |
| 189 |
'LU', |
| 190 |
'MF', |
| 191 |
'MO', |
| 192 |
'MV', |
| 193 |
'NE', |
| 194 |
'NR', |
| 195 |
'PG', |
| 196 |
'PS', |
| 197 |
'RS', |
| 198 |
'SE', |
| 199 |
'SM', |
| 200 |
'SX', |
| 201 |
'TH', |
| 202 |
'TR', |
| 203 |
'UM', |
| 204 |
'VG', |
| 205 |
'YT', |
| 206 |
'AL', |
| 207 |
'AU', |
| 208 |
'BE', |
| 209 |
'BM', |
| 210 |
'BV', |
| 211 |
'CF', |
| 212 |
'CN', |
| 213 |
'CY', |
| 214 |
'DZ', |
| 215 |
'ET', |
| 216 |
'FR', |
| 217 |
'GH', |
| 218 |
'GR', |
| 219 |
'HM', |
| 220 |
'IL', |
| 221 |
'IT', |
| 222 |
'KH', |
| 223 |
'KW', |
| 224 |
'LK', |
| 225 |
'MA', |
| 226 |
'MK', |
| 227 |
'MR', |
| 228 |
'MY', |
| 229 |
'NI', |
| 230 |
'OM', |
| 231 |
'PL', |
| 232 |
'PY', |
| 233 |
'SA', |
| 234 |
'SI', |
| 235 |
'SR', |
| 236 |
'TC', |
| 237 |
'TL', |
| 238 |
'TW', |
| 239 |
'UZ', |
| 240 |
'VU', |
| 241 |
'ZW', |
| 242 |
'AI', |
| 243 |
'AT', |
| 244 |
'BD', |
| 245 |
'BL', |
| 246 |
'BT', |
| 247 |
'CD', |
| 248 |
'CM', |
| 249 |
'CX', |
| 250 |
'DO', |
| 251 |
'ES', |
| 252 |
'FO', |
| 253 |
'GG', |
| 254 |
'GQ', |
| 255 |
'HK', |
| 256 |
'IE', |
| 257 |
'IS', |
| 258 |
'KG', |
| 259 |
'KS', |
| 260 |
'LI', |
| 261 |
'LY', |
| 262 |
'MH', |
| 263 |
'MQ', |
| 264 |
'MX', |
| 265 |
'NG', |
| 266 |
'NZ', |
| 267 |
'PK', |
| 268 |
'PW', |
| 269 |
'RW', |
| 270 |
'SH', |
| 271 |
'SO', |
| 272 |
'SZ', |
| 273 |
'TK', |
| 274 |
'TV', |
| 275 |
'UY', |
| 276 |
'VN', |
| 277 |
'ZM', |
| 278 |
'AG', |
| 279 |
'AS', |
| 280 |
'BB', |
| 281 |
'BJ', |
| 282 |
'BS', |
| 283 |
'CC', |
| 284 |
'CL', |
| 285 |
'CW', |
| 286 |
'DM', |
| 287 |
'ER', |
| 288 |
'FM', |
| 289 |
'GF', |
| 290 |
'GP', |
| 291 |
'GY', |
| 292 |
'ID', |
| 293 |
'IR', |
| 294 |
'KE', |
| 295 |
'KR', |
| 296 |
'LC', |
| 297 |
'LV', |
| 298 |
'MG', |
| 299 |
'MP', |
| 300 |
'MW', |
| 301 |
'NF', |
| 302 |
'NU', |
| 303 |
'PH', |
| 304 |
'PT', |
| 305 |
'RU', |
| 306 |
'SG', |
| 307 |
'SN', |
| 308 |
'SY', |
| 309 |
'TJ', |
| 310 |
'TT', |
| 311 |
'US', |
| 312 |
'VI', |
| 313 |
'ZA', |
| 314 |
]; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Ensure country code is supported by PayPlug. |
| 319 |
* |
| 320 |
* @param string $country The ISO 3166-1 alpha-2 code for the country |
| 321 |
* |
| 322 |
* @return bool |
| 323 |
* |
| 324 |
* @author Clément Boirie |
| 325 |
*/ |
| 326 |
public static function is_country_supported($country) |
| 327 |
{ |
| 328 |
$country = trim($country); |
| 329 |
if (empty($country)) { |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
return in_array(strtoupper($country), self::get_supported_countries()); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Get default country. |
| 338 |
* |
| 339 |
* @return string |
| 340 |
*/ |
| 341 |
public static function get_default_country() |
| 342 |
{ |
| 343 |
$country = \WC()->countries->get_base_country(); |
| 344 |
|
| 345 |
return (self::is_country_supported($country)) ? strtoupper($country) : 'FR'; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Get minimum amount allowed by PayPlug. |
| 350 |
* |
| 351 |
* This amount is in cents. |
| 352 |
* |
| 353 |
* @return int |
| 354 |
*/ |
| 355 |
public static function get_minimum_amount() |
| 356 |
{ |
| 357 |
return 99; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Get maximum amount allowed by PayPlug. |
| 362 |
* |
| 363 |
* This amount is in cents. |
| 364 |
* |
| 365 |
* @return int |
| 366 |
*/ |
| 367 |
public static function get_maximum_amount() |
| 368 |
{ |
| 369 |
return 2000000; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Convert amount in cents. |
| 374 |
* |
| 375 |
* @param float $amount |
| 376 |
* |
| 377 |
* @return int |
| 378 |
*/ |
| 379 |
public static function get_payplug_amount($amount) |
| 380 |
{ |
| 381 |
if (is_null($amount)) { |
| 382 |
return $amount; |
| 383 |
} |
| 384 |
|
| 385 |
return absint(wc_format_decimal(((float) $amount * 100), wc_get_price_decimals())); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Extract useful metadata from PayPlug response. |
| 390 |
* |
| 391 |
* @param APIResource $resource |
| 392 |
* |
| 393 |
* @return array |
| 394 |
*/ |
| 395 |
public static function extract_transaction_metadata($resource) |
| 396 |
{ |
| 397 |
// For non-card payment methods (e.g. Oney), the API returns "card": null. |
| 398 |
// Accessing null->property throws \Error in PHP 8+, which is not caught by catch(\Exception). |
| 399 |
$card = (isset($resource->card) && $resource->card !== null) ? $resource->card : null; |
| 400 |
|
| 401 |
return [ |
| 402 |
'transaction_id' => sanitize_text_field($resource->id), |
| 403 |
'paid' => (bool) $resource->is_paid, |
| 404 |
'refunded' => (bool) $resource->is_refunded, |
| 405 |
'amount' => sanitize_text_field($resource->amount), |
| 406 |
'amount_refunded' => sanitize_text_field($resource->amount_refunded), |
| 407 |
'3ds' => (bool) $resource->is_3ds, |
| 408 |
'live' => (bool) $resource->is_live, |
| 409 |
'paid_at' => isset($resource->hosted_payment->paid_at) ? sanitize_text_field($resource->hosted_payment->paid_at) : sanitize_text_field($resource->created_at), |
| 410 |
'card_last4' => $card !== null ? sanitize_text_field($card->last4) : '', |
| 411 |
'card_exp_month' => $card !== null ? sanitize_text_field($card->exp_month) : '', |
| 412 |
'card_exp_year' => $card !== null ? sanitize_text_field($card->exp_year) : '', |
| 413 |
'card_brand' => $card !== null ? sanitize_text_field($card->brand) : '', |
| 414 |
'card_country' => $card !== null ? sanitize_text_field($card->country) : '', |
| 415 |
]; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* @param \WC_Order $order |
| 420 |
* |
| 421 |
* @return array|bool |
| 422 |
* |
| 423 |
* @author Clément Boirie |
| 424 |
*/ |
| 425 |
public static function get_transaction_metadata($order) |
| 426 |
{ |
| 427 |
if (self::is_pre_30()) { |
| 428 |
return get_post_meta($order->id, '_payplug_metadata', true); |
| 429 |
} else { |
| 430 |
return $order->get_meta('_payplug_metadata', true); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Save transaction metadata extracted from PayPlug response. |
| 436 |
* |
| 437 |
* @param \WC_Order $order |
| 438 |
* @param array $metadata |
| 439 |
* |
| 440 |
* @return void |
| 441 |
*/ |
| 442 |
public static function save_transaction_metadata($order, $metadata): void |
| 443 |
{ |
| 444 |
if (self::is_pre_30()) { |
| 445 |
update_post_meta($order->id, '_payplug_metadata', $metadata); |
| 446 |
} else { |
| 447 |
$order->add_meta_data('_payplug_metadata', $metadata, true); |
| 448 |
$order->save_meta_data(); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Set flag ipn ( in progress / over ) on order |
| 454 |
* |
| 455 |
* @param \WC_Order $order |
| 456 |
* @param array $metadata |
| 457 |
* @param bool $flag |
| 458 |
* |
| 459 |
* @return void |
| 460 |
*/ |
| 461 |
public static function set_flag_ipn_order($order, $metadata, $flag): void |
| 462 |
{ |
| 463 |
$metadata['transaction_in_progress'] = $flag; |
| 464 |
self::save_transaction_metadata($order, $metadata); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Get transient key from payplug option |
| 469 |
* |
| 470 |
* @return string |
| 471 |
*/ |
| 472 |
public static function get_transient_key($options) |
| 473 |
{ |
| 474 |
$transient_key = PayplugGateway::OPTION_NAME . (array_key_exists('mode', $options) && (bool) $options['mode'] ? '_live' : '_test'); |
| 475 |
|
| 476 |
return $transient_key; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Get transient live key from payplug option |
| 481 |
* |
| 482 |
* @return string |
| 483 |
*/ |
| 484 |
public static function get_live_transient_key() |
| 485 |
{ |
| 486 |
return PayplugGateway::OPTION_NAME . '_live'; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Set transient data for payplug account |
| 491 |
* |
| 492 |
* @return string |
| 493 |
*/ |
| 494 |
public static function set_transient_data($data, $options = null) |
| 495 |
{ |
| 496 |
$options = $options ? $options : self::get_payplug_options(); |
| 497 |
$transient_key = self::get_transient_key($options); |
| 498 |
set_transient($transient_key, isset($data['httpResponse']) ? $data['httpResponse'] : []); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Get current option from payplug settings |
| 503 |
* |
| 504 |
* @return array |
| 505 |
*/ |
| 506 |
public static function get_account_data_from_options() |
| 507 |
{ |
| 508 |
$options = self::get_payplug_options(); |
| 509 |
$transient_key = self::get_transient_key($options); |
| 510 |
$account = get_transient($transient_key); |
| 511 |
|
| 512 |
if (empty($account) || !is_array($account)) { |
| 513 |
self::set_account_data_from_options(); |
| 514 |
$account = get_transient($transient_key); |
| 515 |
} |
| 516 |
|
| 517 |
return $account; |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Get current option from payplug settings |
| 522 |
*this should replace get_account_data_from_options |
| 523 |
* |
| 524 |
* @return array |
| 525 |
*/ |
| 526 |
public static function generic_get_account_data_from_options($gateway_id) |
| 527 |
{ |
| 528 |
$options = self::get_payplug_options(); |
| 529 |
$transient_key = self::get_transient_key($options); |
| 530 |
$account = get_transient($transient_key); |
| 531 |
|
| 532 |
//if transient is empty, it goes and get the permissions for the customer to populate it |
| 533 |
if (empty($account) || !is_array($account)) { |
| 534 |
self::set_account_data_from_options(); |
| 535 |
$account = get_transient($transient_key); |
| 536 |
} |
| 537 |
|
| 538 |
if (!empty($account)) { |
| 539 |
$helper = new self(); |
| 540 |
$configuration = $helper->get_service('configuration'); |
| 541 |
$account['permissions']['payplug'] = $configuration->get_option('payment_methods.configuration.payplug.active'); |
| 542 |
} |
| 543 |
|
| 544 |
return $account; |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Set current option from payplug settings and api call |
| 549 |
* |
| 550 |
* @return void |
| 551 |
*/ |
| 552 |
public static function set_account_data_from_options() |
| 553 |
{ |
| 554 |
$helper = new self(); |
| 555 |
$configuration = $helper->get_service('configuration'); |
| 556 |
$options = $configuration->get_options(); |
| 557 |
|
| 558 |
if (empty($options) || !isset($options['api_key']) || !isset($options['jwt'])) { |
| 559 |
return []; |
| 560 |
} |
| 561 |
|
| 562 |
// get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts). |
| 563 |
$mode = (bool) $options['mode'] ? 'live' : 'test'; |
| 564 |
$key = (string) $helper->get_api()->get_bearer_token($mode); |
| 565 |
|
| 566 |
if (empty($key)) { |
| 567 |
return []; |
| 568 |
} |
| 569 |
|
| 570 |
try { |
| 571 |
$parameters_account = Authentication::getAccount(new Payplug($key)); |
| 572 |
self::set_transient_data($parameters_account, $options); |
| 573 |
} catch (\Payplug\Exception\UnauthorizedException $e) { |
| 574 |
self::exception_handler_400_logout($e->getCode(), __('payplug_enable_feature', 'payplug'), sprintf('Account request error from PayPlug API : %s <br><b> ' . __('Successfully logged out.', 'payplug') . '</b>', wc_print_r($e->getMessage(), true))); |
| 575 |
} catch (\Payplug\Exception\ConfigurationNotSetException $e) { |
| 576 |
} catch (\Payplug\Exception\ForbiddenException $e) { |
| 577 |
} catch (\Payplug\Exception\ForbiddenException $e) { |
| 578 |
return []; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
/** |
| 583 |
* Get min and max for oney payment |
| 584 |
* |
| 585 |
* @return array |
| 586 |
*/ |
| 587 |
public static function get_min_max_oney() |
| 588 |
{ |
| 589 |
$account = self::get_account_data_from_options(); |
| 590 |
if (!$account) { |
| 591 |
return []; |
| 592 |
} |
| 593 |
|
| 594 |
return [ |
| 595 |
'min' => floatval($account['configuration']['oney']['min_amounts']['EUR']) / 100, |
| 596 |
'max' => floatval($account['configuration']['oney']['max_amounts']['EUR']) / 100, |
| 597 |
]; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Check if oney is available with current settings |
| 602 |
* |
| 603 |
* @return bool |
| 604 |
*/ |
| 605 |
public static function is_oney_available() |
| 606 |
{ |
| 607 |
$account = self::get_account_data_from_options(); |
| 608 |
if (!$account) { |
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
$options = self::get_payplug_options(); |
| 613 |
$oney_active = (bool) $options['payment_methods']['configuration']['oney']['active']; |
| 614 |
|
| 615 |
return $account && $account['permissions'][PayplugPermissions::USE_ONEY] == '1' && $oney_active; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Hide popup for if country_code != payplug country |
| 620 |
* https://payplug-prod.atlassian.net/browse/WOOC-249 |
| 621 |
* |
| 622 |
* @return bool |
| 623 |
*/ |
| 624 |
public static function show_oney_popup() |
| 625 |
{ |
| 626 |
$account = self::get_account_data_from_options(); |
| 627 |
if ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == true && $account['country'] == self::getISOCountryCode()) { |
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
return false; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* @return bool |
| 636 |
*/ |
| 637 |
public static function check_order_max_amount($order_total) |
| 638 |
{ |
| 639 |
if ($order_total < PayplugGatewayOney3x::MIN_AMOUNT || $order_total > PayplugGatewayOney3x::MAX_AMOUNT) { |
| 640 |
return false; |
| 641 |
} |
| 642 |
|
| 643 |
return true; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Load translations from plugin languages folder. |
| 648 |
* |
| 649 |
* @param string $plugin_rel_path |
| 650 |
* |
| 651 |
* @return bool |
| 652 |
*/ |
| 653 |
public static function load_plugin_textdomain($plugin_rel_path) |
| 654 |
{ |
| 655 |
$domain = 'payplug'; |
| 656 |
|
| 657 |
$locale = apply_filters('plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain); |
| 658 |
|
| 659 |
$mofile = $domain . '-' . $locale . '.mo'; |
| 660 |
|
| 661 |
$path = WP_PLUGIN_DIR . '/' . trim($plugin_rel_path, '/'); |
| 662 |
|
| 663 |
return load_textdomain($domain, $path . '/' . $mofile); |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Check and update value for oney simulation |
| 668 |
* |
| 669 |
* @return void |
| 670 |
*/ |
| 671 |
public static function oney_simulation_values($keys_array, &$array): void |
| 672 |
{ |
| 673 |
foreach ($keys_array as $key) { |
| 674 |
if (array_key_exists($key, $array)) { |
| 675 |
$array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100; |
| 676 |
foreach ($array[$key]['installments'] as $k => $value) { |
| 677 |
$array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100; |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
public static function getISOCountryCode() |
| 684 |
{ |
| 685 |
preg_match('([a-z-]+)', get_locale(), $country); |
| 686 |
|
| 687 |
return strtoupper($country[0]); |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
* Get country of the payplug merchant account and save it to the database |
| 692 |
* |
| 693 |
* @return string payplug merchant account country |
| 694 |
*/ |
| 695 |
public static function get_payplug_merchant_country() |
| 696 |
{ |
| 697 |
$data = self::get_payplug_options(); |
| 698 |
|
| 699 |
//in order to reduce the getAccount calls |
| 700 |
if (isset($data['payplug_live_key'])) { |
| 701 |
if (!isset($data['payplug_merchant_country'])) { |
| 702 |
return self::UpdateCountryOption($data); |
| 703 |
} |
| 704 |
|
| 705 |
return $data['payplug_merchant_country']; |
| 706 |
} |
| 707 |
|
| 708 |
$country = wc_get_base_location(); |
| 709 |
|
| 710 |
return $country['country']; |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Update payplug options and return the country |
| 715 |
* |
| 716 |
* @param $options |
| 717 |
* |
| 718 |
* @throws \Payplug\Exception\ConfigurationException |
| 719 |
* |
| 720 |
* @return string |
| 721 |
*/ |
| 722 |
public static function UpdateCountryOption($options) |
| 723 |
{ |
| 724 |
if (!isset($options['api_key'])) { |
| 725 |
$options['company_iso'] = 'FR'; |
| 726 |
|
| 727 |
return $options['company_iso']; |
| 728 |
} |
| 729 |
|
| 730 |
try { |
| 731 |
$api_key = json_decode($options['api_key'], true); |
| 732 |
|
| 733 |
//fail safe for non activated account |
| 734 |
if ((isset($options['mode'])) && $api_key['test']) { |
| 735 |
$key = $options['mode'] && !empty($api_key['live']) ? $api_key['live'] : $api_key['test']; |
| 736 |
} |
| 737 |
|
| 738 |
if (empty($api_key['live'])) { |
| 739 |
$options['mode'] = false; |
| 740 |
} |
| 741 |
|
| 742 |
if (isset($key) && !empty($key)) { |
| 743 |
$response = self::get_account_data_from_options(); |
| 744 |
} |
| 745 |
|
| 746 |
if (isset($response['httpResponse']['country'])) { |
| 747 |
$options['company_iso'] = $response['httpResponse']['country']; |
| 748 |
update_option('woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options)); |
| 749 |
} else { |
| 750 |
|
| 751 |
//default value for merchant country |
| 752 |
$options['company_iso'] = 'FR'; |
| 753 |
} |
| 754 |
} catch (ForbiddenException $e) { |
| 755 |
PayplugGateway::log('Error while getting account : ' . $e->getMessage(), 'error'); |
| 756 |
\WC_Admin_Settings::add_error($e->getMessage()); |
| 757 |
$options['company_iso'] = 'FR'; |
| 758 |
} |
| 759 |
|
| 760 |
return $options['company_iso']; |
| 761 |
} |
| 762 |
|
| 763 |
public static function get_live_key() |
| 764 |
{ |
| 765 |
// get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts). |
| 766 |
return (new self())->get_api()->get_bearer_token('live'); |
| 767 |
} |
| 768 |
|
| 769 |
public static function get_test_key() |
| 770 |
{ |
| 771 |
return (new self())->get_api()->get_bearer_token('test'); |
| 772 |
} |
| 773 |
|
| 774 |
public static function check_mode() |
| 775 |
{ |
| 776 |
return self::get_payplug_options()['mode']; |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Domain the Integrated Payment SDK should submit card-tokenization requests to. Depends |
| 781 |
* on whether the connected merchant account is QA or production, which is a build-time |
| 782 |
* distinction (see SECURE_DOMAIN in payplug-config.php), not something this can derive |
| 783 |
* from the Test/Live mode toggle at runtime. |
| 784 |
* |
| 785 |
* @return string |
| 786 |
*/ |
| 787 |
public static function get_secure_domain(): string |
| 788 |
{ |
| 789 |
return SECURE_DOMAIN; |
| 790 |
} |
| 791 |
|
| 792 |
public static function payplug_logout(): void |
| 793 |
{ |
| 794 |
$helper = new self(); |
| 795 |
$helper->get_service('configuration')->clean_option(); |
| 796 |
set_transient(self::get_transient_key(self::get_payplug_options()), null); |
| 797 |
} |
| 798 |
|
| 799 |
public static function plugin_deactivation(): void |
| 800 |
{ |
| 801 |
$option_name = 'woocommerce_payplug_settings'; |
| 802 |
delete_option($option_name); |
| 803 |
// for site options in Multisite |
| 804 |
delete_site_option($option_name); |
| 805 |
\Payplug\PayplugWoocommerce\Model\Lock::delete_lock_table(); |
| 806 |
} |
| 807 |
|
| 808 |
public static function available_shipping_methods($carriers = []) |
| 809 |
{ |
| 810 |
// Build enabled shipping methods based on active Zone |
| 811 |
$enabled_method_types = []; |
| 812 |
|
| 813 |
if (class_exists('\\WC_Shipping_Zones')) { |
| 814 |
// Regular zones |
| 815 |
$zones = \WC_Shipping_Zones::get_zones(); |
| 816 |
foreach ($zones as $zone) { |
| 817 |
$methods = isset($zone['shipping_methods']) ? $zone['shipping_methods'] : []; |
| 818 |
foreach ($methods as $m) { |
| 819 |
if (!empty($m->enabled)) { |
| 820 |
$enabled_method_types[$m->id] = true; |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
// Locations not covered by zones (zone 0) |
| 826 |
$defaultZone = new \WC_Shipping_Zone(0); |
| 827 |
foreach ($defaultZone->get_shipping_methods(true) as $m) { |
| 828 |
if (!empty($m->enabled)) { |
| 829 |
$enabled_method_types[$m->id] = true; |
| 830 |
} |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
$shippings = WC()->shipping()->get_shipping_methods(); |
| 835 |
$shippings_methods = []; |
| 836 |
|
| 837 |
foreach ($shippings as $shipping) { |
| 838 |
// Only keep shipping method types that are currently enabled in at least one zone |
| 839 |
if (!isset($enabled_method_types[$shipping->id])) { |
| 840 |
continue; |
| 841 |
} |
| 842 |
$shippings_methods[] = [ |
| 843 |
'id_carrier' => $shipping->id, |
| 844 |
'name' => $shipping->method_title, |
| 845 |
'checked' => in_array($shipping->id, $carriers, true), |
| 846 |
]; |
| 847 |
} |
| 848 |
|
| 849 |
return $shippings_methods; |
| 850 |
} |
| 851 |
|
| 852 |
public static function get_payplug_options() |
| 853 |
{ |
| 854 |
$helper = new self(); |
| 855 |
|
| 856 |
return $helper->get_service('configuration')->get_options(); |
| 857 |
} |
| 858 |
|
| 859 |
public static function get_applepay_options() |
| 860 |
{ |
| 861 |
$options = self::get_payplug_options(); |
| 862 |
$applepay_configuration = $options['payment_methods']['configuration']['apple_pay']; |
| 863 |
$applepay_display = json_decode($applepay_configuration['display'], true); |
| 864 |
$applepay = [ |
| 865 |
'enabled' => (bool) $applepay_configuration['active'], |
| 866 |
'checkout' => (bool) $applepay_display['checkout'], |
| 867 |
'cart' => (bool) $applepay_display['cart'], |
| 868 |
'carriers' => json_decode($applepay_configuration['carriers'], true), |
| 869 |
]; |
| 870 |
|
| 871 |
return $applepay; |
| 872 |
} |
| 873 |
|
| 874 |
public static function is_checkout_block() |
| 875 |
{ |
| 876 |
return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout'); |
| 877 |
} |
| 878 |
|
| 879 |
public static function is_cart_block() |
| 880 |
{ |
| 881 |
return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('cart'), 'woocommerce/cart'); |
| 882 |
} |
| 883 |
|
| 884 |
public static function is_product_block() |
| 885 |
{ |
| 886 |
return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('product'), 'woocommerce/product'); |
| 887 |
} |
| 888 |
|
| 889 |
/** |
| 890 |
* Checks if subscriptions are enabled on the site. |
| 891 |
* |
| 892 |
* @return bool Whether subscriptions is enabled or not. |
| 893 |
* |
| 894 |
* @since 5.6.0 |
| 895 |
*/ |
| 896 |
public static function is_subscriptions_enabled() |
| 897 |
{ |
| 898 |
return class_exists('WC_Subscriptions') && class_exists('WC_Subscription') && version_compare(WC_Subscriptions::$version, '2.2.0', '>='); |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
* Cart has any subscriptions |
| 903 |
* |
| 904 |
* @return bool |
| 905 |
*/ |
| 906 |
public static function is_subscription() |
| 907 |
{ |
| 908 |
if (is_null(WC()->cart)) { |
| 909 |
wc_load_cart(); |
| 910 |
WC()->cart->get_cart_from_session(); |
| 911 |
} |
| 912 |
|
| 913 |
if (empty(WC()->cart) || empty(WC()->cart->get_cart())) { |
| 914 |
return false; |
| 915 |
} |
| 916 |
|
| 917 |
foreach (WC()->cart->get_cart() as $prod) { |
| 918 |
if (empty($prod['product_id'])) { |
| 919 |
return false; |
| 920 |
} |
| 921 |
|
| 922 |
$pid = $prod['product_id']; |
| 923 |
$product = wc_get_product($pid); |
| 924 |
|
| 925 |
if ($product->is_type('subscription')) { |
| 926 |
return true; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
return false; |
| 931 |
} |
| 932 |
|
| 933 |
/** |
| 934 |
* Handling 400 errors from API (get_account, get_permissions) and return JSON rest response |
| 935 |
* |
| 936 |
* @param $title |
| 937 |
* @param $msg |
| 938 |
* |
| 939 |
* @return false |
| 940 |
*/ |
| 941 |
public static function exception_handler_400_logout($error_code, $title, $msg) |
| 942 |
{ |
| 943 |
if (!empty($error_code) && ($error_code === 401 || $error_code === 403)) { |
| 944 |
self::payplug_logout(); |
| 945 |
wp_send_json_error([ |
| 946 |
'title' => $title, |
| 947 |
'msg' => $msg, |
| 948 |
'close' => __('payplug_ok', 'payplug'), |
| 949 |
]); |
| 950 |
} |
| 951 |
|
| 952 |
return false; |
| 953 |
} |
| 954 |
} |
| 955 |
|