Manage custom amount with paypal Magento2

Hello,

If you want to manage custom fee in your magento, then you can follow following blogs to manage them.

Add custom amount order total magento2

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 800.

and added RS -246 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 observer in your module:

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

<?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="Webkul_Test_payment_collect_total" instance="Webkul\Test\Observer\AddCustomAmountItem" />
    </event>
</config>

Now define this observer, at path:

app/code/Webkul/Test/Observer/AddCustomAmountItem.php

<?php

namespace Webkul\Test\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Add Weee item to Payment Cart amount.
 */
class AddCustomAmountItem implements ObserverInterface
{
    /**
     * Add custom amount as custom item to payment cart totals.
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Payment\Model\Cart $cart */
        $cart = $observer->getEvent()->getCart();
        $customAmount = 246;
        $cart->addCustomItem(__('Custom Amount'), 1, -1.00 * $customAmount, 'customfee');
    }
}

Here, addCustomItem() function is added your custom amount field in cart, and then paypal manages the amount as well.

in this, first parameter is,  the field name, i.e. amount field name.

second parameter is, quantity for the field.

third parameter is, amount, which you want to add, in example i added (-1.00 *), because i want to pass negative value.

forth parameter is, identifier, i.e. id of your custom amount field.

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

Hope this blog will help you in implementation of functionality in magento.

Thank you.

. . .

Ask a Feature

css.php