Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Magento 2 Custom Charge Error fixed With PayPal in Custom Module

    If you want to add custom charge in your module you can check this blog Custom Price Fee

    In Magento 2 Paypal is implemented with the module name Paypal, If the customer selects Paypal method for the payment then Magento\Paypal\Model\Cart will call instead of Magento\Payment\Model\Cart  it used for collect items and amount and validate the info collected before sending to PayPal.
    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, the amount is not same as order amount.

    I have described here that how to solve this issue in our custom Magento2 Modules.

    1) you need to create the plugin, go to NameSpace/ModuleName/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\Cart">
            <plugin name="webkul_update_paypal_custom_fee_order" type="NameSpace\ModuleName\Plugin\Model\UpdateCustomFeeForOrderPayPal"/>
        </type>
    </config>

    2) Now, Define this plug-in at the path NameSpace\ModuleName\Plugin\Model\UpdateCustomFeeForOrderPayPal.php

    <?php
    namespace NameSpace\ModuleName\Plugin\Model;
    class UpdateCustomFeeForOrderPayPal
    {
        /**
         * @var \Magento\Checkout\Model\Session
         */
        protected $_checkoutSession;
        const AMOUNT_SUBTOTAL = 'subtotal';
        public function __construct(
            \Magento\Checkout\Model\Session $checkoutSession
        ) {
            $this->_checkoutSession = $checkoutSession;
        }
    
        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'];
            
            // to check the paypal payment method
            if(in_array($paymentMethod,$paypalMehodList)){
              $result[self::AMOUNT_SUBTOTAL] = $result[self::AMOUNT_SUBTOTAL] + $quote->getCustomfee();
            
            }
            
            return $result;
        }
    }

    Here, we check after plugin, if you want to check how to create the plugin in Magento, You can check this blog Magento 2 Create & uses Of Plugins
    Here, afterGetAmounts we check if the payment method is PayPal then we add the customfee with the subtotal.
    Hope it will help you. Thank You.

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    2 comments

  • Amine Yakoubi
    Thanks for the solution
    • Anuj Gupta (Moderator)
      You are most welcome.
  • Back to Top