Kevin
Kevin

Reputation: 153

How to charge Magento 2 Payment Fee

I want to charge extra fee, according to the selected payment method. I've used following module to do it in magento 1.
https://github.com/manishiitg/excellence_magento_blog/tree/master/Fee%20Module/app

Is there a similar module for magento 2.

Upvotes: 2

Views: 7505

Answers (3)

Prince Patel
Prince Patel

Reputation: 3060

You need to recalculate totals by selecting payment method by using Magento_Checkout/js/action/select-payment-method

define(
[
    'jquery',
    'ko',
    'Magento_Checkout/js/model/quote',
    'Mageprince_Paymentfee/js/action/checkout/cart/totals'
],
function($, ko ,quote, totals) {
    'use strict';
    var isLoading = ko.observable(false);

    return function (paymentMethod) {
        quote.paymentMethod(paymentMethod);
        totals(isLoading, paymentMethod['method']);
    }
});

Here is the full source code in my repository with lots of features like add shipping/discount in subtotal, add any number of payment method fees, Calculate tax by specific tax class, etc.

https://github.com/mageprince/magento2-paymentfee

Upvotes: 0

Khoa Truong
Khoa Truong

Reputation: 933

We can use a payment with specific fee module in Magento 2: https://github.com/mrkhoa99/Boolfly_payment_fee

This module will create a Cash On Delivery module with a specific fee. We can follow this guide to create your own module.

Upvotes: 3

Ibnab
Ibnab

Reputation: 139

First you need explore the complete process of how to add fee to order totals in magento2 in this link : https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2 and after that : create di.xml and push for exmaple :

<?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>

for create plugin and injected to afterGetAmounts($cart,$result) , and beforeGetAllItems($cart)

<?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,
        \Psr\Log\LoggerInterface $logger,
        \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'];

        if(in_array($paymentMethod,$paypalMehodList)){
        $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'];
        if($paypalTest < 3 && in_array($paymentMethod,$paypalMehodList)){
        if(method_exists($cart , 'addCustomItem' ))
        {
        $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 the complete modification we test and add fee paypal to amounts and add fee as custom item requested to paypal , the compete course is inside Magento 2 Paypal Fee (charge) and life cycle

Upvotes: 2

Related Questions