PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
← All changes | src/PayplugWoocommerceHelper.php +308 -37 1.2.52.9.0 View file →
@@ -6,8 +6,10 @@
6 6 if ( ! defined( 'ABSPATH' ) ) {
7 7 exit;
8 8 }
9 9
10 +use Payplug\Exception\ForbiddenException;
11 +use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
10 12 use Payplug\Resource\APIResource;
11 13 use Payplug\Payplug;
12 14 use Payplug\Authentication;
13 15 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
@@ -446,40 +448,119 @@
446 448 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
447 449 }
448 450
449 451 /**
452 + * Get transient key from payplug option
453 + *
454 + * @return string
455 + */
456 + public static function get_transient_key($options)
457 + {
458 + $transient_key = PayplugGateway::OPTION_NAME . (array_key_exists('mode', $options) && $options['mode'] === 'yes' ? "_live" : "_test");
459 + return $transient_key;
460 + }
461 +
462 +
463 + /**
464 + * Get transient live key from payplug option
465 + *
466 + * @return string
467 + */
468 + public static function get_live_transient_key()
469 + {
470 + return PayplugGateway::OPTION_NAME . "_live";
471 + }
472 +
473 + /**
474 + * Set transient data for payplug account
475 + *
476 + * @return string
477 + */
478 + public static function set_transient_data($data, $options = null)
479 + {
480 + $options = $options ? $options : get_option('woocommerce_payplug_settings', []);
481 + $transient_key = PayplugWoocommerceHelper::get_transient_key($options);
482 + set_transient($transient_key, isset($data['httpResponse']) ? $data['httpResponse'] : []);
483 + }
484 +
485 + /**
450 486 * Get current option from payplug settings
451 487 *
452 488 * @return array
453 489 */
454 - public static function get_account_data_from_options($force_update = false) {
455 - $options = get_option('woocommerce_payplug_settings', []);
456 - $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
457 - $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
458 - if (empty($payplug_test_key) && empty($payplug_live_key)) {
459 - return array();
460 - }
490 + public static function get_account_data_from_options()
491 + {
492 + $options = get_option('woocommerce_payplug_settings', []);
493 + $transient_key = self::get_transient_key($options);
494 + $account = get_transient($transient_key);
461 495
462 - $transient_key = PayplugGatewayOney3x::OPTION_NAME . ($options['mode'] === 'yes' ? "_live" : "_test");
463 - $payplug_oney_config = get_transient($transient_key);
464 - if ($payplug_oney_config && !$force_update) {
465 - $account = $payplug_oney_config;
466 - } else {
467 - try {
468 - $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
469 - set_transient($transient_key, $parameters_account['httpResponse']);
470 - $account = get_transient($transient_key);
471 - } catch (\Payplug\Exception\UnauthorizedException $e) {
472 - } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
473 - }
474 - }
475 - $account['oneyEnabled'] = $options['oney'] ? $options['oney'] : '';
476 - $account['oneyCgvEnabled'] = $options['oneycgv'] ? $options['oneycgv'] : '';
477 - return $account;
496 + if(empty($account) || !is_array($account) ){
497 + self::set_account_data_from_options();
498 + $account = get_transient($transient_key);
499 + }
478 500
501 + return $account;
479 502 }
480 503
481 504 /**
505 + * Get current option from payplug settings
506 + *this should replace get_account_data_from_options
507 + *
508 + * @return array
509 + */
510 + public static function generic_get_account_data_from_options($gateway_id){
511 + $options = get_option('woocommerce_payplug_settings', []);
512 + $transient_key = self::get_transient_key($options);
513 + $account = get_transient($transient_key);
514 +
515 + //if transient is empty, it goes and get the permissions for the customer to populate it
516 + if(empty($account) || !is_array($account) ){
517 + self::set_account_data_from_options();
518 + $account = get_transient($transient_key);
519 + }
520 +
521 + if( isset($options[$gateway_id]) && $options[$gateway_id] == "yes"){
522 + $account['permissions'][$gateway_id] = true;
523 + }
524 +
525 + return $account;
526 +
527 + }
528 +
529 + /**
530 + * Set current option from payplug settings and api call
531 + *
532 + * @return void
533 + */
534 + public static function set_account_data_from_options()
535 + {
536 + $options = get_option('woocommerce_payplug_settings', []);
537 + $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
538 + $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
539 +
540 + if (empty($payplug_test_key) && empty($payplug_live_key)) {
541 + return array();
542 + }
543 +
544 + if( $options['mode'] === 'yes' && empty($payplug_live_key) ){
545 + return array();
546 + }
547 +
548 + if( $options['mode'] != 'yes' && empty($payplug_test_key)){
549 + return array();
550 + }
551 +
552 + try {
553 + $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
554 + self::set_transient_data($parameters_account, $options);
555 + } catch (\Payplug\Exception\UnauthorizedException $e) {
556 + } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
557 + } catch( \Payplug\Exception\ForbiddenException $e){
558 + } catch (\Payplug\Exception\ForbiddenException $e){return array();}
559 +
560 + }
561 +
562 + /**
482 563 * Get min and max for oney payment
483 564 *
484 565 * @return array
485 566 */
@@ -484,9 +565,9 @@
484 565 * @return array
485 566 */
486 567 public static function get_min_max_oney() {
487 568 $account = self::get_account_data_from_options();
488 - if (empty($account)) {
569 + if (!$account) {
489 570 return array();
490 571 }
491 572 return [
492 573 'min' => floatval($account['configuration']['oney']['min_amounts']['EUR'])/100,
@@ -500,14 +581,43 @@
500 581 * @return boolean
501 582 */
502 583 public static function is_oney_available() {
503 584 $account = self::get_account_data_from_options();
504 - if (empty($account)) {
585 + if (!$account) {
505 586 return false;
506 587 }
507 - return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $account['oneyEnabled'] === "yes" && $account['oneyCgvEnabled'] === "yes");
588 +
589 + $options = self::get_payplug_options();
590 + $oney_active = (isset($options['oney']) && !empty($options['oney'])) ? $options['oney'] : '';
591 +
592 + return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $oney_active === "yes");
508 593 }
509 594
595 + /**
596 + * Hide popup for if country_code != payplug country
597 + * https://payplug-prod.atlassian.net/browse/WOOC-249
598 + *
599 + * @return bool
600 + */
601 + public static function show_oney_popup()
602 + {
603 + $account = self::get_account_data_from_options();
604 + if( $account && $account['permissions'][PayplugPermissions::USE_ONEY] == true && $account["country"] == self::getISOCountryCode() ){
605 + return true;
606 + }
607 + return false;
608 + }
609 +
610 + /**
611 + * @return bool
612 + */
613 + public static function check_order_max_amount($order_total){
614 + if ($order_total < PayplugGatewayOney3x::MIN_AMOUNT || $order_total > PayplugGatewayOney3x::MAX_AMOUNT) {
615 + return false;
616 + }
617 + return true;
618 + }
619 +
510 620 /**
511 621 * Load translations from plugin languages folder.
512 622 *
513 623 * @param string $plugin_rel_path
@@ -525,21 +635,182 @@
525 635 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
526 636
527 637 return load_textdomain( $domain, $path . '/' . $mofile );
528 638 }
529 -
639 +
530 640 /**
531 641 * Check and update value for oney simulation
532 642 *
533 643 * @return void
534 644 */
535 - public static function oney_simulation_values ($keys_array, &$array) {
536 - foreach($keys_array as $key) {
537 - if (array_key_exists($key, $array)) {
538 - $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
539 - foreach ($array[$key]['installments'] as $k => $value) {
540 - $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
541 - }
542 - }
543 - }
544 - }
645 + public static function oney_simulation_values ($keys_array, &$array) {
646 + foreach($keys_array as $key) {
647 + if (array_key_exists($key, $array)) {
648 + $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
649 + foreach ($array[$key]['installments'] as $k => $value) {
650 + $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
651 + }
652 + }
653 + }
654 + }
655 +
656 + public static function getISOCountryCode()
657 + {
658 + preg_match( '([a-z-]+)', get_locale(), $country );
659 + return strtoupper($country[0]);
660 + }
661 +
662 + /**
663 + * Get country of the payplug merchant account and save it to the database
664 + *
665 + * @return string payplug merchant account country
666 + */
667 + public static function get_payplug_merchant_country()
668 + {
669 + $data = get_option('woocommerce_payplug_settings');
670 +
671 + //in order to reduce the getAccount calls
672 + if (isset($data['payplug_live_key'])) {
673 +
674 + if (!isset($data['payplug_merchant_country'])) {
675 + return self::UpdateCountryOption($data);
676 + }
677 +
678 + return $data['payplug_merchant_country'];
679 + }
680 +
681 + $country = wc_get_base_location();
682 + return $country['country'];
683 +
684 + }
685 +
686 + /**
687 + * Update payplug options and return the country
688 + *
689 + * @param $options
690 + * @return string
691 + * @throws \Payplug\Exception\ConfigurationException
692 + */
693 + public static function UpdateCountryOption($options){
694 +
695 + try {
696 +
697 + //fail safe for non activated account
698 + if ((isset($options['mode'])) && $options['payplug_test_key']){
699 + $key = $options['mode'] === "yes" && !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : $options['payplug_test_key'];
700 + }
701 +
702 + if( empty($options['payplug_live_key'] )){
703 + $options['mode'] = "no";
704 + }
705 +
706 + if (isset($key) && !empty($key)){
707 +
708 + //$response = Authentication::getAccount(new Payplug($key));
709 + $response = self::get_account_data_from_options();
710 + }
711 +
712 + if (isset($response['httpResponse']['country'])) {
713 + $options['payplug_merchant_country'] = $response['httpResponse']['country'];
714 + update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
715 + }else{
716 +
717 + //default value for merchant country
718 + $options['payplug_merchant_country'] = 'FR';
719 + }
720 +
721 + } catch (ForbiddenException $e) {
722 + PayplugGateway::log('Error while getting account : ' . $e->getMessage(), 'error');
723 + \WC_Admin_Settings::add_error($e->getMessage());
724 + $options['payplug_merchant_country'] = 'FR';
725 + }
726 +
727 + return $options['payplug_merchant_country'];
728 +
729 + }
730 +
731 + public static function get_live_key()
732 + {
733 + return get_option('woocommerce_payplug_settings', [])['payplug_live_key'];
734 + }
735 +
736 + public static function get_test_key()
737 + {
738 + return get_option('woocommerce_payplug_settings', [])['payplug_test_key'];
739 + }
740 +
741 + /**
742 + *
743 + * Checks fi the user is logged in
744 + *
745 + * @return bool
746 + */
747 + public static function user_logged_in()
748 + {
749 + return !empty(get_option( 'woocommerce_payplug_settings', [] )['payplug_test_key']);
750 + }
751 +
752 + public static function check_mode(){
753 + return get_option( 'woocommerce_payplug_settings', [] )['mode'] === "yes" ? true : false;
754 + }
755 +
756 + public static function payplug_logout($gateway) {
757 +
758 + if ($gateway->user_logged_in()) {
759 + $data = get_option($gateway->get_option_key());
760 + $data['payplug_test_key'] = '';
761 + $data['payplug_live_key'] = '';
762 + $data['payplug_merchant_id'] = '';
763 + $data['enabled'] = 'no';
764 + $data['mode'] = 'no';
765 + $data['oneclick'] = 'no';
766 + update_option(
767 + $gateway->get_option_key(),
768 + apply_filters('woocommerce_settings_api_sanitized_fields_' . $gateway->id, $data)
769 + );
770 + if("payplug" === $gateway->id) {
771 + return true;
772 + }
773 + } else {
774 + return false;
775 + }
776 + }
777 +
778 + public static function plugin_deactivation(){
779 + $option_name = 'woocommerce_payplug_settings';
780 + delete_option( $option_name );
781 + // for site options in Multisite
782 + delete_site_option( $option_name );
783 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_table();
784 + }
785 +
786 + public static function available_shipping_methods($carriers = []) {
787 + $shippings = WC()->shipping()->get_shipping_methods();
788 + $shippings_methods = [];
789 + foreach ($shippings as $shipping) {
790 + array_push($shippings_methods,[
791 + "id_carrier" => $shipping->id,
792 + "name" => $shipping->method_title,
793 + "checked" => in_array($shipping->id, $carriers)
794 + ]);
795 + }
796 + return $shippings_methods;
797 + }
798 +
799 + public static function get_payplug_options() {
800 + return get_option('woocommerce_payplug_settings', []);
801 + }
802 +
803 + public static function get_applepay_options() {
804 + $options = self::get_payplug_options();
805 + $applepay = [
806 + "enabled" => (!empty($options['apple_pay'])) ? $options['apple_pay'] : false,
807 + "checkout" => (!empty($options['applepay_checkout'])) ? $options['applepay_checkout'] : false,
808 + "cart" => (!empty($options['applepay_cart'])) ? $options['applepay_cart'] : false,
809 + "carriers" => (!empty($options['applepay_carriers'])) ? $options['applepay_carriers'] : [],
810 +
811 + ];
812 +
813 + return $applepay;
814 + }
815 +
545 816 }