How to Manage Custom Amount with PayPal in Magento 2
Accept PayPal Payments in Multiple Currencies
Offer Your Customers the Facility of Paying in Their 20+ Currencies Through PayPal.
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:
Here, 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:
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:
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="payment_cart_collect_items_and_amounts"> <observer name="add_price_to_paypal" instance="Vendor\Extension\Observer\AddFeeToPaypal" /> </event> </config> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace Vendor\Extension\Observer; use \Magento\Framework\Event\ObserverInterface; use \Magento\Framework\Event\Observer; use \Magento\Checkout\Model\Session; class AddPriceToPaypal implements ObserverInterface { public $checkout; public function __construct(Session $checkout) { $this->checkout = $checkout; } public function execute(Observer $observer) { $cart = $observer->getEvent()->getCart(); $quote = $this->checkout->getQuote(); $extraFee = []; $extraFee[] = $this->serializer->unserialize($quote->getFeesAmount()); if ($extraFee) { foreach ($extraFee as $key => $fees) { $cart->addCustomItem('Extra Fee', 1, $fees[$key]); } } return $this; } } |
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…! 🍀
Install Magento 2 PayPal Multi-Currency and let customers pay in their preferred currency rather than the base currency.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
30+ User Generated Content Statistics You Need to Know [2024]
The Ultimate Shopify SEO Checklist You Need to Rank Your Store in 2024
Next