Back to Top

Manage Custom Amount with Paypal in Magento 2

Updated 19 February 2024

Hello,

If you want to manage custom fee in your Magento, then you can read the respective blog.

Now if in an order you added some custom amount, and want to pay the payment by Magento’s default Paypal payment method, then it throws the error that, amount is not same as order amount.

For example:

I create an order for RS 45.

Searching for an experienced
Magento 2 Company ?
Find out More

and added RS 1 as a custom amount in my order.

Now, if i am trying to place an order using Paypal express payment method then it gives me error:

Now, to solve this issue you have to manage that custom amount for the Paypal process also.

To manage it in Paypal Payment method you can follow the given process:

Add an Plugin in your module:

file path: app/code/Webkul/Test/etc/di.xml

<?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="webkul_paypal_custom_fee_api_call" type="Webkul\Test\Plugin\Model\Nvp"/>
    </type>

    <type name="Magento\Paypal\Model\Cart">
        <plugin name="webkul_update_paypal_custom_fee_order" type="Webkul\Test\Plugin\Model\UpdateCustomFeeForOrderPayPal"/>
    </type>
  
</config>

Now define this plugin, at path:

app/code/Webkul\Test\Plugin\Model\UpdateCustomFeeForOrderPayPal

<?php

namespace Webkul\Test\Plugin\Model;

class UpdateCustomFeeForOrderPayPal
{

    /**
     * checkout session var
     * @var \Magento\Checkout\Model\Session _checkoutSession
     */
    protected $_checkoutSession;

    public const AMOUNT_SUBTOTAL = 'subtotal';
    public const REQUEST_AMOUNT = 'AMT';

    /**
     * Init
     *
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(
        \Magento\Checkout\Model\Session$checkoutSession,
    ) {
        $this->_checkoutSession = $checkoutSession;
    }

    /**
     * Custom fee Plugin
     *
     * @param mixed $cart
     * @param mixed $result
     * @return mixed
     */
    public function afterGetAmounts($cart, $result)
    {
        $quote = $this->_checkoutSession->getQuote();
        $paymentMethod = $quote->getPayment()->getMethod();
        $paypalMehodList = ['payflowpro', 'payflow_link', 'payflow_advanced',
         'braintree_paypal', 'paypal_express_bml', 'payflow_express_bml', 'payflow_express', 'paypal_express'];
        if (in_array($paymentMethod, $paypalMehodList)) {
            $result[self::AMOUNT_SUBTOTAL] = $result[self::AMOUNT_SUBTOTAL] + $quote->getCustomFees();
            $customFee = $quote->getCustomFees();
            $cart->addCustomItem(__('Custom Fee'), 1, $customFee, 'Custom Fee');
            $subtotalAmount = $cart->getSubtotal() + $quote->getShippingAddress()->getShippingAmount();
            $cart->addSubtotal($subtotalAmount);
        }
         
        return $result;
    }
}

app/code/Webkul\Test\Plugin\Model\Nvp

<?php

namespace Webkul\Test\Plugin\Model;

use \Magento\Checkout\Model\Session as CheckoutSession;
use \Magento\Paypal\Model\Api\AbstractApi;

/**
 * NVP API wrappers model
 *
 * @method string getToken()
 */
class Nvp
{
    /**
     * checkout session var
     * @var \Magento\Checkout\Model\Session _checkoutSession
     */
    protected $_checkoutSession;

    /**
     * Init
     *
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(
        CheckoutSession $checkoutSession
    ) {
        $this->_checkoutSession = $checkoutSession;
    }
    /**
     * Before Call Method for updated request key
     *
     * @param AbstractApi $subject
     * @param string $title
     * @param array $request
     */
    public function beforeCall(AbstractApi $subject, $title, array $request)
    {
        $quote = $this->_checkoutSession->getQuote();
        if ($quote->getCustomFees()) {
          
             $request['AMT'] =  $request['ITEMAMT'] + $request['SHIPPINGAMT'] + $request['TAXAMT'];
        }
        return [$title, $request];
    }
}

After this code your order will get placed using Paypal payment method without any error.

I hope this blog will help you with Manage Custom Amount with Paypal in Magento 2. You may also check our wide range of best Magento 2 Extensions.

Please reach out to our team via a support ticket if you have any queries.

Try this and if you have any queries then just comment below 🙂

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home