mollie / magento2

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

Issue with Klarna Slice/Pay Later payments and Fooman Order Fees #512

Closed gjportegies closed 2 years ago

gjportegies commented 2 years ago

Describe the bug We found orders in our backend that hang on the payment status 'Payment Review'. We contacted Mollie and we could not find any of those orders in the Mollie dashboard. After checking the mollie log files we found the following (I've anonimized a lot):

Mollie.INFO: request: {"amount":{"currency":"EUR","value":"414.80"},"orderNumber":"0000000000","billingAddress":{"organizationName":null,"title":"","givenName":"example","familyName":"example","email":"example@example.com","streetAndNumber":"Example 16","postalCode":"1111AA","city":"Amsterdam","region":null,"country":"NL"},"consumerDateOfBirth":null,"lines":[{"type":"physical","name":"Australian bermuda met twee ritsen en zwarte bies 20  olijfgroen","quantity":1,"unitPrice":{"currency":"EUR","value":"79.95"},"totalAmount":{"currency":"EUR","value":"79.95"},"vatRate":"21.00","vatAmount":{"currency":"EUR","value":"13.88"},"sku":"SKU-1","productUrl":""},{"type":"physical","name":"Australian duo jack zilveren borstbies 20  zwart - olijf groen","quantity":1,"unitPrice":{"currency":"EUR","value":"159.95"},"totalAmount":{"currency":"EUR","value":"159.95"},"vatRate":"21.00","vatAmount":{"currency":"EUR","value":"27.76"},"sku":"SKU-2","productUrl":""},{"type":"physical","name":"Australian duo print jack acetaat oldschool 27753","quantity":1,"unitPrice":{"currency":"EUR","value":"169.95"},"totalAmount":{"currency":"EUR","value":"169.95"},"vatRate":"21.00","vatAmount":{"currency":"EUR","value":"29.50"},"sku":"SKU-3","productUrl":""},{"type":"shipping_fee","name":"PostNL","quantity":1,"unitPrice":{"currency":"EUR","value":"0.00"},"totalAmount":{"currency":"EUR","value":"0.00"},"vatRate":"0.00","vatAmount":{"currency":"EUR","value":"0.00"},"sku":"tig_postnl_regular"}],"redirectUrl":"","webhookUrl":"","locale":"nl_NL","method":"klarnasliceit","metadata":{"order_id":"111044","store_id":"6","payment_token":""},"shippingAddress":{"organizationName":null,"title":"","givenName":"example","familyName":"example","email":"example@example.com","streetAndNumber":"Example 16","postalCode":"1111AA","city":"Amsterdam","region":null,"country":"NL"}} [] []

This is followed by the following error:

Mollie.ERROR: error: [<date>] 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 €409.85 but got €414.80. Field: amount. Documentation: https://docs.mollie.com/overview/handling-errors [] []

The user added an extra 'sticker pakket' which is added to the checkout with the Fooman Order Fees extension. Since this is an 'order fee' it is not a separate line on the order. The issue is only appearing when choosing one of the Klarna payment methods.

Used versions

To Reproduce Steps to reproduce the behavior:

  1. Add a product to the cart.
  2. Go to the checkout.
  3. Select an option added by the Order Fees module.
  4. Choose a Klarna payment method.
  5. Check the mollie.log for the error.

Expected behavior The order should be processed and send to Mollie.

Actual behavior The Mollie extension throws an error because the order lines total does not match the total amount.

Additional context Possible related:

fooman commented 2 years ago

Kristof here the developer of the above extension. I have had a look at some of the other custom order line implementations for other 3rd party extensions and believe something like the below might work. Please note that I do not have access to an instance with Mollie configured as such the code is not yet tested.

Mollie\Payment\Service\Order\Lines\Generator\FoomanTotals.php

<?php
/*
 * Copyright Magmodules.eu. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Mollie\Payment\Service\Order\Lines\Generator;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Framework\Module\Manager;
use Mollie\Payment\Helper\General;

class FoomanTotals implements GeneratorInterface
{
    /**
     * @var General
     */
    private $mollieHelper;

    /**
     * @var Manager
     */
    private $moduleManager;

    public function __construct(
        General $mollieHelper,
        Manager $moduleManager
    ) {
        $this->mollieHelper = $mollieHelper;
        $this->moduleManager = $moduleManager;
    }

    public function process(OrderInterface $order, array $orderLines): array
    {
        if (!$this->moduleManager->isEnabled('Fooman_Totals')) {
            return $orderLines;
        }

        $extAttr = $order->getExtensionAttributes();
        if (!$extAttr) {
            return $orderLines;
        }

        $foomanGroup = $extAttr->getFoomanTotalGroup();
        if (empty($foomanGroup)) {
            return $orderLines;
        }

        $totals = $foomanGroup->getItems();
        if (empty($totals)) {
            return $orderLines;
        }

        $forceBaseCurrency = (bool)$this->mollieHelper->useBaseCurrency($order->getStoreId());
        $currency = $forceBaseCurrency ? $order->getBaseCurrencyCode() : $order->getOrderCurrencyCode();

        foreach ($totals as $total) {
            $amount = $forceBaseCurrency ? $total->getBaseAmount() : $total->getAmount();
            $taxAmount = $forceBaseCurrency ? $total->getBaseTaxAmount() : $total->getTaxAmount();

            $vatRate = 0;
            if ($taxAmount) {
                $vatRate = round(($taxAmount / $amount) * 100, 2);
            }
            $orderLines[] = [
                'type' => 'surcharge',
                'name' =>  $total->getLabel(),
                'quantity' => 1,
                'unitPrice' => $this->mollieHelper->getAmountArray($currency, $amount + $taxAmount),
                'totalAmount' => $this->mollieHelper->getAmountArray($currency, $amount + $taxAmount),
                'vatRate' => $vatRate,
                'vatAmount' => $this->mollieHelper->getAmountArray($currency, $taxAmount)
            ];
        }

        return $orderLines;
    }
}

etc/di.xml

    <type name="Mollie\Payment\Service\Order\Lines\OrderLinesGenerator">
        <arguments>
            <argument name="generators" xsi:type="array">
                <item name="fooman_totals" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\FoomanTotals</item>
                <item name="weee_fee" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\WeeeFeeGenerator</item>
                <item name="mageworx_rewardpoints" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\MageWorxRewardPoints</item>
                <item name="amasty_extrafee" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\AmastyExtraFee</item>
                <item name="mirasvit_rewards" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\MirasvitRewards</item>
                <item name="aheadworks_addfreegift" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\AheadworksAddFreeGift</item>
                <item name="magento_giftcard" xsi:type="object">Mollie\Payment\Service\Order\Lines\Generator\MagentoGiftCard</item>
            </argument>
        </arguments>
    </type>
fooman commented 2 years ago

Just made one minor edit, this can go one package lower (Fooman_Totals) so this would theoretically support any extensions we build on top of that package (not just Surcharges).

gjportegies commented 2 years ago

We just tested the changes @fooman suggested on our test environment and everything worked as expected (Both methods Klarna Pay Later and Klarna Slice It).

Frank-Magmodules commented 2 years ago

HI, @gjportegies  and @fooman (Kristof),

We are happy to share that we've just released the new 2.11.0 version where we have fixed this issue.
Thank you for opening this issue, for the detailed issue report, and for your PR contribution! 

We are closing this issue now but please feel free to reopen the issue if this still occurs.