From e8915017d430203840af0a5a986e6786c18b5ac0 Mon Sep 17 00:00:00 2001 From: Wajih Date: Wed, 6 Apr 2022 11:34:42 +0400 Subject: [PATCH] Managed Form (#32) * Add the managed form to checkout page * Add the managed form option to admin panel Handle display the managed form in checkout * Process ManagedForm payment request * Add Client key to the admin panel * Dynamic JS file based on the region * Handle Managed form with Tokenization option * SDK Core update with the correct iFrame groups * Run Tokenization routine only for logged-in users --- includes/_managed_form.php | 67 +++++++++++++++++++++++++ includes/paytabs_core.php | 23 +++++---- includes/paytabs_payment_methods.php | 74 ++++++++++++++++++++++------ paytabs-woocommerce.php | 6 +-- 4 files changed, 142 insertions(+), 28 deletions(-) create mode 100644 includes/_managed_form.php diff --git a/includes/_managed_form.php b/includes/_managed_form.php new file mode 100644 index 0000000..9ff7861 --- /dev/null +++ b/includes/_managed_form.php @@ -0,0 +1,67 @@ +client_key; + +$_js_path = 'payment/js/paylib.js'; +$_js_url = $this->get_endpoint_url() . $_js_path; +?> + + + +
+ +
+ +
+ + +
+
+ + + +
+
+ + + +
+ +
+
+ + + \ No newline at end of file diff --git a/includes/paytabs_core.php b/includes/paytabs_core.php index 5247974..324e390 100755 --- a/includes/paytabs_core.php +++ b/includes/paytabs_core.php @@ -2,11 +2,10 @@ /** * PayTabs v2 PHP SDK - - * Version: 2.7.6 + * Version: 2.7.7 */ -define('PAYTABS_SDK_VERSION', '2.7.6'); +define('PAYTABS_SDK_VERSION', '2.7.7'); @@ -863,7 +862,7 @@ class PaytabsApi const GROUP_IFRAME = 'iframe'; const PAYMENT_TYPES = [ - '0' => ['name' => 'all', 'title' => 'PayTabs - All', 'currencies' => null, 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_AUTH_CAPTURE]], + '0' => ['name' => 'all', 'title' => 'PayTabs - All', 'currencies' => null, 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], '1' => ['name' => 'stcpay', 'title' => 'PayTabs - StcPay', 'currencies' => ['SAR'], 'groups' => [PaytabsApi::GROUP_IFRAME]], '2' => ['name' => 'stcpayqr', 'title' => 'PayTabs - StcPay(QR)', 'currencies' => ['SAR'], 'groups' => []], '3' => ['name' => 'applepay', 'title' => 'PayTabs - ApplePay', 'currencies' => ['AED', 'SAR'], 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_AUTH_CAPTURE]], @@ -871,13 +870,13 @@ class PaytabsApi '5' => ['name' => 'mada', 'title' => 'PayTabs - Mada', 'currencies' => ['SAR'], 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], '6' => ['name' => 'creditcard', 'title' => 'PayTabs - CreditCard', 'currencies' => null, 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_CARDS_INTERNATIONAL, PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], '7' => ['name' => 'sadad', 'title' => 'PayTabs - Sadad', 'currencies' => ['SAR'], 'groups' => []], - '8' => ['name' => 'fawry', 'title' => 'PayTabs - @Fawry', 'currencies' => ['EGP'], 'groups' => []], - '9' => ['name' => 'knet', 'title' => 'PayTabs - KnPay', 'currencies' => ['KWD'], 'groups' => [PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_IFRAME]], + '8' => ['name' => 'fawry', 'title' => 'PayTabs - @Fawry', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME]], + '9' => ['name' => 'knet', 'title' => 'PayTabs - KnPay', 'currencies' => ['KWD'], 'groups' => [PaytabsApi::GROUP_CARDS]], '10' => ['name' => 'amex', 'title' => 'PayTabs - Amex', 'currencies' => ['AED', 'SAR'], 'groups' => [PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_CARDS_INTERNATIONAL, PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], - '11' => ['name' => 'valu', 'title' => 'PayTabs - valU', 'currencies' => ['EGP'], 'groups' => []], + '11' => ['name' => 'valu', 'title' => 'PayTabs - valU', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME]], '12' => ['name' => 'meeza', 'title' => 'PayTabs - Meeza', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], - '13' => ['name' => 'meezaqr', 'title' => 'PayTabs - Meeza (QR)', 'currencies' => ['EGP'], 'groups' => []], - '14' => ['name' => 'unionpay', 'title' => 'PayTabs - UnionPay', 'currencies' => ['AED'], 'groups' => [PaytabsApi::GROUP_AUTH_CAPTURE, PaytabsApi::GROUP_IFRAME]], + '13' => ['name' => 'meezaqr', 'title' => 'PayTabs - Meeza (QR)', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME]], + '14' => ['name' => 'unionpay', 'title' => 'PayTabs - UnionPay', 'currencies' => ['AED'], 'groups' => [PaytabsApi::GROUP_AUTH_CAPTURE]], '15' => ['name' => 'samsungpay', 'title' => 'PayTabs - SamsungPay', 'currencies' => ['AED', 'SAR'], 'groups' => []], '16' => ['name' => 'knetdebit', 'title' => 'PayTabs - KnPay (Debit)', 'currencies' => ['KWD'], 'groups' => []], '17' => ['name' => 'knetcredit', 'title' => 'PayTabs - KnPay (Credit)', 'currencies' => ['KWD'], 'groups' => []], @@ -945,6 +944,12 @@ public static function getEndpoints() return $endpoints; } + public static function getEndpoint($region) + { + $endpoint = self::BASE_URLS[$region]['endpoint']; + return $endpoint; + } + public static function getInstance($region, $merchant_id, $key) { if (self::$instance == null) { diff --git a/includes/paytabs_payment_methods.php b/includes/paytabs_payment_methods.php index 79a0269..180b122 100755 --- a/includes/paytabs_payment_methods.php +++ b/includes/paytabs_payment_methods.php @@ -68,13 +68,16 @@ public function __construct() $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); $this->enabled = $this->get_option('enabled'); + $this->payment_form = $this->get_option('payment_form'); $this->is_frammed_page = ($this->payment_form === 'iframe'); + $this->is_managed_form = ($this->payment_form === "managed_form"); // PT $this->paytabs_endpoint = $this->get_option('endpoint'); $this->merchant_id = $this->get_option('profile_id'); $this->merchant_key = $this->get_option('server_key'); + $this->client_key = $this->get_option('client_key'); $this->hide_shipping = $this->get_option('hide_shipping') == 'yes'; @@ -152,6 +155,12 @@ private function get_ipn_url() return $ipn_url; } + + private function get_endpoint_url() + { + return PaytabsApi::getEndpoint($this->paytabs_endpoint); + } + /** * Plugin options */ @@ -169,6 +178,15 @@ public function init_form_fields() $addional_fields = []; + $redirect_modes = [ + 'redirect' => __('Redirect to hosted form on PayTabs server', 'PayTabs'), + 'iframe' => __('iFrame payment form integrated into checkout', 'PayTabs') + ]; + if ($this->_code == 'creditcard') { + $redirect_modes['managed_form'] = __('Managed form'); + } + + if ($this->_is_card_method) { $addional_fields['allow_associated_methods'] = [ 'title' => __('Allow associated methods', 'PayTabs'), @@ -213,10 +231,7 @@ public function init_form_fields() $addional_fields['payment_form'] = [ 'title' => __('Payment form type', 'PayTabs'), 'type' => 'select', - 'options' => array( - 'redirect' => __('Redirect to hosted form on PayTabs server', 'PayTabs'), - 'iframe' => __('iFrame payment form integrated into checkout', 'PayTabs'), - ), + 'options' => $redirect_modes, 'description' => __("Hosted form on PayTabs server is the secure solution of choice, While iFrame provides better customer experience (https strongly advised)", 'PayTabs'), 'default' => 'redirect', 'desc_tip' => false, @@ -265,6 +280,13 @@ public function init_form_fields() 'default' => '', 'required' => true ), + 'client_key' => array( + 'title' => __('Client Key', 'PayTabs'), + 'type' => 'text', + 'description' => __('Please enter your PayTabs "Client Key". You can find it on your Merchant’s Portal', 'PayTabs'), + 'default' => '', + 'required' => true + ), 'hide_shipping' => array( 'title' => __('Hide shipping info', 'PayTabs'), 'label' => __('Hide shipping info', 'PayTabs'), @@ -319,24 +341,27 @@ function payment_fields() { if ($this->description) echo wpautop(wptexturize($this->description)); - if (!$this->supports('tokenization') || !$this->enable_tokenise) { - return; - } - if (!is_checkout()) { return; } - $this->tokenization_script(); - $this->saved_payment_methods(); + if (is_user_logged_in()) { + if ($this->supports('tokenization') && $this->enable_tokenise) { + $this->tokenization_script(); + $this->saved_payment_methods(); - $has_subscription = class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription(); - if ($has_subscription) { - echo wpautop('Will Save to Account'); - } else { - $this->save_payment_method_checkbox(); + $has_subscription = class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription(); + if ($has_subscription) { + echo wpautop('Will Save to Account'); + } else { + $this->save_payment_method_checkbox(); + } + } + } + + if ($this->is_managed_form) { + include_once('_managed_form.php'); } - // $this->form(); } @@ -404,6 +429,8 @@ public function process_payment($order_id) 'result' => 'success', 'redirect' => $order->get_checkout_payment_url(true) . "&t={$this->is_tokenise()}" ); + } elseif ($this->is_managed_form) { + $values = $this->prepareOrder_ManagedForm($order); } else { $values = WooCommerce2 ? $this->prepareOrder2($order) : $this->prepareOrder($order); } @@ -1499,6 +1526,21 @@ private function prepareOrder_Tokenised($order, $tokenObj, $amount_to_charge = n // + private function prepareOrder_ManagedForm($order) + { + // ToDo: + // Use ManagedForm holder class + + $values = WooCommerce2 ? $this->prepareOrder2($order) : $this->prepareOrder($order); + + $payment_token = filter_input(INPUT_POST, 'token'); + $values['payment_token'] = $payment_token; + + return $values; + } + + // + private function getPaymentMethod($order) { return WooCommerce2 ? $order->payment_method : $order->get_payment_method(); diff --git a/paytabs-woocommerce.php b/paytabs-woocommerce.php index f2040e5..250c469 100644 --- a/paytabs-woocommerce.php +++ b/paytabs-woocommerce.php @@ -9,11 +9,11 @@ * Plugin URI: https://paytabs.com/ * Description: PayTabs is a 3rd party payment gateway. Ideal payment solutions for your internet business. - * Version: 4.10.0 + * Version: 4.11.0 * Requires PHP: 7.0 * Author: PayTabs * Author URI: w.kammoun@paytabs.com - * Revision Date: 17/February/2022 + * Revision Date: 03/April/2022 */ if (!function_exists('add_action')) { @@ -22,7 +22,7 @@ -define('PAYTABS_PAYPAGE_VERSION', '4.10.0'); +define('PAYTABS_PAYPAGE_VERSION', '4.11.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__));