How to Manage Custom Amount with PayPal in Magento 2

Are you facing issues with custom amounts while using PayPal payment method in Magento 2? Read this blog post to find the method to manage custom amount with paypal in Magento 2.

Magento 2 comes with built-in PayPal integration, the most famous international payment gateway solution. This built-in payment gateway integration powers store owners to accept payments from any corner around the globe. The integration works flawlessly most of the time and allows the customers to complete the payment easily through PayPal. Previously, one of my colleagues showed you the solution to Magento PayPal duplicate invoice error.

But, things go wrong when there is a custom amount added or discounted from the cart subtotal. This can be the extra fees in Magento 2 or the extra discounts you’re offering to the customers through custom fields. Take the following order total, for example:

How to Manage Custom Amount with PayPal in Magento 2Here, the cart subtotal is INR 100.00, the extra fee is INR 10.00 (custom amount), and the shipping is INR 5.0, amounting to the order total of INR 115.00. On proceeding with the payment for the above order with PayPal Express Checkout, the following error prevents the customers from making the payment:

Transaction refused because of an invalid argument in paypal

The error reads:

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

The error is produced because of the mismatch of the payment amount with the order total. The custom fields, extra fees in Magento 2 for example, are not passed to PayPal for the calculation of order totals, which produces the PayPal #10413b error.

Let’s see how we can manage custom amount with PayPal express checkout in Magento 2 using the addCustomItem() function.

Method to Manage Custom Amount with PayPal in Magento 2

In order to solve the above error, you need to pass each of the custom amounts to PayPal as well for processing. Follow the steps mentioned below:

Step 1: First, create an event file at app/code/Vendor/Extension/etc/events.xml with the following code:

Step 2: Now, you need to create an observer that will pass the custom amounts to the PayPal payment gateway using the addCustomItem function. Create an AddPriceToPaypal.php file at Vendor/Extension/Observer/ with the following code:

Tip: You can replace the 1 in ('Extra Fee', 1, $fees[$key]) with -1 if you want the amount to be deducted from the cart subtotal.

and you’re done!

This will solve the custom price payment issues with PayPal in Magento 2, i.e. “Transaction refused because of an invalid argument in PayPal”

I hope this post will help you add a custom fee to Paypal express checkout in Magento 2. 😇

If you still have any doubts or queries regarding the provided solution, feel free to ask in the comment section below. I would be happy to help you. 🙂

Found this solution helpful? Share it with your Magento friends via social media. 🐱‍🏍

Thanks for reading…! 🍀

Magento 2 PayPal Multi-Currency

Install Magento 2 PayPal Multi-Currency and let customers pay in their preferred currency rather than the base currency.

(based on 4 Reviews)
How to Manage Custom Amount with PayPal in Magento 2

Ronak Bhatti

Ronak is a Magento Developer at Meetanshi. He loves playing cricket. Apart from the work, he is an enthusiastic person who loves learning new skills.

Leave a Reply

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