Skip to content

Commit

Permalink
Tamara payment method (#80)
Browse files Browse the repository at this point in the history
* Tamara payment method

* Packages update

* add a default priority field on load
  • Loading branch information
Walaa-Elsaeed-93 authored Jul 24, 2024
1 parent 34dface commit dc0a559
Show file tree
Hide file tree
Showing 8 changed files with 1,550 additions and 1,642 deletions.
2 changes: 1 addition & 1 deletion assets/js/frontend/blocks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-html-entities', 'wp-i18n'), 'version' => '42876f73af7e658f29e0');
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-html-entities', 'wp-i18n'), 'version' => 'e7e5fc1b719107d77a08');
2 changes: 1 addition & 1 deletion assets/js/frontend/blocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions icons/tamara.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 132 additions & 3 deletions includes/paytabs_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

/**
* PayTabs v2 PHP SDK
* Version: 2.21.0
* Version: 2.22.0
* PHP >= 7.0.0
*/

define('PAYTABS_SDK_VERSION', '2.21.0');
define('PAYTABS_SDK_VERSION', '2.22.0');

define('PAYTABS_DEBUG_FILE_NAME', 'debug_paytabs.log');
define('PAYTABS_DEBUG_SEVERITY', ['Info', 'Warning', 'Error']);
Expand All @@ -20,6 +20,16 @@ static function paymentType($key)
return PaytabsApi::PAYMENT_TYPES[$key]['name'];
}

static function getPaymentMethodDetails($code)
{
foreach (PaytabsApi::PAYMENT_TYPES as $k => $v) {
if ($v['name'] == $code) {
return $v;
}
}
return false;
}

static function paymentAllowed($code, $currencyCode)
{
$row = null;
Expand Down Expand Up @@ -272,6 +282,119 @@ static function isValidDiscountPatterns($patterns_str)

return true;
}

/** Checks if the PT payload has applied a valid discount.
* @return bool
*/
static function hasDiscountApplied($patterns, $amounts, $types, $pt_response)
{
$applied_discounts = self::getAppliedDiscounts($patterns, $pt_response);
if (empty($applied_discounts)) return false;

$cart_amount = (float)$pt_response->cart_amount;
$tran_total = (float)$pt_response->tran_total;
if ($cart_amount == $tran_total) return false;

$_idxs = implode(',', $applied_discounts);
PaytabsHelper::log("Discount flag detected, Amount: {$cart_amount} >> {$tran_total}, Indexes [{$_idxs}]", 1);

foreach ($applied_discounts as $i) {
$discount_amount = $amounts[$i];
$discount_type = $types[$i];

$valid_discount = self::isValidDiscount($discount_type, $discount_amount, $cart_amount, $tran_total);
if ($valid_discount) {
return $i;
}
}

return false;
}

/**
* If the response payload contains the Discount flag set
* And if the used card in the trx matches any of the card patterns in the system
* @return array of indexes of the used card patterns
*/
static function getAppliedDiscounts($patterns, $pt_response, $udf_key = 'udf3')
{
$user_defined = @$pt_response->user_defined;
if (!$user_defined) return false;

$udf3 = @$user_defined->$udf_key;
if (!$udf3) return false;

if ($udf3 != PaytabsEnum::DISCOUNT_FLAG) return false;

$card_used = @$pt_response->payment_info->payment_description;
if (!$card_used) return false;

$card_used = substr(str_replace(' ', '', $card_used), 0, 6);

$count = count($patterns);
if ($count < 1) return false;

$indexes = [];

$card_used_length = strlen($card_used);
for ($i = 0; $i < $count; $i++) {
$pattern_str = $patterns[$i];
$patterns_arr = explode(',', $pattern_str);
foreach ($patterns_arr as $pattern) {
$shared_length = min(strlen($pattern), $card_used_length);

if (substr($pattern, 0, $shared_length) == substr($card_used, 0, $shared_length)) {
PaytabsHelper::log("Discount matched: Card ($card_used), {$pattern_str} => {$pattern}", 1);

$indexes[] = $i;
}
}
}

if (count($indexes) > 0) {
return $indexes;
}

return false;
}

