Skip to content

Commit

Permalink
fix BuckarooFee
Browse files Browse the repository at this point in the history
  • Loading branch information
vegimcarkaxhija committed Nov 12, 2024
1 parent e372f03 commit 13076c0
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 272 deletions.
3 changes: 0 additions & 3 deletions Model/ConfigProvider/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
* @method mixed getDebugTypes()
* @method mixed getDebugEmail()
* @method mixed getLimitByIp()
* @method mixed getFeePercentageMode()
* @method mixed getOrderStatusPending()
* @method mixed getOrderStatusNew()
* @method mixed getPaymentFeeLabel()
Expand Down Expand Up @@ -73,7 +72,6 @@ class Account extends AbstractConfigProvider
const XPATH_ACCOUNT_DEBUG_TYPES = 'buckaroo_magento2/account/debug_types';
const XPATH_ACCOUNT_DEBUG_EMAIL = 'buckaroo_magento2/account/debug_email';
const XPATH_ACCOUNT_LIMIT_BY_IP = 'buckaroo_magento2/account/limit_by_ip';
const XPATH_ACCOUNT_FEE_PERCENTAGE_MODE = 'buckaroo_magento2/account/fee_percentage_mode';
const XPATH_ACCOUNT_PAYMENT_FEE_LABEL = 'buckaroo_magento2/account/payment_fee_label';
const XPATH_ACCOUNT_ORDER_STATUS_NEW = 'buckaroo_magento2/account/order_status_new';
const XPATH_ACCOUNT_ORDER_STATUS_PENDING = 'buckaroo_magento2/account/order_status_pending';
Expand Down Expand Up @@ -135,7 +133,6 @@ public function getConfig($store = null)
'debug_types' => $this->getDebugTypes($store),
'debug_email' => $this->getDebugEmail($store),
'limit_by_ip' => $this->getLimitByIp($store),
'fee_percentage_mode' => $this->getFeePercentageMode($store),
'payment_fee_label' => $this->getPaymentFeeLabel($store),
'order_status_new' => $this->getOrderStatusNew($store),
'order_status_pending' => $this->getOrderStatusPending($store),
Expand Down
181 changes: 52 additions & 129 deletions Model/Total/Quote/BuckarooFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@
*/
namespace Buckaroo\Magento2\Model\Total\Quote;

use Buckaroo\Magento2\Exception;
use Magento\Catalog\Helper\Data;
use Buckaroo\Magento2\Logging\Log;
use Buckaroo\Magento2\Helper\PaymentGroupTransaction;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\Tax\Model\Calculation as TaxModelCalculation;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Factory;
use Buckaroo\Magento2\Model\Config\Source\TaxClass\Calculation;
use Buckaroo\Magento2\Model\ConfigProvider\Account as ConfigProviderAccount;
use Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee as ConfigProviderBuckarooFee;

