Skip to content

Commit 887f476

Browse files
Merge pull request #80 from unzerdev/develop
Release version 3.2.6
2 parents 81f8f81 + f09d92b commit 887f476

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55

6+
## [3.2.6](https://github.com/unzerdev/magento2/compare/3.2.5..3.2.6)
7+
### Fixed
8+
* Discount amount when taxes are applied
9+
610
## [3.2.5](https://github.com/unzerdev/magento2/compare/3.2.4..3.2.5)
711
### Added
812
* Direct Bank Transfer

Helper/Order.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Unzer\PAPI\Helper;
55

6+
use Magento\Framework\App\Config\ScopeConfigInterface;
67
use Magento\Framework\App\ProductMetadataInterface;
78
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Framework\Module\ModuleListInterface;
@@ -12,6 +13,8 @@
1213
use Magento\Sales\Api\Data\OrderAddressInterface;
1314
use Magento\Sales\Model\Order as OrderModel;
1415
use Magento\Sales\Model\Order\Item;
16+
use Magento\Store\Model\ScopeInterface;
17+
use Magento\Tax\Model\Config as MagentoTaxConfig;
1518
use Unzer\PAPI\Block\System\Config\Form\Field\BirthDateFactory;
1619
use Unzer\PAPI\Model\Config;
1720
use Unzer\PAPI\Model\Source\CreateThreatMetrixId;
@@ -54,6 +57,11 @@ class Order
5457
*/
5558
private ProductMetadataInterface $_productMetadata;
5659

60+
/**
61+
* @var ScopeConfigInterface
62+
*/
63+
private $scopeConfig;
64+
5765
/**
5866
* @var BasketFactory
5967
*/
@@ -80,6 +88,7 @@ class Order
8088
* @param Config $moduleConfig
8189
* @param ModuleListInterface $moduleList
8290
* @param ProductMetadataInterface $productMetadata
91+
* @param ScopeConfigInterface $scopeConfig
8392
* @param BasketFactory $basketFactory
8493
* @param BasketItemFactory $basketItemFactory
8594
* @param BirthDateFactory $birthDateFactory
@@ -89,6 +98,7 @@ public function __construct(
8998
Config $moduleConfig,
9099
ModuleListInterface $moduleList,
91100
ProductMetadataInterface $productMetadata,
101+
ScopeConfigInterface $scopeConfig,
92102
BasketFactory $basketFactory,
93103
BasketItemFactory $basketItemFactory,
94104
BirthDateFactory $birthDateFactory,
@@ -97,6 +107,7 @@ public function __construct(
97107
$this->_moduleConfig = $moduleConfig;
98108
$this->_moduleList = $moduleList;
99109
$this->_productMetadata = $productMetadata;
110+
$this->scopeConfig = $scopeConfig;
100111
$this->basketFactory = $basketFactory;
101112
$this->basketItemFactory = $basketItemFactory;
102113
$this->birthDateFactory = $birthDateFactory;
@@ -113,6 +124,7 @@ public function __construct(
113124
public function createBasketForOrder(OrderModel $order): Basket
114125
{
115126
$basket = $this->createBasket($order);
127+
$vatRate = 0;
116128

117129
if ($order->getShippingAmount() > 0) {
118130
$basket->addBasketItem(
@@ -132,11 +144,15 @@ public function createBasketForOrder(OrderModel $order): Basket
132144
$basket->addBasketItem(
133145
$this->createBasketItem($orderItem)
134146
);
147+
148+
if($orderItem->getTaxPercent() !== null){
149+
$vatRate = (float)$orderItem->getTaxPercent();
150+
}
135151
}
136152

137153
if (abs($order->getBaseDiscountAmount()) > 0) {
138154
$basket->addBasketItem(
139-
$this->createVoucherItem($order)
155+
$this->createVoucherItem($order, $vatRate)
140156
);
141157
}
142158

@@ -198,12 +214,36 @@ protected function createBasketItem(Item $orderItem): BasketItem
198214
* Create Voucher Item
199215
*
200216
* @param OrderModel $order
217+
* @param float $vatRate
201218
* @return BasketItem
202219
*/
203-
protected function createVoucherItem(OrderModel $order): BasketItem
220+
protected function createVoucherItem(OrderModel $order, float $vatRate): BasketItem
204221
{
222+
$discount = $order->getBaseDiscountAmount();
223+
$storeId = $order->getStoreId();
224+
225+
$taxAfterDiscount = $this->scopeConfig->getValue(
226+
MagentoTaxConfig::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT,
227+
ScopeInterface::SCOPE_STORE,
228+
$storeId
229+
);
230+
231+
$pricesIncludeTax = $this->scopeConfig->getValue(
232+
MagentoTaxConfig::CONFIG_XML_PATH_PRICE_INCLUDES_TAX,
233+
ScopeInterface::SCOPE_STORE,
234+
$storeId
235+
);
236+
237+
if ($taxAfterDiscount && !$pricesIncludeTax) {
238+
$discount *= (1 + $vatRate/100);
239+
}
240+
205241
$basketVoucherItemDiscountAmount = $this->basketItemFactory->create();
206-
$basketVoucherItemDiscountAmount->setAmountDiscountPerUnitGross(abs($order->getBaseDiscountAmount()));
242+
$basketVoucherItemDiscountAmount->setAmountDiscountPerUnitGross(
243+
abs(round($discount, 2, PHP_ROUND_HALF_DOWN)
244+
)
245+
);
246+
$basketVoucherItemDiscountAmount->setVat($vatRate);
207247
$basketVoucherItemDiscountAmount->setAmountPerUnitGross(0);
208248
$basketVoucherItemDiscountAmount->setQuantity(1);
209249
$basketVoucherItemDiscountAmount->setTitle('Discount');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "unzerdev/magento2",
33
"description": "This extension for Magento 2 provides a direct integration of the Unzer payment types to your Magento 2 shop via the Unzer Payment API (PAPI).",
44
"type": "magento2-module",
5-
"version": "3.2.5",
5+
"version": "3.2.6",
66
"license": "Apache-2.0",
77
"require": {
88
"php": "~7.4.0|~8.1.0|~8.2.0|~8.3.0",

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
4-
<module name="Unzer_PAPI" setup_version="3.2.5">
4+
<module name="Unzer_PAPI" setup_version="3.2.6">
55
<sequence>
66
<module name="Magento_Checkout"/>
77
<module name="Magento_Config" />

0 commit comments

Comments
 (0)