/   /   /  Magento 2 Paypal Fee (charge) and life cycle

Note:

For more extensions and themes visit our store

Magento 2 Paypal Fee (charge) and life cycle


In magento 2 paypal is implemented with module Payal , in case if the customer choose payapl as method of payment M2 use Magento\Paypal\Model\Cart in place of \Magento\Payment\Model\Cart , it used for collect items and amount and validate the infos collected before sending to paypal ,

The model have more additional methods as (it extend from parent Magento\Paypal\Model\Cart) :
getAmounts()  : Get shipping, tax, subtotal and discount amounts all together
_validate( ) : Check the line items and totals according to PayPal business logic limitations
_importItemsFromSalesModel() : Import items from sales model with workarounds for PayPal

Yeah after collecting the amounts and validate  let's consider we choose Paypal express , the checkout redirect the function Paypal/Controller/Express/Start Action but inside this action it use the global Api class Model/Api/Nvp.php

Example of customize  Payapl Fee :
First you need explore the complete process of how to add fee to order totals in magento2 in this link : http://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2
Ok you want add charge in case of the customer use paypal as method payment , we need add custom item to request sent to paypal and change the total amount sent (the class responsive is Nvp.php),
ok you need create plugin , go to Ibnab/PF/etc/di.xml or Ibnab/PF/etc/frontend/di.xml and write this lines :

  1.  
  2. <?xml version="1.0"?>
  3. <!--
  4. /**
  5.  * Copyright © 2015 Magento. All rights reserved.
  6.  * See COPYING.txt for license details.
  7.  */
  8. -->
  9. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  10.     <type name="Magento\Sales\Model\Order">
  11.         <plugin name="update_payment_fee_order" type="Ibnab\PF\Plugin\UpdateFeeForOrder"/>
  12.     </type>
  13.     <type name="Magento\Paypal\Model\Cart">
  14.         <plugin name="update_paypal_fee_order" type="Ibnab\PF\\Plugin\UpdateFeeForOrder"/>
  15.     </type>
  16. </config>
  17.  

Here we modified to method getAllItems() of parent class \Magento\Payment\Model\Cart and getAmounts() of Magento\Paypal\Model\Cart
And After that create cretae the class  Ibnab/PF/Plugin/UpdateFeeForOrder.php :

  1.  
  2. <?php
  3. namespace Ibnab\PF\Plugin;
  4. class UpdateFeeForOrder
  5. {
  6.     /**
  7.      * @var \Magento\Quote\Model\QuoteFactory
  8.      */
  9.     protected $quote;
  10.     protected $logger;
  11.     protected $_checkoutSession;
  12.     protected $_registry;
  13.     const AMOUNT_Payment = 'payment_fee';
  14.     const AMOUNT_SUBTOTAL = 'subtotal';
  15.     public function __construct(
  16.         \Magento\Quote\Model\Quote $quote,
  17.         \Psr\Log\LoggerInterface $logger,
  18.         \Magento\Checkout\Model\Session $checkoutSession,
  19.         \Magento\Framework\Registry $registry
  20.     ) {
  21.         $this->quote = $quote;
  22.         $this->logger = $logger;
  23.         $this->_checkoutSession = $checkoutSession;
  24.         $this->_registry = $registry;
  25.     }
  26.     /**
  27.      * Get shipping, tax, subtotal and discount amounts all together
  28.      *
  29.      * @return array
  30.      */
  31.     public function afterGetAmounts($cart,$result)
  32.     {
  33.         $total = $result;
  34.         $quote = $this->_checkoutSession->getQuote();
  35.         $paymentMethod = $quote->getPayment()->getMethod();
  36.         $paypalMehodList = ['payflowpro','payflow_link','payflow_advanced','braintree_paypal','paypal_express_bml','payflow_express_bml','payflow_express','paypal_express'];
  37.         
  38.         if(in_array($paymentMethod,$paypalMehodList)){
  39.         $total[self::AMOUNT_SUBTOTAL] = $total[self::AMOUNT_SUBTOTAL] + $quote->getFeeAmount();
  40.         
  41.         }
  42.         
  43.         return  $total;
  44.     }
  45.     /**
  46.      * Get shipping, tax, subtotal and discount amounts all together
  47.      *
  48.      * @return array
  49.      */
  50.     public function beforeGetAllItems($cart)
  51.     {
  52.         $paypalTest = $this->_registry->registry('is_paypal_items')? $this->_registry->registry('is_paypal_items') : 0;
  53.         $quote = $this->_checkoutSession->getQuote();
  54.         $paymentMethod = $quote->getPayment()->getMethod();
  55.         
  56.         $paypalMehodList = ['payflowpro','payflow_link','payflow_advanced','braintree_paypal','paypal_express_bml','payflow_express_bml','payflow_express','paypal_express'];
  57.         if($paypalTest < 3 && in_array($paymentMethod,$paypalMehodList)){
  58.         if(method_exists($cart , 'addCustomItem' ))
  59.         {
  60.         $cart->addCustomItem(__("Payment Fee"), 1 ,$quote->getFeeAmount());
  61.         $reg = $this->_registry->registry('is_paypal_items');
  62.         $current = $reg + 1 ;
  63.         $this->_registry->unregister('is_paypal_items');
  64.         $this->_registry->register('is_paypal_items', $current);
  65.         }
  66.         }
  67.     }
  68. }
  69.  

Here we  use the concept of after method in plugin it pass ed 2 Arguments the object of class and the result final , in  afterGetAmounts  we test if current payment method is paypal we add the charge ,
and in second method we used the concept of before and passed the object of class :
we have here 2 test for the current method and the number of access to method for ignoring the duplicate item .

You can get the complet Magento 2 Payment Fee support Paypal

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.