mollie / Shopware6

46 stars 52 forks source link

The amount of the order does not match the total amount from the order lines #562

Open ts-mothership opened 1 year ago

ts-mothership commented 1 year ago

Similar to issue #439 the high precision rounding does not seem to work on some cases. We tested the plugin version 3.4.0 and 3.6.0 in Shopware 6.4.17.2.

From the log:

[2023-04-11T15:00:36.109565+00:00] Mollie.DEBUG: Preparing Shopware Order 450792 to be sent to Mollie (Session: ...) {"amount":{"currency":"EUR","value":"30.03"},"locale":"de-AT","method":"sofort","lines":[{"type":"physical","name":"DIN 931 Sechskantschraube A2 M 8x85","quantity":50,"unitPrice":{"currency":"EUR","value":"0.48"},"totalAmount":{"currency":"EUR","value":"23.78"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"42256","imageUrl":"http%3A%2F%2Fshop.test%2Fmedia%2Fe5%2F18%2F9a%2F1676975641%2F2166_TI-15_B_2.PNG","productUrl":"","metadata":{"orderLineItemId":"cf2d46f01b4e47b195022e7b85da8232"}},{"type":"shipping_fee","name":"Delivery costs 1","quantity":1,"unitPrice":{"currency":"EUR","value":"6.25"},"totalAmount":{"currency":"EUR","value":"6.25"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"mol-delivery-1","imageUrl":"","productUrl":"","metadata":{"orderLineItemId":"e30904b31cea4010af7314450cd559fa"}},{"type":"physical","name":"Rundung","quantity":1,"unitPrice":{"currency":"EUR","value":"-0.01"},"totalAmount":{"currency":"EUR","value":"-0.01"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"R","imageUrl":"","productUrl":"","metadata":{"type":"rounding","orderLineItemId":""}}],"session":"","processors":{"uid":{"uid":"34bd133"},"web":{"url":"/at/account/order/update/25c0444cd3ea4748812997a996493eff","ip":"172.19.0.0","http_method":"POST","server":"_","referrer":"http://shop.test/at/account/order/edit/25c0444cd3ea4748812997a996493eff?error-code=CHECKOUT__MOLLIE_COULD_NOT_CREATE_PAYMENT_URL"}}} []
[2023-04-11T15:00:45.127118+00:00] Mollie.DEBUG: Mollie Order Request (Session: ...) {"body":{"amount":{"currency":"EUR","value":"30.03"},"locale":"de-AT","method":"sofort","orderNumber":"450792","payment":{"webhookUrl":"http://https://dev.shop.com/at/mollie/webhook/9b422c6d22714434a81ab4e493c51d9e","customerId":"cst_2z35JyZbzn"},"redirectUrl":"http://shop.test/at/mollie/payment/9b422c6d22714434a81ab4e493c51d9e","webhookUrl":"http://https://dev.shop.com/at/mollie/webhook/9b422c6d22714434a81ab4e493c51d9e","lines":[{"type":"physical","name":"DIN 931 Sechskantschraube A2 M 8x85","quantity":50,"unitPrice":{"currency":"EUR","value":"0.48"},"totalAmount":{"currency":"EUR","value":"23.78"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"42256","imageUrl":"http%3A%2F%2Fshop.test%2Fmedia%2Fe5%2F18%2F9a%2F1676975641%2F2166_TI-15_B_2.PNG","productUrl":"","metadata":{"orderLineItemId":"cf2d46f01b4e47b195022e7b85da8232"}},{"type":"shipping_fee","name":"Delivery costs 1","quantity":1,"unitPrice":{"currency":"EUR","value":"6.25"},"totalAmount":{"currency":"EUR","value":"6.25"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"mol-delivery-1","imageUrl":"","productUrl":"","metadata":{"orderLineItemId":"e30904b31cea4010af7314450cd559fa"}},{"type":"physical","name":"Rundung","quantity":1,"unitPrice":{"currency":"EUR","value":"-0.01"},"totalAmount":{"currency":"EUR","value":"-0.01"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"R","imageUrl":"","productUrl":"","metadata":{"type":"rounding","orderLineItemId":""}}],"billingAddress":{"title":"","givenName":"*","familyName":"*","email":"*","streetAndNumber":"*","postalCode":"1234","city":"Testort","country":"AT","organizationName":"*","phone":"*"},"shippingAddress":{"title":"","givenName":"*","familyName":"*","email":"*","streetAndNumber":"*","postalCode":"1234","city":"Testort","country":"AT","organizationName":"*","phone":"*"}},"session":"","processors":{"uid":{"uid":"34bd133"},"web":{"url":"/at/account/order/update/25c0444cd3ea4748812997a996493eff","ip":"172.19.0.0","http_method":"POST","server":"_","referrer":"http://shop.test/at/account/order/edit/25c0444cd3ea4748812997a996493eff?error-code=CHECKOUT__MOLLIE_COULD_NOT_CREATE_PAYMENT_URL"}}} []
[2023-04-11T15:00:45.597812+00:00] Mollie.CRITICAL: Could not create Mollie order (Session: ...) {"function":"finalize-payment","exception":"[object] (Mollie\\Api\\Exceptions\\ApiException(code: 422): [2023-04-11T15:00:45+0000] Error executing API call (422: Unprocessable Entity): The amount of the order does not match the total amount from the order lines. Expected order amount to be €30.02 but got €30.03. Documentation: https://docs.mollie.com/overview/handling-errors. Field: amount at /var/www/share/dev/htdocs/vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Client/MollieHttpClient.php:151)","session":"","processors":{"uid":{"uid":"34bd133"},"web":{"url":"/at/account/order/update/25c0444cd3ea4748812997a996493eff","ip":"172.19.0.0","http_method":"POST","server":"_","referrer":"http://shop.test/at/account/order/edit/25c0444cd3ea4748812997a996493eff?error-code=CHECKOUT__MOLLIE_COULD_NOT_CREATE_PAYMENT_URL"},"introspection":{"file":null,"line":null,"class":"Kiener\\MolliePayments\\Service\\MollieApi\\Order","callType":"->","function":"createOrder"}}} []
[2023-04-11T15:00:45.600586+00:00] Mollie.ERROR: Error when starting Mollie payment: Could not create Mollie order (Session: ...) {"function":"order-prepare","exception":"[object] (RuntimeException(code: 422): Could not create Mollie order at /var/www/share/dev/htdocs/vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Order.php:184)\n[previous exception] [object] (Mollie\\Api\\Exceptions\\ApiException(code: 422): [2023-04-11T15:00:45+0000] Error executing API call (422: Unprocessable Entity): The amount of the order does not match the total amount from the order lines. Expected order amount to be €30.02 but got €30.03. Documentation: https://docs.mollie.com/overview/handling-errors. Field: amount at /var/www/share/dev/htdocs/vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Client/MollieHttpClient.php:151)","session":"","processors":{"uid":{"uid":"34bd133"},"web":{"url":"/at/account/order/update/25c0444cd3ea4748812997a996493eff","ip":"172.19.0.0","http_method":"POST","server":"_","referrer":"http://shop.test/at/account/order/edit/25c0444cd3ea4748812997a996493eff?error-code=CHECKOUT__MOLLIE_COULD_NOT_CREATE_PAYMENT_URL"},"introspection":{"file":null,"line":null,"class":"Kiener\\MolliePayments\\Handler\\PaymentHandler","callType":"->","function":"pay"}}} []
not rounded rounded
Unit-Price 0,4755 € -
Quantity 50 -
Subtotal 23,775 € 23,78 €
Shipping 6,25 € 6,25 €
Grandtotal 30,025 € 30,03 €

It seems \Kiener\MolliePayments\Service\MollieApi\Fixer\RoundingDifferenceFixer::fixAmountDiff is rounding incorrectly. It compares the order value of '30.025' with the sum of already rounded line items '30.03'. This creates a diff of 0.005 which creates a new line item with a rounding position of -0.01 €.

So I guess you should either use the rounded order grand total or use the unrounded line items when comparing both values.

ts-mothership commented 1 year ago

To compare already rounded values I'd suggest a change in https://github.com/mollie/Shopware6/blob/master/src/Service/MollieApi/Builder/MollieLineItemBuilder.php#L119:

        if ($fixRoundingDifferences) {
            $mollieOrderLines = $this->roundingDiffFixer->fixAmountDiff(
-                $order->getAmountTotal(),
+                round($order->getAmountTotal(), MollieOrderPriceBuilder::MOLLIE_PRICE_PRECISION),
                $mollieOrderLines,
                $fixRoundingTitle,
                $fixRoundingSKU
            );
        }