I develop custom extension which add custom fee to order based on payment method selection. Every thing working fine except "Paypal Express Checkout".

When I select this method, it added custom fee into total fine and it show on checkout like this.

enter image description here

To pass this custom fee into paypal express checkout, I do code like this.


<?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_fee_to_paypal" instance="Vendor\Extension\Observer\AddFeeToPaypal" />

And Observer file like this.

namespace Vendor\Extension\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use \Magento\Checkout\Model\Session;

class AddFeeToPaypal 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();
        $address = $quote->getIsVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();

        if ($fee = $address->getCustomFee())
            $cart->addCustomItem('Paypal Payment Fee', 1, $fee);

        return $this;

But is show me error like this.

PayPal gateway has rejected request. The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details).

enter image description here

How can I solve this error?

Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.