Skip to content

Commit

Permalink
Merge pull request #163 from buckaroo-it/BP-3482-Fix-Payment-fee-not-…
Browse files Browse the repository at this point in the history
…visible-in-some-places-order-confirmation-page-Backoffice-area

Bp 3482 fix payment fee not visible in some places order confirmation page backoffice area
  • Loading branch information
vegimcarkaxhija authored Jun 11, 2024
2 parents a635e5d + 5f31b8d commit a461497
Show file tree
Hide file tree
Showing 29 changed files with 2,100 additions and 793 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ composer.lock
var
tools
dev/node_modules
dev/package-lock.json
.idea
389 changes: 233 additions & 156 deletions buckaroo3.php

Large diffs are not rendered by default.

97 changes: 66 additions & 31 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/

use Buckaroo\PrestaShop\Src\Config\Config;
use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException;

Expand Down Expand Up @@ -42,9 +43,7 @@ public function postProcess()
*/
private function renderCartSummary(Cart $cart, array $presentedCart = null)
{
if (!$presentedCart) {
$presentedCart = $this->cart_presenter->present($cart);
}
$presentedCart = $presentedCart ?: $this->cart_presenter->present($cart);

$this->context->smarty->assign([
'configuration' => $this->getTemplateVarConfiguration(),
Expand All @@ -54,36 +53,33 @@ private function renderCartSummary(Cart $cart, array $presentedCart = null)

$responseArray = [
'cart_summary_totals' => $this->render('checkout/_partials/cart-summary-totals'),
'paymentFee' => $presentedCart['totals']['paymentFee'] ?? null,
'paymentFeeTax' => $presentedCart['totals']['paymentFeeTax'] ?? null,
'includedTaxes' => $presentedCart['totals']['includedTaxes'] ?? null,
];

$paymentFee = $presentedCart['totals']['paymentFee'] ?? null;
if (isset($paymentFee)) {
$responseArray['paymentFee'] = $paymentFee;
}

$this->ajaxRender(json_encode($responseArray));
}

/**
* @throws PrestaShopException
* @throws LocalizationException
* @throws Exception
*/
private function calculateTotalWithPaymentFee()
{
$cart = $this->context->cart;
$paymentFeeValue = Tools::getValue('paymentFee');
$paymentFeeValue = trim($paymentFeeValue);
$paymentFeeValue = trim(Tools::getValue('paymentFee'));

if (!$paymentFeeValue) {
$this->renderCartSummary($cart);

return;
}

$paymentFee = $this->calculatePaymentFee($paymentFeeValue, $cart);
$orderTotals = $this->calculateOrderTotals($cart, $paymentFee);

$this->updatePresentedCart($cart, $orderTotals, $paymentFee);
$this->updatePresentedCart($cart, $orderTotals);
}

private function calculatePaymentFee($paymentFeeValue, $cart): DecimalNumber
Expand All @@ -92,45 +88,74 @@ private function calculatePaymentFee($paymentFeeValue, $cart): DecimalNumber

if (strpos($paymentFeeValue, '%') !== false) {
$paymentFeeValue = str_replace('%', '', $paymentFeeValue);
$paymentFeeValue = new DecimalNumber((string) $paymentFeeValue);
$percentage = $paymentFeeValue->dividedBy(new DecimalNumber('100'));

$percentage = (new DecimalNumber((string) $paymentFeeValue))->dividedBy(new DecimalNumber('100'));
return $orderTotal->times($percentage);
} elseif ($paymentFeeValue > 0) {
return new DecimalNumber((string) $paymentFeeValue);
}

return new DecimalNumber('0');
return new DecimalNumber($paymentFeeValue > 0 ? (string) $paymentFeeValue : '0');
}

private function calculateOrderTotals($cart, $paymentFee): array
{
$orderTotalWithFee = (new DecimalNumber((string) $cart->getOrderTotal()))->plus($paymentFee);
$orderTotalNoTaxWithFee = (new DecimalNumber((string) $cart->getOrderTotal(false)))->plus($paymentFee);
$paymentFeeValue = (float) $paymentFee->toPrecision(2);
$address = new Address($cart->id_address_invoice);
$taxManager = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_TAX'));
$taxCalculator = $taxManager->getTaxCalculator();
$taxRate = $taxCalculator->getTotalRate();
$taxRateDecimal = $taxRate / 100;

if (Configuration::get(Config::PAYMENT_FEE_MODE) === 'subtotal_incl_tax') {
$baseFee = $paymentFeeValue / (1 + $taxRateDecimal);
$taxFee = $paymentFeeValue - $baseFee;
$orderTotalWithFee = (new DecimalNumber((string) $cart->getOrderTotal(true, Cart::BOTH)))->plus(new DecimalNumber((string) $paymentFee));
$orderTotalNoTaxWithFee = (new DecimalNumber((string) $cart->getOrderTotal(false, Cart::BOTH)))->plus(new DecimalNumber((string) $paymentFee));
$paymentFeeTax = new DecimalNumber((string) $taxFee);
$paymentFee = new DecimalNumber((string) $baseFee);
} else {
$paymentFeeTaxAmount = $paymentFeeValue * $taxRateDecimal;
$totalFeePriceTaxIncl = $paymentFeeValue + $paymentFeeTaxAmount;
$paymentFeeTax = new DecimalNumber((string) $paymentFeeTaxAmount);
$orderTotalWithFee = (new DecimalNumber((string) $cart->getOrderTotal(true, Cart::BOTH)))->plus(new DecimalNumber((string) $totalFeePriceTaxIncl));
$orderTotalNoTaxWithFee = (new DecimalNumber((string) $cart->getOrderTotal(false, Cart::BOTH)))->plus($paymentFee);
}

return [
'total_including_tax' => $orderTotalWithFee->toPrecision(2),
'total_excluding_tax' => $orderTotalNoTaxWithFee->toPrecision(2),
];
return [
'total_including_tax' => $orderTotalWithFee->toPrecision(2),
'total_excluding_tax' => $orderTotalNoTaxWithFee->toPrecision(2),
'payment_fee' => $paymentFee->toPrecision(2),
'payment_fee_tax' => $paymentFeeTax->toPrecision(2),
];
}

/**
* @throws PrestaShopException
* @throws LocalizationException
* @throws Exception
*/
private function updatePresentedCart($cart, $orderTotals, $paymentFee)
private function updatePresentedCart($cart, $orderTotals)
{
$taxConfiguration = new TaxConfiguration();
$presentedCart = $this->cart_presenter->present($cart);

$buckarooFee = $this->formatPrice($paymentFee->toPrecision(2));
$presentedCart['totals'] = [
$buckarooFee = $this->formatPrice($orderTotals['payment_fee']);
$paymentFeeTax = $this->formatPrice($orderTotals['payment_fee_tax']);
$totalWithoutTax = new DecimalNumber((string) $cart->getOrderTotal(false, Cart::BOTH));
$totalWithTax = new DecimalNumber((string) $cart->getOrderTotal(true, Cart::BOTH));
$includedTaxes = $totalWithTax->minus($totalWithoutTax)->plus(new DecimalNumber($orderTotals['payment_fee_tax']))->toPrecision(2);

$presentedCart['totals'] = $this->getTotalsArray($orderTotals, $buckarooFee, $paymentFeeTax, $includedTaxes);

$this->renderCartSummary($cart, $presentedCart);
}

private function getTotalsArray($orderTotals, $buckarooFee, $paymentFeeTax, $includedTaxes): array
{
$totalsArray = [
'total' => [
'type' => 'total',
'label' => $this->translator->trans('Total', [], 'Shop.Theme.Checkout'),
'amount' => $taxConfiguration->includeTaxes() ? $orderTotals['total_including_tax'] : $orderTotals['total_excluding_tax'],
'value' => $this->formatPrice($taxConfiguration->includeTaxes() ? $orderTotals['total_including_tax'] : $orderTotals['total_excluding_tax']),
'amount' => $orderTotals['total_including_tax'],
'value' => $this->formatPrice($orderTotals['total_including_tax']),
],
'total_including_tax' => [
'type' => 'total',
Expand All @@ -145,9 +170,19 @@ private function updatePresentedCart($cart, $orderTotals, $paymentFee)
'value' => $this->formatPrice($orderTotals['total_excluding_tax']),
],
'paymentFee' => $buckarooFee,
'paymentFeeTax' => $paymentFeeTax,
];

$this->renderCartSummary($cart, $presentedCart);
if (Configuration::get(Config::PAYMENT_FEE_MODE) === 'subtotal') {
$totalsArray['includedTaxes'] = [
'type' => 'tax',
'label' => $this->translator->trans('Included taxes', [], 'Shop.Theme.Checkout'),
'amount' => $includedTaxes,
'value' => $this->formatPrice($includedTaxes),
];
}

return $totalsArray;
}

/**
Expand All @@ -157,4 +192,4 @@ private function formatPrice($amount): string
{
return $this->context->getCurrentLocale()->formatPrice($amount, $this->context->currency->iso_code);
}
}
}
Loading

0 comments on commit a461497

Please sign in to comment.