Skip to content

Commit

Permalink
Valu widget (#65)
Browse files Browse the repository at this point in the history
* Add valU widget
valU widget display inside product details page.
Set the widget location inside the product summary

* Use higher resolution icon for valU

* SDK core update
  • Loading branch information
wajihkm authored Dec 6, 2023
1 parent 6fea84d commit f087396
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 13 deletions.
Binary file modified icons/valu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/valu_long.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions includes/_valu_widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$_url_logo = $valu_payment->getIconWidget();

?>

<div style="border: 1px solid orange; border-radius: 12px; margin: 10px 0px; padding: 3px;">
<table style="margin: 0;">
<tr>
<td style="width: 1%; white-space: nowrap;">
<img src="<?= esc_url($_url_logo) ?>" alt="valU logo" style="min-height: 32px; max-width: 128pt;">
</td>
<td style="vertical-align: middle;">
<?= $plan ?>
</td>
</tr>
</table>
</div>
23 changes: 21 additions & 2 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.15.0
* Version: 2.17.0
* PHP >= 7.0.0
*/

define('PAYTABS_SDK_VERSION', '2.15.0');
define('PAYTABS_SDK_VERSION', '2.17.0');

define('PAYTABS_DEBUG_FILE_NAME', 'debug_paytabs.log');
define('PAYTABS_DEBUG_SEVERITY', ['Info', 'Warning', 'Error']);
Expand Down Expand Up @@ -1049,6 +1049,7 @@ class PaytabsApi
'23' => ['name' => 'forsa', 'title' => 'PayTabs - Forsa', 'currencies' => ['EGP'], 'groups' => [PaytabsApi::GROUP_IFRAME]],
'24' => ['name' => 'tabby', 'title' => 'PayTabs - Tabby', 'currencies' => ['AED'], '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]],

];

Expand Down Expand Up @@ -1097,6 +1098,8 @@ class PaytabsApi
const URL_TOKEN_QUERY = 'payment/token';
const URL_TOKEN_DELETE = 'payment/token/delete';

const URL_INQUIRY_VALU = 'payment/info/valu/inquiry';

//

private $base_url;
Expand Down Expand Up @@ -1201,6 +1204,22 @@ function token_delete($token)
return $res;
}

function inqiry_valu($params)
{
$res1 = $this->sendRequest(self::URL_INQUIRY_VALU, $params);

$res = json_decode($res1);

$res->success = false;

if (isset($res->valuResponse, $res->valuResponse->responseCode)) {
if ($res->valuResponse->responseCode == 0) {
$res->success = true;
}
}

return $res;
}
//

function is_valid_redirect($post_values)
Expand Down
48 changes: 41 additions & 7 deletions includes/paytabs_gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,47 @@ public function init_form_fields()
{
parent::init_form_fields();

$this->form_fields['valu_product_id'] = array(
'title' => __('valU product ID', 'PayTabs'),
'type' => 'text',
'description' => __('Please enter the product ID of your valU account.', 'PayTabs'),
'default' => '',
'required' => true
);
$this->form_fields['valu_widget_enable'] = [
'title' => __('ValU widget', 'PayTabs'),
'label' => __('Enable ValU widget.', 'PayTabs'),
'description' => __('Show valU widget in product\'s details page.', 'PayTabs'),
'type' => 'checkbox',
'default' => 'no'
];
$this->form_fields['valu_widget_static_content'] = [
'title' => __('ValU widget, Static content', 'PayTabs'),
'type' => 'checkbox',
'label' => __('ValU widget, Static content.', 'PayTabs'),
'description' => __('Display the static content in the widget, Otherwise call the API to fetch live content based on the price.', 'PayTabs'),
'default' => 'no'
];
$this->form_fields['valu_widget_phone_number'] = [
'title' => __('ValU phone number', 'PayTabs'),
'type' => 'text',
'description' => __('Registered valU phone number.', 'PayTabs'),
'default' => '',
'desc_tip' => true,
];
$this->form_fields['valu_widget_price_threshold'] = [
'title' => __('ValU price threshold', 'PayTabs'),
'type' => 'text',
'description' => __('Display The widget if the product price higher than the current thershold.', 'PayTabs'),
'default' => '1000',
'desc_tip' => true,
];
}

public function getIconWidget()
{
$icon_name = 'valu_long.png';

$iconPath = PAYTABS_PAYPAGE_DIR . "icons/{$icon_name}";
$icon = '';
if (file_exists($iconPath)) {
$icon = PAYTABS_PAYPAGE_ICONS_URL . "{$icon_name}";
}

return $icon;
}
}

Expand Down
7 changes: 5 additions & 2 deletions includes/paytabs_payment_methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ public function __construct()


if ($this->_code == 'valu') {
$this->valu_product_id = $this->get_option('valu_product_id');
//$this->valu_product_id = $this->get_option('valu_product_id');
$this->valu_widget_enable = $this->get_option('valu_widget_enable') == 'yes';
$this->valu_widget_static_content = $this->get_option('valu_widget_static_content') == 'yes';
$this->valu_widget_phone_number = $this->get_option('valu_widget_phone_number');
$this->valu_widget_price_threshold = $this->get_option('valu_widget_price_threshold');
}

