mollie / magento2

Mollie Payments for Magento 2
https://www.mollie.com
Other
98 stars 50 forks source link

Incorrect order lines when using Klarna payments and bundle item discounts (qty > 1) #619

Closed lphilippo closed 1 year ago

lphilippo commented 1 year ago

We encountered an issue for the combination of Klarna related payments and orders where discounts are applied on bundle items.

On submit we would receive the following error message:

Order line 2 is invalid. Total amount is off. Expected total amount to be €16.20 (2 × €9.00 - €1.80), got €7.20. Field: lines.2.totalAmount. 

Where the discount and unit price are as expected but the total of 7.20 is indeed incorrect. When diving into this, it is caused by the following line ignoring the quantity ( QtyOrderded ) of $orderItem. It feels odd that the total value does not keep the quantity in mind.

https://github.com/mollie/magento2/blob/6fd1e63583941c970e9efe9ab3caa7cc9a00ba1f/Service/Order/Lines/Processor/BundleWithoutDynamicPricing.php#L55

When we apply the following patch, we end up with the correct subtotal and vat amount, and the order continues successfully to your checkout interface.

--- a/mollie/magento2/Service/Order/Lines/Processor/BundleWithoutDynamicPricing.php
+++ b/mollie/magento2/Service/Order/Lines/Processor/BundleWithoutDynamicPricing.php
@@ -50,9 +50,10 @@
@@ -49,10 +49,11 @@
         $taxPercent = $orderItem->getTaxPercent();
         $unitPrice = $orderLine['totalAmount']['value'] / $orderItem->getQtyOrdered();
-        $newVatAmount = (($unitPrice - $discountAmount) / (100 + $taxPercent)) * $taxPercent;
+        $quantity = (float) $orderItem->getQtyOrdered();
+        $newVatAmount = (($quantity * $unitPrice - $discountAmount) / (100 + $taxPercent)) * $taxPercent;

         $orderLine['unitPrice'] = $this->mollieHelper->getAmountArray($currency, $unitPrice);
-        $orderLine['totalAmount'] = $this->mollieHelper->getAmountArray($currency, $unitPrice - $discountAmount);
+        $orderLine['totalAmount'] = $this->mollieHelper->getAmountArray($currency, $quantity * $unitPrice - $discountAmount);
         $orderLine['vatAmount'] = $this->mollieHelper->getAmountArray($currency, $newVatAmount);
         $orderLine['discountAmount'] = $this->mollieHelper->getAmountArray($currency, $discountAmount);

Can you please confirm whether or not the quantity is omitted here on purpose or not? Finally, we do not know why only Klarna payments are affected, but we assume that in those case there might be occurring more validations on your end.

lphilippo commented 1 year ago

Additionally, sorry for not including all requirements of the bug report, but the issue was added directly to a line in the source code (which does not load the default bug report structure, it seems) . In any case:

Describe the bug See original comment.

Used versions Magento 2.4.5-p1 (community) Mollie 2.20.0

To Reproduce Steps to reproduce the behavior:

  1. Add products, including a product with bundle items.
  2. Make sure the quantity of the discounted product is more than 1.
  3. Proceed to checkout

Expected behavior Continuing to Mollie Checkout environment to finish the order.

Actual behavior Redirect back to the cart page with the described error message.

Frank-Magmodules commented 1 year ago

Hello @lphilippo ,

Thanks for opening this issue. Could you specify a bit more about the applied discount? 
We have tried both Catalog Price Rules and Cart Price Rules, but in both cases, this seems to work just fine. 
We want to verify your fix before applying this to the code.

lphilippo commented 1 year ago

Hello @Frank-Magmodules

Simplified, we have a Cart Price Rule, which gives you a discount in percentage on certain products.

The products to which the discount applies are bundle items. The price of each bundle item itself is EUR 0,-, as the price is completely set on the underlying bundle options. Better said, the bundle product of "Jacket" has a price of EUR 0,-, but the linked simple items of "Red version" and "Black version" have a price of, respectively, EUR 50,- and EUR 60,- (in the "Bundle Items" configuration).

However, the price calculation itself seems to work perfectly, as the subtotal and unit price in the error message are completely accurate. Only the final result is off, due to the quantity not being kept in account for the tax and discount.

Therefor, it only happens for us, when the discounted product has a quantity larger than 1.

Frank-Magmodules commented 1 year ago

Good news @lphilippo , we were able to reproduce your issue and we've implemented your suggestion, thanks for that, we will update you once it's in an official release within this issue. Have a good weekend ahead!

Frank-Magmodules commented 1 year ago

HI There @lphilippo ,

We are delighted to let you know that the latest version of the Mollie plugin, now includes the solution you suggested. We are grateful for your contribution to improving the plugin, and we have included a special thank you note for you in our changelog!

We consider this issue to be resolved. Nevertheless, if you encounter any further difficulties, please do not hesitate to reopen the issue, and we'll be happy to help you.

lphilippo commented 1 year ago

Hi @Frank-Magmodules

Perhaps a bit superfluous, but we just wanted to confirm that our test cases indeed work as expected on version 2.24.0, where they still failed on 2.23.0.

Thanks for incorporating the changes!

Frank-Magmodules commented 1 year ago

Great @lphilippo , thank you for your confirmation here!