Magento 2: PayPal gateway has rejected request. The totals of the cart item amounts do not match order amounts

PayPal gateway has rejected request. - SBDEVBLOG

PayPal gateway has rejected the request.? Have you added custom charges or tax to the total? If yes, then let’s fix it.

First, let’s talk about what Paypal is. Paypal is an international payment method that is widely used on e-commerce platforms. Hence, it also comes with Magento 2 (Adobe Commerce) as a built-in payment method.

We often require additional charges like custom fees, custom tax, etc. However, in such cases, we need help with PayPal while placing the orders at checkout.

We might get an error: PayPal gateway has rejected request. The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details).

If we look into the payment logs, we may find an error in the details under the key L_LONGMESSAGE0 of the arrays of responses: The totals of the cart item amounts do not match order amounts.

Looking at the request parameter in payment logs, we may find AMT, which stands for the total amount, which might differ from the expected total amount. However, on the payment screen of PayPal, we may see the full amount as the desired total amount, but in the log, we can see a difference from the screen.

PayPal calculates the total internally from requested parameters; if AMT differs from the provided sub-items of the capacity, then it will throw the error described above.

It may happen while adding custom charges to the total programmatically in Magento 2 (Adobe Commerce).

For example, if we add a custom tax, AMT should equal a given formula. AMT = SHIPPINGAMT + ITEMAMT + TAXAMT.

So, let’s fix the error. First, let’s assume we have added a custom tax, which caused the mentioned issue.

Magento 2: PayPal gateway has rejected request.

Firstly, we need to handle the total for PayPal to make AMT compatible with cart totals. To fix this, we need to use the plugin for the class given below:

Magento\Paypal\Model\Api\Nvp

We must generate di.xml in the module’s etc directory to do that.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Paypal\Model\Api\Nvp">
        <plugin name="fix_mismatch_amount_at_paypal" type="SbDevBlog\PaypalNvp\Plugin\Model\Nvp" />
    </type>
</config>

We must create a plugin class inside the mentioned path.

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */

namespace SbDevBlog\PaypalNvp\Plugin\Model;

use Magento\Paypal\Model\Api\AbstractApi;

class Nvp
{
    /**
     * Fix: The totals of the cart item amounts do not match order amounts.
     *
     * @param AbstractApi $subject
     * @param $title
     * @param array $request
     * @return array
     */
    public function beforeCall(AbstractApi $subject, $title, array $request)
    {
        if (isset($request['ITEMAMT']) && isset($request['SHIPPINGAMT']) && isset($request['TAXAMT'])) {
            $request['AMT'] = $request['ITEMAMT'] + $request['SHIPPINGAMT'] + $request['TAXAMT'];
        }
        return [$title, $request];
    }
}

That’s it. Please share your review by using the comment box. Thank you for reading the post-. PayPal gateway has rejected request.

Please subscribe and share SbDevBlog with your connection.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *