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 :
<?xml version="1.0"?> <!-- /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Sales\Model\Order"> <plugin name="update_payment_fee_order" type="Ibnab\PF\Plugin\UpdateFeeForOrder"/> </type> <type name="Magento\Paypal\Model\Cart"> <plugin name="update_paypal_fee_order" type="Ibnab\PF\\Plugin\UpdateFeeForOrder"/> </type> </config>
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 :
<?php namespace Ibnab\PF\Plugin; class UpdateFeeForOrder { /** * @var \Magento\Quote\Model\QuoteFactory */ protected $quote; protected $logger; protected $_checkoutSession; protected $_registry; const AMOUNT_Payment = 'payment_fee'; const AMOUNT_SUBTOTAL = 'subtotal'; public function __construct( \Magento\Quote\Model\Quote $quote, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\Registry $registry ) { $this->quote = $quote; $this->logger = $logger; $this->_checkoutSession = $checkoutSession; $this->_registry = $registry; } /** * Get shipping, tax, subtotal and discount amounts all together * * @return array */ public function afterGetAmounts($cart,$result) { $total = $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']; $total[self::AMOUNT_SUBTOTAL] = $total[self::AMOUNT_SUBTOTAL] + $quote->getFeeAmount(); } return $total; } /** * Get shipping, tax, subtotal and discount amounts all together * * @return array */ public function beforeGetAllItems($cart) { $paypalTest = $this->_registry->registry('is_paypal_items')? $this->_registry->registry('is_paypal_items') : 0; $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']; { $cart->addCustomItem(__("Payment Fee"), 1 ,$quote->getFeeAmount()); $reg = $this->_registry->registry('is_paypal_items'); $current = $reg + 1 ; $this->_registry->unregister('is_paypal_items'); $this->_registry->register('is_paypal_items', $current); } } } }
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