-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
8 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters