How to manage Custom amount with PayPal Express Checkout in Magento 2

How to manage custom amount with PayPal in Magento 2

Available in more than 200 markets & trusted by 200 million customers around the world, Paypal enables consumers and merchants to send & receive money in more than different currencies, PayPal is a worth considering option when it comes to choosing a payment Gateway for business. And, PayPal Express Checkout offered by PayPal allows the store customer to perform faster checkout in an ecommerce store with an enhanced layer of security. During the checkout, the customer is redirected to the secure PayPal webpage to complete the payment information. And once the transaction is completed, the customer is redirected back to your store. Adding PayPal Express Checkout to your Magento 2 store showcase the familiar PayPal button on your store, which has been reported to increase sales.
But several times it happens that while charging some extra fees on order doesn’t match with PayPal order total and it throws an error and doesn’t allow the customers to proceed further. To fix this issue, you need to manage this custom amount with PayPal Express Checkout by adding a piece of code in your Magento 2 store.
To do the same, first, we need to create an “events.xml” file inside our custom extension folder.
app\code\Vendor\Extension\etc\events.xml

<pre class="lang:default decode:true">
<?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="vendor_extension_payment_collect_total" instance="Vendor\Extension\Observer\AddCustomAmountItem" />
    </event>
</config>
</pre>

Now, we need to create one more file, for adding a custom amount in Paypal Checkout and display in subtotal.
app\code\Vendor\Extension\Observer\AddCustomAmountItem.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Observer;
 
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
 
class AddCustomAmountItem implements ObserverInterface
{
	public function execute(Observer $observer)
	{
    	/** @var \Magento\Payment\Model\Cart $cart */
    	$cart = $observer->getEvent()->getCart();
        $customAmount = 100;
        $cart->addCustomItem(__('Custom Amount'), 1, -1.00 * $customAmount, 'customfee');
	}
}
</pre>

That’s it! You have successfully manage your custom amount with Paypal Express Checkout.

If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.

And, Let us know if you are facing an issue while implementing this code.

Happy Coding!

Previous Article

How to get Dynamic Generated row value of System Configuration in Magento 2

Next Article

Top 6 Points Which Boost E-commerce Sales Fast for Upcoming Years

Write a Comment
  1. There’s a manual amount 100 in $CustomAmount. How to pick variable(X) entered in Extra Fee Amount in STORES>CONFIGURATION>>MAGECOMP>>>EXTRAFEE AMOUNT = X ?

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