$this->enable_tokenise = $this->get_option('enable_tokenise') == 'yes';
Expand Down Expand Up @@ -602,7 +606,6 @@ function receipt_page($order_id)
}
}


public function scheduled_subscription_payment($amount_to_charge, $renewal_order)
{
$user_id = $renewal_order->get_user_id();
Expand Down
113 changes: 113 additions & 0 deletions includes/widgets/valu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

class ValuWidget
{

function init($valu_payment)
{
if ($valu_payment->valu_widget_enable) {

$product_price = $this->get_product_price();
if ($product_price) {
if ($product_price >= $valu_payment->valu_widget_price_threshold) {

if ($valu_payment->valu_widget_static_content) {
$plan = $this->get_static_content();
} else {
$plan = $this->call_valu_api($valu_payment, $product_price);
}

if ($plan) {
include(PAYTABS_PAYPAGE_DIR . 'includes/_valu_widget.php');
}
}
}
}
}

private function get_static_content()
{
return "Buy Now & Pay Later up to 60 Months!";
}

private function get_product_price()
{
// Get the current product's ID.
$product_id = get_the_ID();
$product = wc_get_product($product_id);
$product_price = 0;

if ($product) {
// Get the product price.
$product_price = $product->get_price();
}

return $product_price;
}

private function call_valu_api($valu_payment, $product_price)
{
$_paytabsApi = PaytabsApi::getInstance($valu_payment->paytabs_endpoint, $valu_payment->merchant_id, $valu_payment->merchant_key);
$phone_number = $valu_payment->valu_widget_phone_number;

$data = [
'cart_amount' => $product_price,
'cart_currency' => "EGP",
'customer_details' => [
"phone" => $phone_number
],
];

PaytabsHelper::log("valU inqiry, {$product_price}", 1);
$details = $_paytabsApi->inqiry_valu($data);

if (!$details || !$details->success) {
$_err_msg = json_encode($details);
PaytabsHelper::log("valU Details error: [{$_err_msg}]", 3);
return false;
}

$installments_count = 3;
$valu_plan = $this->getValUPlan($details, $installments_count);

if (!$valu_plan) {
return false;
}

try {
$installment_amount = $valu_plan->emi;

$calculated_installment = round($product_price / $installments_count, 2);
$is_free_interest = $calculated_installment >= $installment_amount;

$txt_free = $is_free_interest ? "interest-free" : "";

$msg = "Pay {$installments_count} {$txt_free} payments of EGP $installment_amount.";

return $msg;
} catch (\Throwable $th) {
PaytabsHelper::log("valU widget error: " . $th->getMessage(), 3);
}

return false;
}

private function getValUPlan($details, $installments_count)
{
try {
$plansList = $details->valuResponse->productList[0]->tenureList;
foreach ($plansList as $plan) {
if ($plan->tenorMonth == $installments_count) {
return $plan;
}
}
} catch (\Throwable $th) {
PaytabsHelper::log("valU Plan error: " . $th->getMessage(), 3);
}

$_log = json_encode($plansList);
PaytabsHelper::log("valU Plan error: No Plan selected, [{$_log}]", 2);

return false;
}
}
19 changes: 17 additions & 2 deletions paytabs-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin URI: https://paytabs.com/
* Description: PayTabs is a <strong>3rd party payment gateway</strong>. Ideal payment solutions for your internet business.
* Version: 4.20.0
* Version: 4.21.0
* Requires PHP: 7.0
* Author: PayTabs
* Author URI: integration@paytabs.com
Expand All @@ -20,7 +20,7 @@
}


define('PAYTABS_PAYPAGE_VERSION', '4.20.0');
define('PAYTABS_PAYPAGE_VERSION', '4.21.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 Expand Up @@ -80,6 +80,7 @@ function woocommerce_paytabs_init()
require_once PAYTABS_PAYPAGE_DIR . 'includes/paytabs_payment_methods.php';
require_once PAYTABS_PAYPAGE_DIR . 'includes/paytabs_gateways.php';
require_once PAYTABS_PAYPAGE_DIR . 'includes/paytabs_payment_token.php';
require_once PAYTABS_PAYPAGE_DIR . 'includes/widgets/valu.php';


/**
Expand Down Expand Up @@ -135,6 +136,20 @@ function paytabs_add_action_links($links)
add_filter('woocommerce_payment_gateways', 'paytabs_filter_gateways', 10, 1);
add_filter('woocommerce_payment_methods_list_item', 'get_account_saved_payment_methods_list_item_paytabs', 10, 2);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'paytabs_add_action_links');

add_action('woocommerce_single_product_summary', 'valu_widget', 21);

function valu_widget()
{
$enabled_gateways = WC()->payment_gateways->get_available_payment_gateways();

if (array_key_exists('paytabs_valu', $enabled_gateways)) {
$valu_payment = $enabled_gateways['paytabs_valu'];

$valu_widget = new ValuWidget();
$valu_widget->init($valu_payment);
}
}
}


Expand Down

0 comments on commit f087396

Please sign in to comment.