From 6fea84d01f62374f588e5e73c9e3faae29bae239 Mon Sep 17 00:00:00 2001 From: MAboyadak Date: Fri, 24 Nov 2023 13:19:09 +0200 Subject: [PATCH] Alt currency (#64) * Adding alternative currency feature * Update SDK --- includes/paytabs_core.php | 28 +++++++++++++--- includes/paytabs_payment_methods.php | 48 +++++++++++++++++++++++++--- paytabs-woocommerce.php | 4 +-- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/includes/paytabs_core.php b/includes/paytabs_core.php index 1e4ca50..0caac1e 100755 --- a/includes/paytabs_core.php +++ b/includes/paytabs_core.php @@ -2,11 +2,11 @@ /** * PayTabs v2 PHP SDK - * Version: 2.14.0 + * Version: 2.15.0 * PHP >= 7.0.0 */ -define('PAYTABS_SDK_VERSION', '2.14.0'); +define('PAYTABS_SDK_VERSION', '2.15.0'); define('PAYTABS_DEBUG_FILE_NAME', 'debug_paytabs.log'); define('PAYTABS_DEBUG_SEVERITY', ['Info', 'Warning', 'Error']); @@ -737,6 +737,11 @@ class PaytabsRequestHolder extends PaytabsBasicHolder */ private $config_id; + /** + * alt_currency + */ + private $alt_currency; + // /** @@ -750,7 +755,8 @@ public function pt_build() $all, $this->hide_shipping, $this->framed, - $this->config_id + $this->config_id, + $this->alt_currency ); return $all; @@ -781,9 +787,9 @@ public function set09Framed(bool $on = false, $redirect_target = 'iframe') public function set11ThemeConfigId($config_id) { - $config_id = (int) trim($config_id); + $config_id = (int) trim($config_id ?? ""); - if (isset($config_id) && (is_int($config_id) && $config_id > 0)) { + if (is_int($config_id) && $config_id > 0) { $this->config_id = [ 'config_id' => $config_id ]; @@ -791,6 +797,18 @@ public function set11ThemeConfigId($config_id) return $this; } + + public function set12AltCurrency($alt_currency) + { + $alt_currency = trim($alt_currency ?? ""); + + if (!empty($alt_currency)) { + $this->alt_currency = [ + 'alt_currency' => $alt_currency + ]; + } + return $this; + } } diff --git a/includes/paytabs_payment_methods.php b/includes/paytabs_payment_methods.php index a2d8df4..975751c 100755 --- a/includes/paytabs_payment_methods.php +++ b/includes/paytabs_payment_methods.php @@ -14,6 +14,10 @@ class WC_Gateway_Paytabs extends WC_Payment_Gateway // Select the PayPage to use private $theme_config_id; + // Alt currency + private $alt_currency_enable; + private $alt_currency; + // const PT_HANDLED = '_pt_handled'; @@ -104,6 +108,9 @@ public function __construct() $this->theme_config_id = $this->get_option('theme_config_id', ''); + $this->alt_currency_enable = $this->get_option('alt_currency_enable', "no") == 'yes'; + $this->alt_currency = $this->get_option('alt_currency', ''); + // This action hook saves the settings add_action("woocommerce_update_options_payment_gateways_{$this->id}", array($this, 'process_admin_options')); @@ -342,6 +349,20 @@ public function init_form_fields() 'description' => "Config id of the theme/payment page (if any) you want to open, You may find it in Dashboard > Developers > PayPage Settings (Themes)", 'default' => '', 'required' => false + ), + 'alt_currency_enable' => array( + 'title' => __('Enable alternative currency', 'PayTabs'), + 'type' => 'checkbox', + 'description' => "Display alternative currency equivalent in the payment page.", + 'default' => "no", + 'required' => false + ), + 'alt_currency' => array( + 'title' => __('Alternative currency', 'PayTabs'), + 'type' => 'text', + 'description' => 'The alternative currency to be shown in the payment page, e.g. "USD", "AED, "SAR"', + 'default' => '', + 'required' => false ) ); @@ -1515,8 +1536,13 @@ private function prepareOrder($order, $isTokenize = false) ->set08Lang($lang) ->set09Framed($this->is_frammed_page, 'top') ->set10Tokenise($tokenise) - ->set11ThemeConfigId($this->theme_config_id) - ->set99PluginInfo('WooCommerce', $woocommerce->version, PAYTABS_PAYPAGE_VERSION); + ->set11ThemeConfigId($this->theme_config_id); + + if ($this->alt_currency_enable) { + $holder->set12AltCurrency($this->getAltCurrency()); + } + + $holder->set99PluginInfo('WooCommerce', $woocommerce->version, PAYTABS_PAYPAGE_VERSION); if ($this->_code == 'valu') { // $holder->set20ValuParams($this->valu_product_id, 0); @@ -1629,8 +1655,13 @@ private function prepareOrder2($order, $isTokenize = false) ->set08Lang($lang) ->set09Framed($this->is_frammed_page, 'top') ->set10Tokenise($tokenise) - ->set11ThemeConfigId($this->theme_config_id) - ->set99PluginInfo('WooCommerce', $woocommerce->version, PAYTABS_PAYPAGE_VERSION); + ->set11ThemeConfigId($this->theme_config_id); + + if ($this->alt_currency_enable) { + $holder->set12AltCurrency($this->getAltCurrency()); + } + + $holder->set99PluginInfo('WooCommerce', $woocommerce->version, PAYTABS_PAYPAGE_VERSION); if ($this->_code == 'valu') { // $holder->set20ValuParams($this->valu_product_id, 0); @@ -1706,4 +1737,13 @@ private function getPaymentMethod($order) { return WooCommerce2 ? $order->payment_method : $order->get_payment_method(); } + + private function getAltCurrency() + { + /* + / any logic needed in the future + */ + + return $this->alt_currency; + } } diff --git a/paytabs-woocommerce.php b/paytabs-woocommerce.php index c39f9fe..4c8f5ec 100644 --- a/paytabs-woocommerce.php +++ b/paytabs-woocommerce.php @@ -9,7 +9,7 @@ * Plugin URI: https://paytabs.com/ * Description: PayTabs is a 3rd party payment gateway. Ideal payment solutions for your internet business. - * Version: 4.19.0 + * Version: 4.20.0 * Requires PHP: 7.0 * Author: PayTabs * Author URI: integration@paytabs.com @@ -20,7 +20,7 @@ } -define('PAYTABS_PAYPAGE_VERSION', '4.19.0'); +define('PAYTABS_PAYPAGE_VERSION', '4.20.0'); define('PAYTABS_PAYPAGE_DIR', plugin_dir_path(__FILE__)); define('PAYTABS_PAYPAGE_ICONS_URL', plugins_url("icons/", __FILE__)); define('PAYTABS_PAYPAGE_IMAGES_URL', plugins_url("images/", __FILE__));