{"id":2197,"date":"2022-11-17T07:30:53","date_gmt":"2022-11-17T07:30:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/manage-custom-amount-with-paypal-in-magento-2\/"},"modified":"2025-05-21T16:59:53","modified_gmt":"2025-05-21T11:29:53","slug":"manage-custom-amount-with-paypal-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/manage-custom-amount-with-paypal-in-magento-2\/","title":{"rendered":"How to Manage Custom Amount with PayPal in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Are you facing issues with custom amounts while using PayPal payment method in Magento 2? Read this blog post to find the method to <em><strong>manage custom amount with paypal in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 comes with built-in PayPal integration, the most <a title=\"Top International Payment Gateways\" href=\"https:\/\/meetanshi.com\/blog\/top-10-international-payment-gateways-worldwide\/\" target=\"_blank\" rel=\"noopener\">famous international payment gateway solution<\/a>. 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 <a title=\"What is PayPal?\" href=\"https:\/\/www.paypal.com\/us\/home\" target=\"_blank\" rel=\"noopener\">PayPal<\/a>. Previously, one of my colleagues showed you the solution to <a title=\"Magento PayPal duplicate invoice error\" href=\"https:\/\/meetanshi.com\/blog\/magento-paypal-duplicate-invoice-error\/\" target=\"_blank\" rel=\"noopener\">Magento PayPal duplicate invoice error<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;re offering to the customers through custom fields. Take the following order total, for example:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/11\/how-to-manage-custom-price-with-paypal-in-magento-2.png\" alt=\"How to Manage Custom Amount with PayPal in Magento 2\" class=\"wp-image-27302\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/11\/Transaction-refused-because-of-an-invalid-argument.png\" alt=\"Transaction refused because of an invalid argument in paypal\" class=\"wp-image-27303\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The error reads:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>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)<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The error is produced because of the mismatch of the payment amount with the order total. The custom fields, <a title=\"Magento 2 Extra Fees\" href=\"https:\/\/meetanshi.com\/magento-2-extra-fee.html\" target=\"_blank\" rel=\"noopener\">extra fees in Magento 2<\/a> for example, are not passed to PayPal for the calculation of order totals, which produces the PayPal #10413b error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see how we can manage custom amount with PayPal express checkout in Magento 2 using the <code>addCustomItem()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Manage Custom Amount with PayPal in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong>First, create an event file at <strong>app\/code\/Vendor\/Extension\/etc\/events.xml<\/strong> with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--?xml version=\"1.0\"?--><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:&nbsp;<\/strong>Now, you need to create an observer that will pass the custom amounts to the PayPal payment gateway using the <code>addCustomItem<\/code> function. Create an <em><strong>AddPriceToPaypal.php <\/strong><\/em>file at <strong>Vendor\/Extension\/Observer\/<\/strong> with the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?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;\n    }\n\n    public function execute(Observer $observer)\n    {\n        $cart = $observer->getEvent()->getCart();\n        $quote = $this->checkout->getQuote();\n        $extraFee = [];\n        $extraFee[] = $this->serializer->unserialize($quote->getFeesAmount());\n        if ($extraFee) {\n                foreach ($extraFee as $key => $fees) {\n                    $cart->addCustomItem('Extra Fee', 1, $fees[$key]);\n                }\n            }\n\n        return $this;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Tip: <\/strong>You can replace the 1 in <code>('Extra Fee', 1, $fees[$key])<\/code> with -1 if you want the amount to be deducted from the cart subtotal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">and you&#8217;re done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will solve the custom price payment issues with PayPal in Magento 2, i.e. &#8220;Transaction refused because of an invalid argument in PayPal&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this post will help you add a custom fee to Paypal express checkout in Magento 2.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Found this solution helpful? Share it with your Magento friends via social media.  \u200d <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading&#8230;!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2197","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2197"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2197\/revisions"}],"predecessor-version":[{"id":13841,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2197\/revisions\/13841"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}