Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 164 additions & 111 deletions Helper/PaymentFee.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Model/ConfigProvider/BuckarooFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BuckarooFee extends AbstractConfigProvider
/**
* Buckaroo fee tax class
*/
const XPATH_BUCKAROOFEE_TAX_CLASS = 'tax/classes/buckaroo_fee_tax_class';
const XPATH_ACCOUNT_BUCKAROO_FEE_TAX_CLASS = 'buckaroo_magento2/account/buckaroo_fee_tax_class';

/**
* Retrieve the tax class for Buckaroo fee
Expand All @@ -41,7 +41,7 @@ class BuckarooFee extends AbstractConfigProvider
*/
public function getBuckarooFeeTaxClass($store = null)
{
return $this->scopeConfig->getValue(self::XPATH_BUCKAROOFEE_TAX_CLASS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
return $this->scopeConfig->getValue(self::XPATH_ACCOUNT_BUCKAROO_FEE_TAX_CLASS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Model/Method/AbstractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ protected function getTaxCategory($order)
$billingAddress = $order->getBillingAddress();
$customerTaxClassId = $order->getCustomerTaxClassId();
$storeId = $order->getStoreId();
$taxClassId = $this->configProviderBuckarooFee->getTaxClass();
$taxClassId = $this->configProviderBuckarooFee->getBuckarooFeeTaxClass($order->getStore());

$request = $this->taxCalculation->getRateRequest(
$shippingAddress,
Expand Down Expand Up @@ -2773,8 +2773,8 @@ public function checkTotalGrossAmount($requestData, $payment)
}

return $requestData;

}

public function canUseForCountry($country)
{
if ($this->getConfigData('allowspecific') != 1) {
Expand Down
10 changes: 3 additions & 7 deletions Model/Method/Afterpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,18 +771,15 @@ public function getArticleArrayLine(
protected function getTaxCategory($order)
{
$storeId = (int) $order->getStoreId();
$taxClassId = $this->configProviderBuckarooFee->getTaxClass($storeId);
$taxClassId = $this->configProviderBuckarooFee->getBuckarooFeeTaxClass($order->getStore());

$taxCategory = 4;

if (!$taxClassId) {
return $taxCategory;
}
/**
* @var \Buckaroo\Magento2\Model\ConfigProvider\Method\Afterpay $afterPayConfig
*/
$afterPayConfig = $this->configProviderMethodFactory
->get($this->_code);

$afterPayConfig = $this->configProviderMethodFactory->get($this->_code);

$highClasses = explode(',', (string)$afterPayConfig->getHighTaxClasses($storeId));
$middleClasses = explode(',', (string)$afterPayConfig->getMiddleTaxClasses($storeId));
Expand All @@ -798,7 +795,6 @@ protected function getTaxCategory($order)
} elseif (in_array($taxClassId, $zeroClasses)) {
$taxCategory = 3;
} else {
// No classes == 4
$taxCategory = 4;
}

Expand Down
58 changes: 27 additions & 31 deletions Model/Total/Quote/Tax/BuckarooFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

namespace Buckaroo\Magento2\Model\Total\Quote\Tax;

use Buckaroo\Magento2\Model\ConfigProvider\Account as ConfigProviderAccount;
use Buckaroo\Magento2\Helper\PaymentGroupTransaction;
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Api\Data\AddressInterfaceFactory as CustomerAddressFactory;
Expand Down Expand Up @@ -55,31 +54,39 @@ class BuckarooFee extends CommonTaxCollector
* @var PaymentGroupTransaction
*/
protected $groupTransaction;

/**
* @var Calculate
*/
protected $calculate;

/**
* @var ConfigProviderAccount
* @var \Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee
*/
protected $configProviderAccount;
protected $configProviderBuckarooFee;

/**
* @var TaxHelper|null
*/
protected $taxHelper;

/**
* Constructor
*
* @param TaxConfig $taxConfig
* @param TaxCalculationInterface $taxCalculationService
* @param QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory
* @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory
* @param TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory
* @param CustomerAddressFactory $customerAddressFactory
* @param CustomerAddressRegionFactory $customerAddressRegionFactory
* @param TaxHelper|null $taxHelper
* @param QuoteDetailsItemExtensionInterfaceFactory|null $quoteDetailsItemExtensionInterfaceFactory
* @param CustomerAccountManagement|null $customerAccountManagement
* @param PriceCurrencyInterface $priceCurrency
* @param PaymentGroupTransaction $groupTransaction
* @param Calculate $calculate
* @param ConfigProviderAccount $configProviderAccount
* @param \Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee $configProviderBuckarooFee
* @param TaxHelper|null $taxHelper
* @param QuoteDetailsItemExtensionInterfaceFactory|null $quoteDetailsItemExtensionInterfaceFactory
* @param CustomerAccountManagement|null $customerAccountManagement
*/
public function __construct(
TaxConfig $taxConfig,
Expand All @@ -92,7 +99,7 @@ public function __construct(
PriceCurrencyInterface $priceCurrency,
PaymentGroupTransaction $groupTransaction,
Calculate $calculate,
ConfigProviderAccount $configProviderAccount,
\Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee $configProviderBuckarooFee,
TaxHelper $taxHelper = null,
QuoteDetailsItemExtensionInterfaceFactory $quoteDetailsItemExtensionInterfaceFactory = null,
?CustomerAccountManagement $customerAccountManagement = null
Expand Down Expand Up @@ -133,12 +140,13 @@ public function __construct(
$this->priceCurrency = $priceCurrency;
$this->groupTransaction = $groupTransaction;
$this->calculate = $calculate;
$this->configProviderAccount = $configProviderAccount;
$this->configProviderBuckarooFee = $configProviderBuckarooFee;
$this->taxHelper = $taxHelper;
$this->setCode('pretax_buckaroo_fee');
}

/**
* Collect buckaroo fee related items and add them to tax calculation
* Collect Buckaroo fee related items and add them to tax calculation
*
* @param Quote $quote
* @param ShippingAssignmentInterface $shippingAssignment
Expand All @@ -151,9 +159,9 @@ public function collect(
Total $total
) {
if (!$shippingAssignment->getItems()) {
parent::collect($quote, $shippingAssignment, $total);
return $this;
}

$orderId = $quote->getReservedOrderId();

// Check if already paid amount is affecting the calculation
Expand All @@ -162,40 +170,25 @@ public function collect(
}

$result = $this->calculate->calculatePaymentFee($quote, $total);

if ($result === null){
return $this;
}
$amount = $this->priceCurrency->convert($result->getRoundedAmount());

$this->addAssociatedTaxable($shippingAssignment, $result, $quote);

$feeDataObject = $this->quoteDetailsItemDataObjectFactory->create()
->setType('buckaroo_fee')
->setCode('buckaroo_fee')
->setQuantity(1);

$feeDataObject->setUnitPrice($result->getRoundedAmount());
$feeDataObject->setTaxClassKey(
$this->taxClassKeyDataObjectFactory->create()
->setType(TaxClassKeyInterface::TYPE_ID)
->setValue(4)
);
$feeDataObject->setIsTaxIncluded(true);

$quoteDetails = $this->prepareQuoteDetails($shippingAssignment, [$feeDataObject]);

$this->taxCalculationService->calculateTax($quoteDetails, $quote->getStoreId());

parent::collect($quote, $shippingAssignment, $total);


return $this;
}

/**
* Add associated taxable for Buckaroo fee
*
* @param ShippingAssignmentInterface $shippingAssignment
* @param Result $result
* @param Quote $quote
* @return void
*/
private function addAssociatedTaxable(ShippingAssignmentInterface $shippingAssignment, Result $result, Quote $quote)
{
Expand All @@ -210,8 +203,11 @@ private function addAssociatedTaxable(ShippingAssignmentInterface $shippingAssig
$associatedTaxables = [];
}

$taxClassId = $this->configProviderAccount->getBuckarooFeeTaxClass();
// Retrieve the store from the quote
$store = $quote->getStore();

// Retrieve the Buckaroo fee tax class ID from BuckarooFeeConfigProvider using the store
$taxClassId = $this->configProviderBuckarooFee->getBuckarooFeeTaxClass($store);
$associatedTaxables[] = [
CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_TYPE => self::QUOTE_TYPE,
CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_CODE => self::CODE_QUOTE_GW,
Expand Down
2 changes: 2 additions & 0 deletions Model/Total/Quote/Tax/BuckarooFeeAfterTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public function collect(
if (empty($extraTaxableDetails[BuckarooFee::QUOTE_TYPE])) {
return $this;
}

$itemTaxDetails = $extraTaxableDetails[BuckarooFee::QUOTE_TYPE];

if (empty($itemTaxDetails[CommonTaxCollector::ASSOCIATION_ITEM_CODE_FOR_QUOTE][0])) {
return $this;
}

$buckarooFeeTaxDetails = $itemTaxDetails[CommonTaxCollector::ASSOCIATION_ITEM_CODE_FOR_QUOTE][0];

$buckarooFeeBaseTaxAmount = $buckarooFeeTaxDetails['base_row_tax'];
Expand Down
11 changes: 2 additions & 9 deletions Service/Sales/Pdf/BuckarooFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getTotalsForDisplay()
$label = $this->paymentFee->getBuckarooPaymentFeeLabel($this->getOrder());
$fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

$isFeeInclusiveOfTax = (bool)$this->configProviderAccount->getBuckarooFeeTaxClass();
$isFeeInclusiveOfTax = $this->configProviderBuckarooFee->getBuckarooFeeTaxClass($store);

$amountInclTax = $this->getSource()->getBuckarooFeeInclTax();

Expand All @@ -88,7 +88,7 @@ public function getTotalsForDisplay()

$amountInclTax = $this->getOrder()->formatPriceTxt($amountInclTax);

if ($isFeeInclusiveOfTax && $this->buckarooPaymentCalculationInclTax($store)) {
if ($isFeeInclusiveOfTax == DisplayType::DISPLAY_TYPE_INCLUDING_TAX) {
$totals = [
[
'amount' => $this->getAmountPrefix() . $amountInclTax,
Expand All @@ -108,11 +108,4 @@ public function getTotalsForDisplay()

return $totals;
}

public function buckarooPaymentCalculationInclTax($store = null)
{
$configValue = $this->configProviderBuckarooFee->getPaymentFeeTax($store);

return $configValue == DisplayType::DISPLAY_TYPE_INCLUDING_TAX;
}
}
31 changes: 21 additions & 10 deletions Service/Tax/TaxCalculate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Buckaroo\Magento2\Service\Tax;

use Buckaroo\Magento2\Model\ConfigProvider\Account as ConfigProviderAccount;
use Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee as BuckarooFeeConfigProvider;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Tax\Model\Calculation;

Expand All @@ -32,30 +32,41 @@ class TaxCalculate
private $taxCalculation;

/**
* @var ConfigProviderAccount
* @var BuckarooFeeConfigProvider
*/
protected $configProviderAccount;
protected $configProviderBuckarooFee;

/**
* Constructor
*
* @param Calculation $taxCalculation
* @param ConfigProviderAccount $configProviderAccount
* @param BuckarooFeeConfigProvider $configProviderBuckarooFee
*/
public function __construct(Calculation $taxCalculation, ConfigProviderAccount $configProviderAccount)
{
public function __construct(
Calculation $taxCalculation,
BuckarooFeeConfigProvider $configProviderBuckarooFee
) {
$this->taxCalculation = $taxCalculation;
$this->configProviderAccount = $configProviderAccount;
$this->configProviderBuckarooFee = $configProviderBuckarooFee;
}

public function getTaxFromAmountIncludingTax(CartInterface $cart, $amount)
/**
* Calculate tax amount from an amount that includes tax.
*
* @param CartInterface $cart
* @param float $amount Amount including tax
* @return float Tax amount
*/
public function getTaxFromAmountIncludingTax(CartInterface $cart, float $amount): float
{
$shippingAddress = $cart->getShippingAddress();
$billingAddress = $cart->getBillingAddress();
$customerTaxClassId = $cart->getCustomerTaxClassId();
$storeId = $cart->getStoreId();
$taxClassId = $this->configProviderAccount->getBuckarooFeeTaxClass();

$taxClassId = $this->configProviderBuckarooFee->getBuckarooFeeTaxClass($cart->getStore());

if (empty($taxClassId) || !is_numeric($taxClassId)) {
// If the tax class ID is not set, return zero tax amount to avoid errors
return 0.0;
}

Expand Down
12 changes: 6 additions & 6 deletions Test/Unit/Model/Total/Quote/BuckarooFeeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function testGetBaseFeeReturnsConfigValueIfIsNumber()
$quoteMock = $this->getFakeMock(Quote::class)->getMock();

$configProviderFeeMock = $this->getFakeMock(ConfigProviderBuckarooFee::class)
->setMethods(['getTaxClass', 'getPaymentFeeTax'])
->setMethods(['getBuckarooFeeTaxClass', 'getPaymentFeeTax'])
->getMock();
$configProviderFeeMock->expects($this->once())->method('getTaxClass')->willReturn(1);
$configProviderFeeMock->expects($this->once())->method('getBuckarooFeeTaxClass')->willReturn(1);
$configProviderFeeMock->expects($this->once())->method('getPaymentFeeTax')->willReturn($taxIncl);

$catalogHelper = $this->getFakeMock(Data::class)->setMethods(['getTaxPrice'])->getMock();
Expand Down Expand Up @@ -373,9 +373,9 @@ public function testCollectShouldReturnSelfIfFeeIsZero()
$configProviderFactoryMock->expects($this->once())->method('getPaymentFee')->willReturn($expectedFee);

$configProviderFeeMock = $this->getFakeMock(ConfigProviderBuckarooFee::class)
->setMethods(['getTaxClass','getPaymentFeeTax'])
->setMethods(['getBuckarooFeeTaxClass','getPaymentFeeTax'])
->getMock();
$configProviderFeeMock->expects($this->once())->method('getTaxClass')->willReturn(1);
$configProviderFeeMock->expects($this->once())->method('getBuckarooFeeTaxClass')->willReturn(1);

$catalogHelperMock = $this->getFakeMock(Data::class)->setMethods(['getTaxPrice'])->getMock();
$catalogHelperMock->expects($this->once())->method('getTaxPrice')->willReturn($expectedFee);
Expand Down Expand Up @@ -438,9 +438,9 @@ public function testCollectShouldSetTotalsOnQuote()
$priceCurrencyMock->method('convert')->with($fee, $store)->willReturn($fee);

$configProviderFeeMock = $this->getFakeMock(ConfigProviderBuckarooFee::class)
->setMethods(['getTaxClass', 'getPaymentFeeTax'])
->setMethods(['getBuckarooFeeTaxClass', 'getPaymentFeeTax'])
->getMock();
$configProviderFeeMock->expects($this->once())->method('getTaxClass')->willReturn(1);
$configProviderFeeMock->expects($this->once())->method('getBuckarooFeeTaxClass')->willReturn(1);
$configProviderFeeMock->expects($this->once())->method('getPaymentFeeTax')->willReturn($taxIncl);

$catalogHelperMock = $this->getFakeMock(Data::class)->setMethods(['getTaxPrice'])->getMock();
Expand Down
Loading