From db6dbc3086d7c6f20014145caa1fe4558b837249 Mon Sep 17 00:00:00 2001 From: richardtong Date: Fri, 17 Jul 2020 14:45:25 +0700 Subject: [PATCH 01/29] add cache susport for get local payments method --- src/includes/class-paymentwall-gateway.php | 64 +++++++++++++--------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 4a75272..e7ddc10 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,6 +11,7 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; + const PS_TIME_BUFFER = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -379,39 +380,48 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s /** * @return array|bool|string */ - public function get_local_payments() { + public function get_local_payment_methods() { $paymentMethods = []; + $psCreateTime = !empty($_SESSION['local_payments_create_time']) ? (int)$_SESSION['local_payments_create_time'] : 0; + if ( + empty($_SESSION['local_payments']) || + time() > $psCreateTime + self::PS_TIME_BUFFER + ) { + $_SESSION['local_payments_create_time'] = 1594974792; + $this->init_configs(); + $uesrIp = $this->getRealClientIP(); + $userCountry = $this->get_country_by_ip($uesrIp); + + if (!empty($userCountry)) { + $params = array( + 'key' => $this->settings['appkey'], + 'country_code' => $userCountry, + 'sign_version' => 2, + 'currencyCode' => get_woocommerce_currency(), + 'amount' => WC()->cart->total + ); - $this->init_configs(); - $uesrIp = $this->getRealClientIP(); - $userCountry = $this->get_country_by_ip($uesrIp); - - if (!empty($userCountry)) { - $params = array( - 'key' => $this->settings['appkey'], - 'country_code' => $userCountry , - 'sign_version' => 2, - 'currencyCode' => get_woocommerce_currency(), - 'amount' => WC()->cart->total - ); + $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( + $params, + $params['sign_version'] + ); - $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( - $params, - $params['sign_version'] - ); + $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); - $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); + if (curl_error($curl)) { + return $paymentMethods; + } - if (curl_error($curl)) { - return $paymentMethods; - } + $response = curl_exec($curl); + $paymentMethods = $this->prepare_payment_methods_from_api_response($response); - $response = curl_exec($curl); - $paymentMethods = $this->prepare_payment_methods_from_api_response($response); + } + } else { + $paymentMethods = $_SESSION['local_payments']; } return $paymentMethods; } @@ -429,6 +439,8 @@ public function prepare_payment_methods_from_api_response($response) { 'name' => $ps['name'], 'img_url' => !empty($ps['img_url']) ? $ps['img_url'] : '' ]; + $_SESSION['local_payments'] = $methods; + $_SESSION['local_payments_create_time'] = time(); } } @@ -436,7 +448,7 @@ public function prepare_payment_methods_from_api_response($response) { } public function html_payment_system() { - $paymentSystems = $this->get_local_payments(); + $paymentSystems = $this->get_local_payment_methods(); if (is_array($paymentSystems) && !empty($paymentSystems)) { echo ''; ?> - - + + session->pw_ps; if ( !empty($selectedPaymentMethod['name']) @@ -547,10 +547,9 @@ public function get_data_from_session($name) { } public function save_data_to_session($name, $data) { - $pwSessionData = $_SESSION['paymentwall_data']; if (!empty($name) && !empty($data)) { - $pwSessionData[$name]['data'] = $data; - $pwSessionData[$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['data'] = $data; + $_SESSION['paymentwall_data']['expired_time'] = time() + self::PS_TIME_BUFFER; } } From 5ef6ddf0a45f6e0b8cb6793870546b3594d3fddb Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:30:31 +0700 Subject: [PATCH 06/29] check condition before prepare payment methods --- src/includes/class-paymentwall-gateway.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index fbab7ce..12a749e 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -386,7 +386,9 @@ public function get_local_payment_methods() { return $localPaymentMethods; } $response = $this->get_payment_methods_from_api(); - $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); + if (!empty($response)) { + $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); + } return $localPaymentMethods; } From 38e344d804ca89fc866df80555d59f8dcfe40305 Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:38:25 +0700 Subject: [PATCH 07/29] fix typo --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 12a749e..b9259e8 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -378,7 +378,7 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s } /** - * @return array|bool|string + * @return array|null */ public function get_local_payment_methods() { $localPaymentMethods = $this->get_data_from_session('payment_methods'); From c3c5615a73538bf9b4cb5d8c0cdc566e5f23e6d2 Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:39:41 +0700 Subject: [PATCH 08/29] fix typo --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index b9259e8..3c8d059 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -551,7 +551,7 @@ public function get_data_from_session($name) { public function save_data_to_session($name, $data) { if (!empty($name) && !empty($data)) { $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data']['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; } } From 3b0ccb158b58869816417f3885b1ddebe6c9dd3a Mon Sep 17 00:00:00 2001 From: richardtong Date: Tue, 21 Jul 2020 10:11:39 +0700 Subject: [PATCH 09/29] remove comment --- src/includes/class-paymentwall-gateway.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 3c8d059..0a9b420 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -478,11 +478,7 @@ public function update_payment_system_order_meta() { WC()->session->pw_ps = json_decode(stripslashes($_POST['pw_payment_system']), true); } } - - /** - * Get user country by IP - * @return mixed - */ + public function get_country_by_ip($ip) { $countryCode = $this->get_data_from_session('country_code'); if (empty($country_code)) { @@ -510,11 +506,6 @@ public function get_country_by_ip($ip) { return null; } - /** - * @param $order_id - * @param $posted_data - * @param $order - */ public function get_payment_method_title($prop, $object) { $paymentSystemName = WC()->session->pw_ps; From b8e040cbc9a14a83898bbf8b781eb4757ec180bd Mon Sep 17 00:00:00 2001 From: richardtong Date: Tue, 21 Jul 2020 11:55:04 +0700 Subject: [PATCH 10/29] check if session exist --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 0a9b420..7212a10 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -528,7 +528,7 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta } public function get_data_from_session($name) { - $pwSessionData = $_SESSION['paymentwall_data']; + $pwSessionData = !empty($_SESSION['paymentwall_data']) ? $_SESSION['paymentwall_data'] : []; if ( !empty($pwSessionData[$name]['data']) && !empty($pwSessionData[$name]['expired_time']) From ff8ffc604bf30f75bae794c56c3c3cdd22d9aa46 Mon Sep 17 00:00:00 2001 From: richardtong Date: Wed, 22 Jul 2020 18:25:14 +0700 Subject: [PATCH 11/29] refactor code --- src/includes/class-paymentwall-gateway.php | 15 +++++++++------ src/paymentwall-for-woocommerce.php | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 7212a10..55f2d6b 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,7 +11,7 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; - const PS_TIME_BUFFER = 600; + const CACHED_DATA_TIME_TO_LIVE = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -258,8 +258,8 @@ function ipn_response() { } if (paymentwall_subscription_enable()) { - $subscriptions = wcs_get_subscriptions_for_order( $original_order_id, array( 'order_type' => 'parent' ) ); - $subscription = array_shift( $subscriptions ); + $subscriptions = wcs_get_subscriptions_for_order($original_order_id, array('order_type' => 'parent')); + $subscription = array_shift($subscriptions); $subscription_key = get_post_meta($original_order_id, '_subscription_id'); } @@ -413,7 +413,7 @@ public function get_payment_methods_from_api() { $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); if (curl_error($curl)) { return null; @@ -481,19 +481,22 @@ public function update_payment_system_order_meta() { public function get_country_by_ip($ip) { $countryCode = $this->get_data_from_session('country_code'); + if (empty($country_code)) { $params = array( 'key' => $this->settings['appkey'], 'uid' => self::USER_ID_GEOLOCATION, 'user_ip' => $ip ); + $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); if (curl_error($curl)) { return null; } + $response = json_decode($response, true); if (!empty($response['code'])) { $this->save_data_to_session('country_code', $response['code']); @@ -542,7 +545,7 @@ public function get_data_from_session($name) { public function save_data_to_session($name, $data) { if (!empty($name) && !empty($data)) { $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::CACHED_DATA_TIME_TO_LIVE; } } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index eaba7e2..c23a77c 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -142,10 +142,10 @@ function check_order_has_virtual_product(WC_Order $order) { return false; } + add_action('init', 'start_sesion'); -function start_sesion(){ - if( !session_id() ) - { +function start_sesion() { + if (!session_id()) { session_start(); } } From 53b9f8e7d7b93c92c42d4de9874ec285cd09953a Mon Sep 17 00:00:00 2001 From: Mason Pham Date: Thu, 23 Jul 2020 13:31:44 +0200 Subject: [PATCH 12/29] Revert "Merge branch 'dev-mdl-390-2' into 'develop'" This reverts merge request !40 --- src/includes/class-paymentwall-gateway.php | 123 ++++++++------------- src/paymentwall-for-woocommerce.php | 7 -- 2 files changed, 47 insertions(+), 83 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 55f2d6b..4a75272 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,7 +11,6 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; - const CACHED_DATA_TIME_TO_LIVE = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -35,7 +34,7 @@ public function __construct() { add_action('woocommerce_api_' . $this->id . '_gateway', array($this, 'handle_action')); add_action('woocommerce_review_order_before_payment', array($this, 'html_payment_system')); - add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); + add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); add_filter('woocommerce_order_get_payment_method_title', array($this,'get_payment_method_title'), 10, 2 ); add_filter('woocommerce_get_order_item_totals', array($this,'update_payment_title_by_selected_method'), 10, 3); @@ -258,8 +257,8 @@ function ipn_response() { } if (paymentwall_subscription_enable()) { - $subscriptions = wcs_get_subscriptions_for_order($original_order_id, array('order_type' => 'parent')); - $subscription = array_shift($subscriptions); + $subscriptions = wcs_get_subscriptions_for_order( $original_order_id, array( 'order_type' => 'parent' ) ); + $subscription = array_shift( $subscriptions ); $subscription_key = get_post_meta($original_order_id, '_subscription_id'); } @@ -378,30 +377,21 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s } /** - * @return array|null + * @return array|bool|string */ - public function get_local_payment_methods() { - $localPaymentMethods = $this->get_data_from_session('payment_methods'); - if (!empty($localPaymentMethods)) { - return $localPaymentMethods; - } - $response = $this->get_payment_methods_from_api(); - if (!empty($response)) { - $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); - } - return $localPaymentMethods; - } + public function get_local_payments() { + + $paymentMethods = []; - public function get_payment_methods_from_api() { $this->init_configs(); - $userIp = $this->getRealClientIP(); - $userCountry = $this->get_country_by_ip($userIp); + $uesrIp = $this->getRealClientIP(); + $userCountry = $this->get_country_by_ip($uesrIp); if (!empty($userCountry)) { $params = array( - 'key' => $this->settings['appkey'], - 'country_code' => $userCountry, - 'sign_version' => 3, + 'key' => $this->settings['appkey'], + 'country_code' => $userCountry , + 'sign_version' => 2, 'currencyCode' => get_woocommerce_currency(), 'amount' => WC()->cart->total ); @@ -413,15 +403,17 @@ public function get_payment_methods_from_api() { $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); if (curl_error($curl)) { - return null; + return $paymentMethods; } + $response = curl_exec($curl); - return $response; + $paymentMethods = $this->prepare_payment_methods_from_api_response($response); + } - return null; + return $paymentMethods; } public function prepare_payment_methods_from_api_response($response) { @@ -437,7 +429,6 @@ public function prepare_payment_methods_from_api_response($response) { 'name' => $ps['name'], 'img_url' => !empty($ps['img_url']) ? $ps['img_url'] : '' ]; - $this->save_data_to_session('payment_methods', $methods); } } @@ -445,7 +436,7 @@ public function prepare_payment_methods_from_api_response($response) { } public function html_payment_system() { - $paymentSystems = $this->get_local_payment_methods(); + $paymentSystems = $this->get_local_payments(); if (is_array($paymentSystems) && !empty($paymentSystems)) { echo '
    '; foreach ($paymentSystems as $gateway) { @@ -464,8 +455,8 @@ public function html_payment_system() { } echo '
'; ?> - - + + session->pw_ps = json_decode(stripslashes($_POST['pw_payment_system']), true); } } - - public function get_country_by_ip($ip) { - $countryCode = $this->get_data_from_session('country_code'); - - if (empty($country_code)) { - $params = array( - 'key' => $this->settings['appkey'], - 'uid' => self::USER_ID_GEOLOCATION, - 'user_ip' => $ip - ); - $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($curl); - if (curl_error($curl)) { - return null; - } - - $response = json_decode($response, true); - if (!empty($response['code'])) { - $this->save_data_to_session('country_code', $response['code']); - return $response['code']; - } - } else { - return $countryCode; + /** + * Get user country by IP + * @return mixed + */ + public function get_country_by_ip($ip) { + $params = array( + 'key' => $this->settings['appkey'], + 'uid' => self::USER_ID_GEOLOCATION, + 'user_ip' => $ip + ); + $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); + $response = curl_exec($curl); + if (curl_error($curl)) { + return null; + } + $response = json_decode($response, true); + if (!empty($response['code'])) { + return $response['code']; } - return null; } + /** + * @param $order_id + * @param $posted_data + * @param $order + */ public function get_payment_method_title($prop, $object) { $paymentSystemName = WC()->session->pw_ps; @@ -518,7 +508,7 @@ public function get_payment_method_title($prop, $object) return $prop; } - public function update_payment_title_by_selected_method($total_rows, $order, $tax_display) { + public function update_payment_title_by_selected_method($total_rows, $order, $tax_display){ $selectedPaymentMethod = WC()->session->pw_ps; if ( !empty($selectedPaymentMethod['name']) @@ -530,23 +520,4 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta return $total_rows; } - public function get_data_from_session($name) { - $pwSessionData = !empty($_SESSION['paymentwall_data']) ? $_SESSION['paymentwall_data'] : []; - if ( - !empty($pwSessionData[$name]['data']) - && !empty($pwSessionData[$name]['expired_time']) - && $pwSessionData[$name]['expired_time'] > time() - ) { - return $pwSessionData[$name]['data']; - } - return null; - } - - public function save_data_to_session($name, $data) { - if (!empty($name) && !empty($data)) { - $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::CACHED_DATA_TIME_TO_LIVE; - } - } - } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index c23a77c..b75ccfe 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -142,10 +142,3 @@ function check_order_has_virtual_product(WC_Order $order) { return false; } - -add_action('init', 'start_sesion'); -function start_sesion() { - if (!session_id()) { - session_start(); - } -} From 934db09f29e6efd97c164c1d47df988e18f2b793 Mon Sep 17 00:00:00 2001 From: richardtong Date: Fri, 17 Jul 2020 14:45:25 +0700 Subject: [PATCH 13/29] add cache susport for get local payments method --- src/includes/class-paymentwall-gateway.php | 64 +++++++++++++--------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 4a75272..e7ddc10 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,6 +11,7 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; + const PS_TIME_BUFFER = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -379,39 +380,48 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s /** * @return array|bool|string */ - public function get_local_payments() { + public function get_local_payment_methods() { $paymentMethods = []; + $psCreateTime = !empty($_SESSION['local_payments_create_time']) ? (int)$_SESSION['local_payments_create_time'] : 0; + if ( + empty($_SESSION['local_payments']) || + time() > $psCreateTime + self::PS_TIME_BUFFER + ) { + $_SESSION['local_payments_create_time'] = 1594974792; + $this->init_configs(); + $uesrIp = $this->getRealClientIP(); + $userCountry = $this->get_country_by_ip($uesrIp); + + if (!empty($userCountry)) { + $params = array( + 'key' => $this->settings['appkey'], + 'country_code' => $userCountry, + 'sign_version' => 2, + 'currencyCode' => get_woocommerce_currency(), + 'amount' => WC()->cart->total + ); - $this->init_configs(); - $uesrIp = $this->getRealClientIP(); - $userCountry = $this->get_country_by_ip($uesrIp); - - if (!empty($userCountry)) { - $params = array( - 'key' => $this->settings['appkey'], - 'country_code' => $userCountry , - 'sign_version' => 2, - 'currencyCode' => get_woocommerce_currency(), - 'amount' => WC()->cart->total - ); + $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( + $params, + $params['sign_version'] + ); - $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( - $params, - $params['sign_version'] - ); + $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); - $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); + if (curl_error($curl)) { + return $paymentMethods; + } - if (curl_error($curl)) { - return $paymentMethods; - } + $response = curl_exec($curl); + $paymentMethods = $this->prepare_payment_methods_from_api_response($response); - $response = curl_exec($curl); - $paymentMethods = $this->prepare_payment_methods_from_api_response($response); + } + } else { + $paymentMethods = $_SESSION['local_payments']; } return $paymentMethods; } @@ -429,6 +439,8 @@ public function prepare_payment_methods_from_api_response($response) { 'name' => $ps['name'], 'img_url' => !empty($ps['img_url']) ? $ps['img_url'] : '' ]; + $_SESSION['local_payments'] = $methods; + $_SESSION['local_payments_create_time'] = time(); } } @@ -436,7 +448,7 @@ public function prepare_payment_methods_from_api_response($response) { } public function html_payment_system() { - $paymentSystems = $this->get_local_payments(); + $paymentSystems = $this->get_local_payment_methods(); if (is_array($paymentSystems) && !empty($paymentSystems)) { echo '
    '; foreach ($paymentSystems as $gateway) { From eb4f93f01acc0b9be0bb4f63c53393df37b95255 Mon Sep 17 00:00:00 2001 From: richardtong Date: Fri, 17 Jul 2020 14:53:08 +0700 Subject: [PATCH 14/29] start session --- src/paymentwall-for-woocommerce.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index b75ccfe..eaba7e2 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -142,3 +142,10 @@ function check_order_has_virtual_product(WC_Order $order) { return false; } +add_action('init', 'start_sesion'); +function start_sesion(){ + if( !session_id() ) + { + session_start(); + } +} From 8ee9857f229768c0f945f23ae161ebb1600d856c Mon Sep 17 00:00:00 2001 From: richardtong Date: Fri, 17 Jul 2020 16:06:44 +0700 Subject: [PATCH 15/29] refactor code, --- src/includes/class-paymentwall-gateway.php | 70 ++++++++++++++-------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index e7ddc10..24e6168 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -383,12 +383,8 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s public function get_local_payment_methods() { $paymentMethods = []; - $psCreateTime = !empty($_SESSION['local_payments_create_time']) ? (int)$_SESSION['local_payments_create_time'] : 0; - if ( - empty($_SESSION['local_payments']) || - time() > $psCreateTime + self::PS_TIME_BUFFER - ) { - $_SESSION['local_payments_create_time'] = 1594974792; + $localPaymentMethods = $this->get_data_from_session('payment_methods'); + if (empty($localPaymentMethods)) { $this->init_configs(); $uesrIp = $this->getRealClientIP(); $userCountry = $this->get_country_by_ip($uesrIp); @@ -421,7 +417,7 @@ public function get_local_payment_methods() { } } else { - $paymentMethods = $_SESSION['local_payments']; + $paymentMethods = $localPaymentMethods; } return $paymentMethods; } @@ -439,8 +435,7 @@ public function prepare_payment_methods_from_api_response($response) { 'name' => $ps['name'], 'img_url' => !empty($ps['img_url']) ? $ps['img_url'] : '' ]; - $_SESSION['local_payments'] = $methods; - $_SESSION['local_payments_create_time'] = time(); + $this->save_data_to_session('payment_methods', $methods); } } @@ -487,22 +482,29 @@ public function update_payment_system_order_meta() { * @return mixed */ public function get_country_by_ip($ip) { - $params = array( - 'key' => $this->settings['appkey'], - 'uid' => self::USER_ID_GEOLOCATION, - 'user_ip' => $ip - ); - $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); - $response = curl_exec($curl); - if (curl_error($curl)) { - return null; - } - $response = json_decode($response, true); - if (!empty($response['code'])) { - return $response['code']; + $countryCode = $this->get_data_from_session('country_code'); + if (empty($country_code)) { + $params = array( + 'key' => $this->settings['appkey'], + 'uid' => self::USER_ID_GEOLOCATION, + 'user_ip' => $ip + ); + $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); + $response = curl_exec($curl); + if (curl_error($curl)) { + return null; + } + $response = json_decode($response, true); + if (!empty($response['code'])) { + $this->save_data_to_session('country_code', $response['code']); + return $response['code']; + } + } else { + return $countryCode; } + return null; } @@ -532,4 +534,24 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta return $total_rows; } + public function get_data_from_session($name) { + + $dataFromSession = $_SESSION['paymentwall_data']; + if ( + !empty($dataFromSession[$name]['data']) + && !empty($dataFromSession[$name]['expired_time']) + && $dataFromSession[$name]['expired_time'] > time() + ) { + return $dataFromSession[$name]['data']; + } + return null; + } + + public function save_data_to_session($name, $data) { + if (!empty($name) && !empty($data)) { + $_SESSION['paymentwall_data'][$name]['data'] = $data; + $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + } + } + } From 32c7d799d00c057f21a240eda416af9460033fdc Mon Sep 17 00:00:00 2001 From: richardtong Date: Fri, 17 Jul 2020 16:13:51 +0700 Subject: [PATCH 16/29] refactor code, --- src/includes/class-paymentwall-gateway.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 24e6168..933c5ae 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -535,22 +535,22 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta } public function get_data_from_session($name) { - - $dataFromSession = $_SESSION['paymentwall_data']; + $pwSessionData = $_SESSION['paymentwall_data']; if ( - !empty($dataFromSession[$name]['data']) - && !empty($dataFromSession[$name]['expired_time']) - && $dataFromSession[$name]['expired_time'] > time() + !empty($pwSessionData[$name]['data']) + && !empty($pwSessionData[$name]['expired_time']) + && $pwSessionData[$name]['expired_time'] > time() ) { - return $dataFromSession[$name]['data']; + return $pwSessionData[$name]['data']; } return null; } public function save_data_to_session($name, $data) { + $pwSessionData = $_SESSION['paymentwall_data']; if (!empty($name) && !empty($data)) { - $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + $pwSessionData[$name]['data'] = $data; + $pwSessionData[$name]['expired_time'] = time() + self::PS_TIME_BUFFER; } } From 263aef89e7e1d7c81362fded4767ef2229e94b83 Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:15:06 +0700 Subject: [PATCH 17/29] refactor code --- src/includes/class-paymentwall-gateway.php | 79 +++++++++++----------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 933c5ae..fbab7ce 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -35,7 +35,7 @@ public function __construct() { add_action('woocommerce_api_' . $this->id . '_gateway', array($this, 'handle_action')); add_action('woocommerce_review_order_before_payment', array($this, 'html_payment_system')); - add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); + add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); add_filter('woocommerce_order_get_payment_method_title', array($this,'get_payment_method_title'), 10, 2 ); add_filter('woocommerce_get_order_item_totals', array($this,'update_payment_title_by_selected_method'), 10, 3); @@ -259,7 +259,7 @@ function ipn_response() { if (paymentwall_subscription_enable()) { $subscriptions = wcs_get_subscriptions_for_order( $original_order_id, array( 'order_type' => 'parent' ) ); - $subscription = array_shift( $subscriptions ); + $subscription = array_shift( $subscriptions ); $subscription_key = get_post_meta($original_order_id, '_subscription_id'); } @@ -381,45 +381,45 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s * @return array|bool|string */ public function get_local_payment_methods() { + $localPaymentMethods = $this->get_data_from_session('payment_methods'); + if (!empty($localPaymentMethods)) { + return $localPaymentMethods; + } + $response = $this->get_payment_methods_from_api(); + $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); + return $localPaymentMethods; + } - $paymentMethods = []; - $localPaymentMethods = $this->get_data_from_session('payment_methods'); - if (empty($localPaymentMethods)) { - $this->init_configs(); - $uesrIp = $this->getRealClientIP(); - $userCountry = $this->get_country_by_ip($uesrIp); - - if (!empty($userCountry)) { - $params = array( - 'key' => $this->settings['appkey'], - 'country_code' => $userCountry, - 'sign_version' => 2, - 'currencyCode' => get_woocommerce_currency(), - 'amount' => WC()->cart->total - ); - - $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( - $params, - $params['sign_version'] - ); + public function get_payment_methods_from_api() { + $this->init_configs(); + $userIp = $this->getRealClientIP(); + $userCountry = $this->get_country_by_ip($userIp); - $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + if (!empty($userCountry)) { + $params = array( + 'key' => $this->settings['appkey'], + 'country_code' => $userCountry, + 'sign_version' => 3, + 'currencyCode' => get_woocommerce_currency(), + 'amount' => WC()->cart->total + ); - if (curl_error($curl)) { - return $paymentMethods; - } + $params['sign'] = (new Paymentwall_Signature_Widget())->calculate( + $params, + $params['sign_version'] + ); - $response = curl_exec($curl); - $paymentMethods = $this->prepare_payment_methods_from_api_response($response); + $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + if (curl_error($curl)) { + return null; } - - } else { - $paymentMethods = $localPaymentMethods; + $response = curl_exec($curl); + return $response; } - return $paymentMethods; + return null; } public function prepare_payment_methods_from_api_response($response) { @@ -462,8 +462,8 @@ public function html_payment_system() { } echo '
'; ?> - - + + session->pw_ps; if ( !empty($selectedPaymentMethod['name']) @@ -547,10 +547,9 @@ public function get_data_from_session($name) { } public function save_data_to_session($name, $data) { - $pwSessionData = $_SESSION['paymentwall_data']; if (!empty($name) && !empty($data)) { - $pwSessionData[$name]['data'] = $data; - $pwSessionData[$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['data'] = $data; + $_SESSION['paymentwall_data']['expired_time'] = time() + self::PS_TIME_BUFFER; } } From ac51a5be7a61c4de7050ea3cef5ace135ee435ae Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:30:31 +0700 Subject: [PATCH 18/29] check condition before prepare payment methods --- src/includes/class-paymentwall-gateway.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index fbab7ce..12a749e 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -386,7 +386,9 @@ public function get_local_payment_methods() { return $localPaymentMethods; } $response = $this->get_payment_methods_from_api(); - $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); + if (!empty($response)) { + $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); + } return $localPaymentMethods; } From 9c46c951499413ac0dc8d07edcdb452d270ce9f8 Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:38:25 +0700 Subject: [PATCH 19/29] fix typo --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 12a749e..b9259e8 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -378,7 +378,7 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s } /** - * @return array|bool|string + * @return array|null */ public function get_local_payment_methods() { $localPaymentMethods = $this->get_data_from_session('payment_methods'); From f47444ccfd56d383acf5861e7e0b0b6aca4d2ab7 Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 20 Jul 2020 18:39:41 +0700 Subject: [PATCH 20/29] fix typo --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index b9259e8..3c8d059 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -551,7 +551,7 @@ public function get_data_from_session($name) { public function save_data_to_session($name, $data) { if (!empty($name) && !empty($data)) { $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data']['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; } } From 93cae0818378935b35751f78373f918365cb98a7 Mon Sep 17 00:00:00 2001 From: richardtong Date: Tue, 21 Jul 2020 10:11:39 +0700 Subject: [PATCH 21/29] remove comment --- src/includes/class-paymentwall-gateway.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 3c8d059..0a9b420 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -478,11 +478,7 @@ public function update_payment_system_order_meta() { WC()->session->pw_ps = json_decode(stripslashes($_POST['pw_payment_system']), true); } } - - /** - * Get user country by IP - * @return mixed - */ + public function get_country_by_ip($ip) { $countryCode = $this->get_data_from_session('country_code'); if (empty($country_code)) { @@ -510,11 +506,6 @@ public function get_country_by_ip($ip) { return null; } - /** - * @param $order_id - * @param $posted_data - * @param $order - */ public function get_payment_method_title($prop, $object) { $paymentSystemName = WC()->session->pw_ps; From e421e82ddf37b52b0f04680af4c7ed8e141fb8c3 Mon Sep 17 00:00:00 2001 From: richardtong Date: Tue, 21 Jul 2020 11:55:04 +0700 Subject: [PATCH 22/29] check if session exist --- src/includes/class-paymentwall-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 0a9b420..7212a10 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -528,7 +528,7 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta } public function get_data_from_session($name) { - $pwSessionData = $_SESSION['paymentwall_data']; + $pwSessionData = !empty($_SESSION['paymentwall_data']) ? $_SESSION['paymentwall_data'] : []; if ( !empty($pwSessionData[$name]['data']) && !empty($pwSessionData[$name]['expired_time']) From 4fee6164fabbc79dc70b9c13114c1b63bf4aa028 Mon Sep 17 00:00:00 2001 From: richardtong Date: Wed, 22 Jul 2020 18:25:14 +0700 Subject: [PATCH 23/29] refactor code --- src/includes/class-paymentwall-gateway.php | 15 +++++++++------ src/paymentwall-for-woocommerce.php | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 7212a10..55f2d6b 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,7 +11,7 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; - const PS_TIME_BUFFER = 600; + const CACHED_DATA_TIME_TO_LIVE = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -258,8 +258,8 @@ function ipn_response() { } if (paymentwall_subscription_enable()) { - $subscriptions = wcs_get_subscriptions_for_order( $original_order_id, array( 'order_type' => 'parent' ) ); - $subscription = array_shift( $subscriptions ); + $subscriptions = wcs_get_subscriptions_for_order($original_order_id, array('order_type' => 'parent')); + $subscription = array_shift($subscriptions); $subscription_key = get_post_meta($original_order_id, '_subscription_id'); } @@ -413,7 +413,7 @@ public function get_payment_methods_from_api() { $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); if (curl_error($curl)) { return null; @@ -481,19 +481,22 @@ public function update_payment_system_order_meta() { public function get_country_by_ip($ip) { $countryCode = $this->get_data_from_session('country_code'); + if (empty($country_code)) { $params = array( 'key' => $this->settings['appkey'], 'uid' => self::USER_ID_GEOLOCATION, 'user_ip' => $ip ); + $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); if (curl_error($curl)) { return null; } + $response = json_decode($response, true); if (!empty($response['code'])) { $this->save_data_to_session('country_code', $response['code']); @@ -542,7 +545,7 @@ public function get_data_from_session($name) { public function save_data_to_session($name, $data) { if (!empty($name) && !empty($data)) { $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::PS_TIME_BUFFER; + $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::CACHED_DATA_TIME_TO_LIVE; } } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index eaba7e2..c23a77c 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -142,10 +142,10 @@ function check_order_has_virtual_product(WC_Order $order) { return false; } + add_action('init', 'start_sesion'); -function start_sesion(){ - if( !session_id() ) - { +function start_sesion() { + if (!session_id()) { session_start(); } } From 3d326d53a3b649f02100d6f043eda998fdca0b6e Mon Sep 17 00:00:00 2001 From: Mason Pham Date: Thu, 23 Jul 2020 13:31:44 +0200 Subject: [PATCH 24/29] Revert "Merge branch 'dev-mdl-390-2' into 'develop'" This reverts merge request !40 --- src/includes/class-paymentwall-gateway.php | 123 ++++++++------------- src/paymentwall-for-woocommerce.php | 7 -- 2 files changed, 47 insertions(+), 83 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 55f2d6b..4a75272 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -11,7 +11,6 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; - const CACHED_DATA_TIME_TO_LIVE = 600; public $id = 'paymentwall'; public $has_fields = true; @@ -35,7 +34,7 @@ public function __construct() { add_action('woocommerce_api_' . $this->id . '_gateway', array($this, 'handle_action')); add_action('woocommerce_review_order_before_payment', array($this, 'html_payment_system')); - add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); + add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); add_filter('woocommerce_order_get_payment_method_title', array($this,'get_payment_method_title'), 10, 2 ); add_filter('woocommerce_get_order_item_totals', array($this,'update_payment_title_by_selected_method'), 10, 3); @@ -258,8 +257,8 @@ function ipn_response() { } if (paymentwall_subscription_enable()) { - $subscriptions = wcs_get_subscriptions_for_order($original_order_id, array('order_type' => 'parent')); - $subscription = array_shift($subscriptions); + $subscriptions = wcs_get_subscriptions_for_order( $original_order_id, array( 'order_type' => 'parent' ) ); + $subscription = array_shift( $subscriptions ); $subscription_key = get_post_meta($original_order_id, '_subscription_id'); } @@ -378,30 +377,21 @@ public function add_feature_support_for_subscription($is_supported, $feature, $s } /** - * @return array|null + * @return array|bool|string */ - public function get_local_payment_methods() { - $localPaymentMethods = $this->get_data_from_session('payment_methods'); - if (!empty($localPaymentMethods)) { - return $localPaymentMethods; - } - $response = $this->get_payment_methods_from_api(); - if (!empty($response)) { - $localPaymentMethods = $this->prepare_payment_methods_from_api_response($response); - } - return $localPaymentMethods; - } + public function get_local_payments() { + + $paymentMethods = []; - public function get_payment_methods_from_api() { $this->init_configs(); - $userIp = $this->getRealClientIP(); - $userCountry = $this->get_country_by_ip($userIp); + $uesrIp = $this->getRealClientIP(); + $userCountry = $this->get_country_by_ip($uesrIp); if (!empty($userCountry)) { $params = array( - 'key' => $this->settings['appkey'], - 'country_code' => $userCountry, - 'sign_version' => 3, + 'key' => $this->settings['appkey'], + 'country_code' => $userCountry , + 'sign_version' => 2, 'currencyCode' => get_woocommerce_currency(), 'amount' => WC()->cart->total ); @@ -413,15 +403,17 @@ public function get_payment_methods_from_api() { $url = Paymentwall_Config::API_BASE_URL . '/payment-systems/?' . http_build_query($params); $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); if (curl_error($curl)) { - return null; + return $paymentMethods; } + $response = curl_exec($curl); - return $response; + $paymentMethods = $this->prepare_payment_methods_from_api_response($response); + } - return null; + return $paymentMethods; } public function prepare_payment_methods_from_api_response($response) { @@ -437,7 +429,6 @@ public function prepare_payment_methods_from_api_response($response) { 'name' => $ps['name'], 'img_url' => !empty($ps['img_url']) ? $ps['img_url'] : '' ]; - $this->save_data_to_session('payment_methods', $methods); } } @@ -445,7 +436,7 @@ public function prepare_payment_methods_from_api_response($response) { } public function html_payment_system() { - $paymentSystems = $this->get_local_payment_methods(); + $paymentSystems = $this->get_local_payments(); if (is_array($paymentSystems) && !empty($paymentSystems)) { echo '
    '; foreach ($paymentSystems as $gateway) { @@ -464,8 +455,8 @@ public function html_payment_system() { } echo '
'; ?> - - + + session->pw_ps = json_decode(stripslashes($_POST['pw_payment_system']), true); } } - - public function get_country_by_ip($ip) { - $countryCode = $this->get_data_from_session('country_code'); - - if (empty($country_code)) { - $params = array( - 'key' => $this->settings['appkey'], - 'uid' => self::USER_ID_GEOLOCATION, - 'user_ip' => $ip - ); - $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($curl); - if (curl_error($curl)) { - return null; - } - - $response = json_decode($response, true); - if (!empty($response['code'])) { - $this->save_data_to_session('country_code', $response['code']); - return $response['code']; - } - } else { - return $countryCode; + /** + * Get user country by IP + * @return mixed + */ + public function get_country_by_ip($ip) { + $params = array( + 'key' => $this->settings['appkey'], + 'uid' => self::USER_ID_GEOLOCATION, + 'user_ip' => $ip + ); + $url = Paymentwall_Config::API_BASE_URL . '/rest/country?' . http_build_query($params); + $curl = curl_init($url); + curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE); + $response = curl_exec($curl); + if (curl_error($curl)) { + return null; + } + $response = json_decode($response, true); + if (!empty($response['code'])) { + return $response['code']; } - return null; } + /** + * @param $order_id + * @param $posted_data + * @param $order + */ public function get_payment_method_title($prop, $object) { $paymentSystemName = WC()->session->pw_ps; @@ -518,7 +508,7 @@ public function get_payment_method_title($prop, $object) return $prop; } - public function update_payment_title_by_selected_method($total_rows, $order, $tax_display) { + public function update_payment_title_by_selected_method($total_rows, $order, $tax_display){ $selectedPaymentMethod = WC()->session->pw_ps; if ( !empty($selectedPaymentMethod['name']) @@ -530,23 +520,4 @@ public function update_payment_title_by_selected_method($total_rows, $order, $ta return $total_rows; } - public function get_data_from_session($name) { - $pwSessionData = !empty($_SESSION['paymentwall_data']) ? $_SESSION['paymentwall_data'] : []; - if ( - !empty($pwSessionData[$name]['data']) - && !empty($pwSessionData[$name]['expired_time']) - && $pwSessionData[$name]['expired_time'] > time() - ) { - return $pwSessionData[$name]['data']; - } - return null; - } - - public function save_data_to_session($name, $data) { - if (!empty($name) && !empty($data)) { - $_SESSION['paymentwall_data'][$name]['data'] = $data; - $_SESSION['paymentwall_data'][$name]['expired_time'] = time() + self::CACHED_DATA_TIME_TO_LIVE; - } - } - } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index c23a77c..b75ccfe 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -142,10 +142,3 @@ function check_order_has_virtual_product(WC_Order $order) { return false; } - -add_action('init', 'start_sesion'); -function start_sesion() { - if (!session_id()) { - session_start(); - } -} From 55dcb491a72ed3dfa373312f4cc56d6d1e580b2d Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 21 Sep 2020 17:50:18 +0700 Subject: [PATCH 25/29] [MDL-398] Hide payment methodts if Paymentwall is deactivated in Woocommerce settings --- src/includes/class-paymentwall-gateway.php | 50 ++++++++++++++-------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 4e9b205..24b141d 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -459,28 +459,40 @@ public function prepare_payment_methods_from_api_response($response) { } public function html_payment_system() { - $paymentMethods = $this->get_local_payment_methods(); - if (is_array($paymentMethods) && !empty($paymentMethods)) { - echo '
    '; - foreach ($paymentMethods as $gateway) { - $dataPaymentSystem = array( - 'id' => $gateway['id'], - 'name' => $gateway['name'] - ); + if ($this->enabled == 'yes') { + $paymentMethods = $this->get_local_payment_methods(); + if (is_array($paymentMethods) && !empty($paymentMethods)) { + echo '
      '; + foreach ($paymentMethods as $gateway) { + $dataPaymentSystem = array( + 'id' => $gateway['id'], + 'name' => $gateway['name'] + ); + ?> +
    • + + +
    • + '; ?> -
    • - - -
    • + + '; - ?> - - - Date: Tue, 22 Sep 2020 17:39:08 +0700 Subject: [PATCH 26/29] [MDL-398] - Don't show PSs if plugin is deactivated, use add_filter to add 'paymentwall' method into payments list --- src/includes/class-paymentwall-gateway.php | 41 +----------- src/paymentwall-for-woocommerce.php | 78 ++++++++++++++++++++++ 2 files changed, 79 insertions(+), 40 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 24b141d..d18278f 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -9,7 +9,7 @@ * */ class Paymentwall_Gateway extends Paymentwall_Abstract { - + const PAYMENTWALL_METHOD = 'paymentwall'; const USER_ID_GEOLOCATION = 'user101'; const CACHED_DATA_TIME_TO_LIVE = 300; @@ -34,7 +34,6 @@ public function __construct() { add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); add_action('woocommerce_api_' . $this->id . '_gateway', array($this, 'handle_action')); - add_action('woocommerce_review_order_before_payment', array($this, 'html_payment_system')); add_action('woocommerce_checkout_update_order_meta', array($this,'update_payment_system_order_meta')); add_filter('woocommerce_order_get_payment_method_title', array($this,'get_payment_method_title'), 10, 2 ); add_filter('woocommerce_get_order_item_totals', array($this,'update_payment_title_by_selected_method'), 10, 3); @@ -458,44 +457,6 @@ public function prepare_payment_methods_from_api_response($response) { return $methods; } - public function html_payment_system() { - if ($this->enabled == 'yes') { - $paymentMethods = $this->get_local_payment_methods(); - if (is_array($paymentMethods) && !empty($paymentMethods)) { - echo '
        '; - foreach ($paymentMethods as $gateway) { - $dataPaymentSystem = array( - 'id' => $gateway['id'], - 'name' => $gateway['name'] - ); - ?> -
      • - - -
      • - '; - ?> - - - is_available()) { + return $availableGateways; + } + + if (!array_key_exists(Paymentwall_Gateway::PAYMENTWALL_METHOD, $availableGateways)) { + $availableGateways[$paymentwallGateway->id] = $paymentwallGateway; + } + } + + return $availableGateways; +} + +add_filter('woocommerce_available_payment_gateways', 'addBrickGateway', 100, 2); +function addBrickGateway($availableGateways) +{ + + if (is_checkout() || is_checkout_pay_page()){ + + $brickGateway = new Paymentwall_Brick(); + + if (!$brickGateway->is_available()) { + return $availableGateways; + } + + if (!array_key_exists(Paymentwall_Brick::BRICK_METHOD, $availableGateways)) { + $availableGateways[$brickGateway->id] = $brickGateway; + } + } + return $availableGateways; +} + +add_action('woocommerce_review_order_before_payment', 'html_payment_system'); +function html_payment_system() { + $paymentwallGateway = new Paymentwall_Gateway(); + if ($paymentwallGateway->is_available()) { + $paymentMethods = $paymentwallGateway->get_local_payment_methods(); + if (is_array($paymentMethods) && !empty($paymentMethods)) { + echo '
          '; + foreach ($paymentMethods as $gateway) { + $dataPaymentSystem = array( + 'id' => $gateway['id'], + 'name' => $gateway['name'] + ); + ?> +
        • + + +
        • + '; + ?> + + + Date: Wed, 23 Sep 2020 15:18:24 +0700 Subject: [PATCH 27/29] Revert 1 line unnecessary --- src/includes/class-paymentwall-gateway.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index d18278f..37d845d 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -9,6 +9,7 @@ * */ class Paymentwall_Gateway extends Paymentwall_Abstract { + const PAYMENTWALL_METHOD = 'paymentwall'; const USER_ID_GEOLOCATION = 'user101'; const CACHED_DATA_TIME_TO_LIVE = 300; From 251ac5c4519e38062825eaae68f68d9a91c3b5c5 Mon Sep 17 00:00:00 2001 From: Mason Date: Thu, 1 Oct 2020 16:02:49 +0700 Subject: [PATCH 28/29] [MDL-398] Update version tag --- src/paymentwall-for-woocommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 6bda77e..9c4b551 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -5,7 +5,7 @@ * Plugin Name: Paymentwall for WooCommerce * Plugin URI: https://www.paymentwall.com/en/documentation/WooCommerce/1409 * Description: Official Paymentwall module for WordPress WooCommerce. - * Version: 1.7.1 + * Version: 1.7.2 * Author: The Paymentwall Team * Author URI: http://www.paymentwall.com/ * Text Domain: paymentwall-for-woocommerce From 397493d3ac56c381154168503f47b60276d85ab3 Mon Sep 17 00:00:00 2001 From: Mason Date: Thu, 1 Oct 2020 16:25:19 +0700 Subject: [PATCH 29/29] [MDL-398] Refactor code --- src/includes/class-paymentwall-gateway.php | 2 +- src/paymentwall-for-woocommerce.php | 69 +++++++++++----------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 37d845d..1f803eb 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -14,7 +14,7 @@ class Paymentwall_Gateway extends Paymentwall_Abstract { const USER_ID_GEOLOCATION = 'user101'; const CACHED_DATA_TIME_TO_LIVE = 300; - public $id = 'paymentwall'; + public $id = self::PAYMENTWALL_METHOD; public $has_fields = true; public function __construct() { diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9c4b551..9ef9900 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -162,7 +162,7 @@ function addPaymentwallGateway($availableGateways){ } if (!array_key_exists(Paymentwall_Gateway::PAYMENTWALL_METHOD, $availableGateways)) { - $availableGateways[$paymentwallGateway->id] = $paymentwallGateway; + $availableGateways[Paymentwall_Gateway::PAYMENTWALL_METHOD] = $paymentwallGateway; } } @@ -182,7 +182,7 @@ function addBrickGateway($availableGateways) } if (!array_key_exists(Paymentwall_Brick::BRICK_METHOD, $availableGateways)) { - $availableGateways[$brickGateway->id] = $brickGateway; + $availableGateways[Paymentwall_Brick::BRICK_METHOD] = $brickGateway; } } return $availableGateways; @@ -191,39 +191,42 @@ function addBrickGateway($availableGateways) add_action('woocommerce_review_order_before_payment', 'html_payment_system'); function html_payment_system() { $paymentwallGateway = new Paymentwall_Gateway(); - if ($paymentwallGateway->is_available()) { - $paymentMethods = $paymentwallGateway->get_local_payment_methods(); - if (is_array($paymentMethods) && !empty($paymentMethods)) { - echo '
            '; - foreach ($paymentMethods as $gateway) { - $dataPaymentSystem = array( - 'id' => $gateway['id'], - 'name' => $gateway['name'] - ); - ?> -
          • - - -
          • - '; + + if (!$paymentwallGateway->is_available()) { + return; + } + + $paymentMethods = $paymentwallGateway->get_local_payment_methods(); + if (is_array($paymentMethods) && !empty($paymentMethods)) { + echo '
              '; + foreach ($paymentMethods as $gateway) { + $dataPaymentSystem = array( + 'id' => $gateway['id'], + 'name' => $gateway['name'] + ); ?> - - +
            • + + +
            • '; + ?> + + +