Skip to content

Commit

Permalink
Merge branch 'bugfix/show-the-payment-fee-tax' into release-week-45
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Nov 6, 2023
2 parents 321339e + cf5e5b8 commit b8e4830
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 13 deletions.
88 changes: 75 additions & 13 deletions Model/PaymentFee/Quote/Address/Total/PaymentFeeTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@

namespace Mollie\Payment\Model\PaymentFee\Quote\Address\Total;

use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Api\Data\AddressInterfaceFactory as CustomerAddressFactory;
use Magento\Customer\Api\Data\RegionInterfaceFactory as CustomerAddressRegionFactory;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory;
use Magento\Tax\Api\Data\QuoteDetailsItemExtensionInterfaceFactory;
use Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory;
use Magento\Tax\Api\Data\TaxClassKeyInterface;
use Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory;
use Magento\Tax\Api\TaxCalculationInterface;
use Magento\Tax\Helper\Data as TaxHelper;
use Magento\Tax\Model\Config as TaxConfig;
use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;
use Mollie\Payment\Config;
use Mollie\Payment\Exceptions\UnknownPaymentFeeType;
use Mollie\Payment\Service\Config\PaymentFee as PaymentFeeConfig;
use Mollie\Payment\Service\PaymentFee\Calculate;
use Mollie\Payment\Service\PaymentFee\Result;

class PaymentFeeTax extends AbstractTotal
class PaymentFeeTax extends CommonTaxCollector
{
/**
* @var PaymentFeeConfig
*/
private $paymentFeeConfig;

/**
* @var PriceCurrencyInterface
*/
Expand All @@ -41,14 +46,55 @@ class PaymentFeeTax extends AbstractTotal
private $config;

public function __construct(
PaymentFeeConfig $paymentFeeConfig,
PriceCurrencyInterface $priceCurrency,
TaxConfig $taxConfig,
TaxCalculationInterface $taxCalculationService,
QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory,
QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory,
TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory,
CustomerAddressFactory $customerAddressFactory,
CustomerAddressRegionFactory $customerAddressRegionFactory,
Calculate $calculate,
Config $config
PriceCurrencyInterface $priceCurrency,
Config $config,
TaxHelper $taxHelper = null,
QuoteDetailsItemExtensionInterfaceFactory $quoteDetailsItemExtensionInterfaceFactory = null,
?CustomerAccountManagement $customerAccountManagement = null
) {
$this->paymentFeeConfig = $paymentFeeConfig;
$this->priceCurrency = $priceCurrency;
$parent = new \ReflectionClass(parent::class);
$parentConstructor = $parent->getConstructor();

// The parent call fails when running setup:di:compile in 2.4.3 and lower due to an extra parameter.
if ($parentConstructor->getNumberOfParameters() == 9) {
// @phpstan-ignore-next-line
parent::__construct(
$taxConfig,
$taxCalculationService,
$quoteDetailsDataObjectFactory,
$quoteDetailsItemDataObjectFactory,
$taxClassKeyDataObjectFactory,
$customerAddressFactory,
$customerAddressRegionFactory,
$taxHelper,
$quoteDetailsItemExtensionInterfaceFactory
);
} else {
// @phpstan-ignore-next-line
parent::__construct(
$taxConfig,
$taxCalculationService,
$quoteDetailsDataObjectFactory,
$quoteDetailsItemDataObjectFactory,
$taxClassKeyDataObjectFactory,
$customerAddressFactory,
$customerAddressRegionFactory,
$taxHelper,
$quoteDetailsItemExtensionInterfaceFactory,
$customerAccountManagement
);
}

$this->calculate = $calculate;
$this->priceCurrency = $priceCurrency;
$this->config = $config;
}

Expand All @@ -74,10 +120,26 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig

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

$feeDataObject = $this->quoteDetailsItemDataObjectFactory->create()
->setType('mollie_payment_fee')
->setCode('mollie_payment_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);

$extensionAttributes = $quote->getExtensionAttributes();

if (!$extensionAttributes) {
return $this;
}
Expand Down
104 changes: 104 additions & 0 deletions Plugin/Tax/Helper/DataPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Plugin\Tax\Helper;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Model\Order\Tax\Item;
use Magento\Tax\Api\Data\OrderTaxDetailsItemInterface;
use Magento\Tax\Api\OrderTaxManagementInterface;

class DataPlugin
{
/**
* @var OrderTaxManagementInterface
*/
private $orderTaxManagement;

public function __construct(
OrderTaxManagementInterface $orderTaxManagement
) {
$this->orderTaxManagement = $orderTaxManagement;
}

public function afterGetCalculatedTaxes(object $callable, array $result, $source): array
{
if (!$source instanceof InvoiceInterface &&
!$source instanceof CreditmemoInterface
) {
return $result;
}

$order = $source->getOrder();
$orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getId());

$items = array_filter($orderTaxDetails->getItems(), function (Item $item) {
return $item->getType() == 'mollie_payment_fee_tax';
});

if (count($items) === 0) {
return $result;
}

foreach ($items as $item) {
$result = $this->aggregateTaxes($result, $item, 1);
}

return $result;
}

/**
* Copied from \Magento\Tax\Helper\Data::_aggregateTaxes
*
* @param $taxClassAmount
* @param OrderTaxDetailsItemInterface $itemTaxDetail
* @param $ratio
* @return array
*/
private function aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $itemTaxDetail, $ratio)
{
$itemAppliedTaxes = $itemTaxDetail->getAppliedTaxes();
foreach ($itemAppliedTaxes as $itemAppliedTax) {
$taxAmount = $itemAppliedTax->getAmount() * $ratio;
$baseTaxAmount = $itemAppliedTax->getBaseAmount() * $ratio;

if (0 == $taxAmount && 0 == $baseTaxAmount) {
continue;
}
$taxCode = $this->getKeyByName($taxClassAmount, $itemAppliedTax->getCode());
if (!isset($taxClassAmount[$taxCode])) {
$taxClassAmount[$taxCode]['title'] = $itemAppliedTax->getTitle();
$taxClassAmount[$taxCode]['percent'] = $itemAppliedTax->getPercent();
$taxClassAmount[$taxCode]['tax_amount'] = $taxAmount;
$taxClassAmount[$taxCode]['base_tax_amount'] = $baseTaxAmount;
} else {
$taxClassAmount[$taxCode]['tax_amount'] += $taxAmount;
$taxClassAmount[$taxCode]['base_tax_amount'] += $baseTaxAmount;
}
}

return $taxClassAmount;
}

/**
* @param array $taxClassAmount
* @param string $name
* @return string|int
*/
private function getKeyByName(array $taxClassAmount, string $name)
{
foreach ($taxClassAmount as $key => $tax) {
if ($tax['title'] === $name) {
return $key;
}
}

return $name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private function getShippingAssignment()
{
/** @var AddressInterface $address */
$address = $this->objectManager->create(AddressInterface::class);
$address->setQuote($this->getQuote());

/** @var ShippingInterface $shipping */
$shipping = $this->objectManager->create(ShippingInterface::class);
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@
</argument>
</arguments>
</type>

<type name="Magento\Tax\Helper\Data">
<plugin name="mollie_add_payment_fee_to_tax_total" type="Mollie\Payment\Plugin\Tax\Helper\DataPlugin" />
</type>
</config>

0 comments on commit b8e4830

Please sign in to comment.