class BuckarooFee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
class BuckarooFee extends AbstractTotal
{
/** @var ConfigProviderAccount */
/**
* @var ConfigProviderAccount
*/
protected $configProviderAccount;

/** @var ConfigProviderBuckarooFee */
/**
* @var ConfigProviderBuckarooFee
*/
protected $configProviderBuckarooFee;

/**
Expand All @@ -53,7 +59,7 @@ class BuckarooFee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
public $catalogHelper;

/**
* @var \Buckaroo\Magento2\Helper\PaymentGroupTransaction
* @var PaymentGroupTransaction
*/
public $groupTransaction;

Expand Down Expand Up @@ -103,12 +109,13 @@ public function __construct(
/**
* Collect grand total address amount
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
*
* @throws \LogicException
* @throws \Buckaroo\Magento2\Exception
* @throws Exception
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
Expand Down Expand Up @@ -136,7 +143,6 @@ public function collect(
return $this;
}

// Ensure payment method is set correctly
$paymentMethod = $quote->getPayment()->getMethod();
if (!$paymentMethod || strpos($paymentMethod, 'buckaroo_magento2_') !== 0) {
return $this;
Expand All @@ -148,7 +154,7 @@ public function collect(
}

// Calculate the base payment fee using the getBaseFee method
$basePaymentFee = $this->getBaseFee($methodInstance, $quote);
$basePaymentFee = $this->getBaseFee($methodInstance, $quote, $total);
if ($basePaymentFee < 0.01) {
return $this;
}
Expand All @@ -163,7 +169,6 @@ public function collect(
* @noinspection PhpUndefinedMethodInspection
*/
$quote->setBaseBuckarooFee($basePaymentFee);

/**
* @noinspection PhpUndefinedMethodInspection
*/
Expand All @@ -190,14 +195,14 @@ public function collect(
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
* @return array
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
/**
* @noinspection PhpUndefinedMethodInspection
*/
$totals = [
return [
'code' => $this->getCode(),
'title' => $this->getLabel(),
'buckaroo_fee' => $total->getBuckarooFee(),
Expand All @@ -207,150 +212,68 @@ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Qu
'buckaroo_fee_tax_amount' => $total->getBuckarooFeeTaxAmount(),
'buckaroo_fee_base_tax_amount' => $total->getBuckarooFeeBaseTaxAmount(),
];

return $totals;
}

/**
* @param \Buckaroo\Magento2\Model\Method\AbstractMethod $methodInstance
* @param \Magento\Quote\Model\Quote $quote
* @param bool $inclTax
* @param \Magento\Quote\Model\Quote\Address\Total $total
*
* @return bool|false|float
* @throws \Buckaroo\Magento2\Exception
*/
public function getBaseFee(
\Buckaroo\Magento2\Model\Method\AbstractMethod $methodInstance,
\Magento\Quote\Model\Quote $quote,
$inclTax = false
\Magento\Quote\Model\Quote\Address\Total $total
) {
$buckarooPaymentMethodCode = $methodInstance->buckarooPaymentMethodCode;
if (!$this->configProviderMethodFactory->has($buckarooPaymentMethodCode)) {
return false;
}

$configProvider = $this->configProviderMethodFactory->get($buckarooPaymentMethodCode);
$basePaymentFee = trim($configProvider->getPaymentFee($quote->getStore()));
$inclTax = $this->configProviderBuckarooFee->getPaymentFeeTax() ==
Calculation::DISPLAY_TYPE_INCLUDING_TAX;
$basePaymentFeeRaw = trim($configProvider->getPaymentFee($quote->getStore()));

$shippingAddress = $quote->getShippingAddress();
$billingAddress = $quote->getBillingAddress();
$customerTaxClassId = $quote->getCustomerTaxClassId();
$storeId = $quote->getStoreId();
$taxClassId = $this->configProviderBuckarooFee->getTaxClass();

$request = $this->taxCalculation->getRateRequest(
$shippingAddress,
$billingAddress,
$customerTaxClassId,
$storeId
);
$request->setProductClassId($taxClassId);
$percent = $this->taxCalculation->getRate($request);

if (is_numeric($basePaymentFee)) {
if (in_array($buckarooPaymentMethodCode, ['billink','afterpay20','afterpay','paypal'])) {
if ($inclTax) {
if ($percent > 0) {
return $basePaymentFee / (1 + ($percent / 100));
}
}
return $basePaymentFee;
} else {
if ($inclTax){
return $basePaymentFee / (1 + ($percent / 100));
}
/**
* Payment fee is a number
*/
return $this->getFeePrice($basePaymentFee);
}
$inclTax= $this->configProviderBuckarooFee->getPaymentFeeTax() ==
Calculation::DISPLAY_TYPE_INCLUDING_TAX;

} elseif (strpos($basePaymentFee, '%') === false) {
/**
* Payment fee is invalid
*/
return false;
}
// Determine if the fee is a percentage or fixed amount
$isPercentage = strpos($basePaymentFeeRaw, '%') !== false;

/**
* Payment fee is a percentage
*/
$percentage = floatval($basePaymentFee);
if ($quote->getShippingAddress()) {
$address = $quote->getShippingAddress();
if ($isPercentage) {
$subtotal = $total->getData('base_subtotal_incl_tax');
if (!$subtotal) {
$subtotal = $total->getTotalAmount('subtotal');
}
$percentage = (float) rtrim($basePaymentFeeRaw, '%');
$basePaymentFee = ($percentage / 100) * $subtotal;
} else {
$address = $quote->getBillingAddress();
}

$total = 0;

$feePercentageMode = $this->configProviderAccount->getFeePercentageMode($quote->getStore());

switch ($feePercentageMode) {
case 'subtotal':
$total = $address->getBaseSubtotal();
break;
case 'subtotal_incl_tax':
$total = $address->getBaseSubtotalTotalInclTax();
break;
$basePaymentFee = (float) $basePaymentFeeRaw;
}
$percentageFee = ($percentage / 100) * $total;

if($inclTax){
if($percent > 0){
return $basePaymentFee / (1 + ($percent / 100));
if (!$inclTax) {
$shippingAddress = $quote->getShippingAddress() ?: $quote->getBillingAddress();
$billingAddress = $quote->getBillingAddress();
$customerTaxClassId = $quote->getCustomerTaxClassId();
$storeId = $quote->getStoreId();
$taxClassId = $this->configProviderBuckarooFee->getTaxClass();
if (!$taxClassId) {
$taxClassId = 0;
}
} else{
return $basePaymentFee;
}

return $basePaymentFee;
}

/**
* Get payment fee price with correct tax
*
* @param float $price
* @param null $priceIncl
*
* @param \Magento\Framework\DataObject|null $pseudoProduct
*
* @return float
* @throws \Buckaroo\Magento2\Exception
*/
public function getFeePrice($price, $priceIncl = null, \Magento\Framework\DataObject $pseudoProduct = null)
{
if ($pseudoProduct === null) {
$pseudoProduct = new \Magento\Framework\DataObject();
}

$pseudoProduct->setTaxClassId($this->configProviderBuckarooFee->getTaxClass());

/**
* @noinspection PhpUndefinedMethodInspection
*/
if ($priceIncl === null
&& $this->configProviderBuckarooFee->getPaymentFeeTax() == Calculation::DISPLAY_TYPE_INCLUDING_TAX
) {
$priceIncl = true;
} else {
$priceIncl = false;
$taxRate = $this->taxCalculation->getRate(
$this->taxCalculation->getRateRequest(
$shippingAddress,
$billingAddress,
$customerTaxClassId,
$storeId
)->setProductClassId($taxClassId)
);
$basePaymentFee = $basePaymentFee * (1 + $taxRate / 100);
}

$price = $this->catalogHelper->getTaxPrice(
$pseudoProduct,
$price,
false,
null,
null,
null,
null,
$priceIncl
);

return $price;
return $basePaymentFee;
}

/**
Expand All @@ -360,6 +283,6 @@ public function getFeePrice($price, $priceIncl = null, \Magento\Framework\DataOb
*/
public function getLabel()
{
return __('Fee');
return __('Payment Fee');
}
}
Loading

0 comments on commit 13076c0

Please sign in to comment.