/**
* Validate the discount amount value.
* @return bool
*/
static function isValidDiscount($discount_type, $discount_amount, $amount_original, $amount_discounted)
{
$amount_original = (float)$amount_original;
$amount_discounted = (float)$amount_discounted;
// if ($amount_original == $amount_discounted) return false;

$computed_discount = 0;
$computed_amount = 0;

if ($discount_type == PaytabsEnum::DISCOUNT_FIXED) {
$computed_discount = $discount_amount;
} elseif ($discount_type == PaytabsEnum::DISCOUNT_PERCENTAGE) {
$computed_discount = $amount_original * $discount_amount / 100;
}

$computed_amount = $amount_original - $computed_discount;

//

$tolerancePercentage = 0.1;

// Calculate the percentage difference
$percentageDifference = abs((($amount_discounted - $computed_amount) / $amount_discounted) * 100);

if ($percentageDifference <= $tolerancePercentage) {
// PaytabsHelper::log("Discount values: {$amount_original}, {$computed_amount}", 1);
return true;
} else {
PaytabsHelper::log("Exceeded the tolerance, Discount values: {$amount_original}, {$computed_amount}", 2);
}

return false;
}
}


Expand Down Expand Up @@ -323,6 +446,7 @@ abstract class PaytabsEnum

//

const DISCOUNT_FLAG = 'flag.card_discount';
const DISCOUNT_PERCENTAGE = "percentage";
const DISCOUNT_FIXED = "fixed";

Expand Down Expand Up @@ -965,7 +1089,7 @@ public function set12AltCurrency($alt_currency)
return $this;
}

public function set13CardDiscounts($discount_patterns, $discount_amounts, $discount_types)
public function set13CardDiscounts($discount_patterns, $discount_amounts, $discount_types, $flag_set = false)
{
if (empty($discount_patterns)) {
PaytabsHelper::log('Paytabs admin: Discount arrays must be not empty', 3);
Expand Down Expand Up @@ -1012,6 +1136,10 @@ public function set13CardDiscounts($discount_patterns, $discount_amounts, $disco
$this->card_discounts = [
'card_discounts' => $cards
];

if ($flag_set) {
$this->set50UserDefined(null, null, PaytabsEnum::DISCOUNT_FLAG);
}
}

return $this;
Expand Down Expand Up @@ -1302,6 +1430,7 @@ class PaytabsApi
'24' => ['name' => 'tabby', 'title' => 'PayTabs - Tabby', 'currencies' => ['AED', 'SAR'], 'groups' => []],
'25' => ['name' => 'souhoola', 'title' => 'PayTabs - Souhoola', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME, PaytabsApi::GROUP_REFUND]],
'26' => ['name' => 'amaninstallments', 'title' => 'PayTabs - Aman installments', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME, PaytabsApi::GROUP_REFUND]],
'27' => ['name' => 'tamara', 'title' => 'PayTabs - Tamara', 'currencies' => ['AED', 'SAR'], 'groups' => [PaytabsApi::GROUP_IFRAME, PaytabsApi::GROUP_REFUND]],

];

Expand Down
9 changes: 9 additions & 0 deletions includes/paytabs_gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,12 @@ class WC_Gateway_Paytabs_AmanInstallments extends WC_Gateway_Paytabs

protected $_icon = "amaninstallments.svg";
}

class WC_Gateway_Paytabs_Tamara extends WC_Gateway_Paytabs
{
protected $_code = 'tamara';
protected $_title = 'PayTabs - Tamara ';
protected $_description = 'PayTabs - Tamara payment method';

protected $_icon = "tamara.svg";
}
Loading

0 comments on commit dc0a559

Please sign in to comment.