Skip to content

Commit

Permalink
Managed Form (#32)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wajihkm authored Apr 6, 2022
1 parent be9ad03 commit e891501
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 28 deletions.
67 changes: 67 additions & 0 deletions includes/_managed_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

$_client_key = $this->client_key;

$_js_path = 'payment/js/paylib.js';
$_js_url = $this->get_endpoint_url() . $_js_path;
?>

<script src="<?= $_js_url ?>"></script>

<div id="pt_managed_form" class="form-row woocommerce-SavedPaymentMethods-saveNew">

<div>
<span style="color: red;" id="paymentErrors"></span>
<div class="row">
<label>Card Number</label>
<input type="text" data-paylib="number" size="20">
</div>
<div class="row">
<label>Expiry Date (MM/YYYY)</label>
<input type="text" data-paylib="expmonth" size="2">
<input type="text" data-paylib="expyear" size="4">
</div>
<div class="row">
<label>Security Code</label>
<input type="text" data-paylib="cvv" size="4">
<input type="hidden" name="token" id="pt_token">
</div>

</div>
</div>


<script type="text/javascript">
var myform = document.getElementsByName('checkout')[0];

var checkout_form = jQuery('form.checkout');

var mf_confirmed = false;

paylib.inlineForm({
'key': '<?= $_client_key ?>',
'form': myform,
'autoSubmit': false,
'callback': function(response) {
console.log(response);
document.getElementById('paymentErrors').innerHTML = '';
if (response.error) {
paylib.handleError(document.getElementById('paymentErrors'), response);
} else {
if (!mf_confirmed) {
jQuery('#pt_token').val(response.token);
mf_confirmed = true;

checkout_form.submit();
}
}
}
});

checkout_form.on('checkout_place_order', function(event, params) {
// console.log('on_place_order', event);
var isFormHidden = jQuery('#pt_managed_form').is(':hidden');

return mf_confirmed || isFormHidden;
});
</script>
23 changes: 14 additions & 9 deletions includes/paytabs_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');



Expand Down Expand Up @@ -863,21 +862,21 @@ 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]],
'4' => ['name' => 'omannet', 'title' => 'PayTabs - OmanNet', 'currencies' => ['OMR'], 'groups' => [PaytabsApi::GROUP_TOKENIZE, PaytabsApi::GROUP_CARDS, PaytabsApi::GROUP_IFRAME]],
'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' => []],
Expand Down Expand Up @@ -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) {
Expand Down
74 changes: 58 additions & 16 deletions includes/paytabs_payment_methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
*/
Expand All @@ -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'),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions paytabs-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* Plugin URI: https://paytabs.com/
* Description: PayTabs is a <strong>3rd party payment gateway</strong>. Ideal payment solutions for your internet business.
* Version: 4.10.0
* Version: 4.11.0
* Requires PHP: 7.0
* Author: PayTabs
* Author URI: [email protected]
* Revision Date: 17/February/2022
* Revision Date: 03/April/2022
*/

if (!function_exists('add_action')) {
Expand All @@ -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__));
Expand Down

0 comments on commit e891501

Please sign in to comment.