Surcharge Fee for Credit Card Payment in magento2

If you would just like to add the 3.5% surcharge without showing it as a separate component you could hook into the event: sales_quote_save_before and sales_quote_address_save_before and write code to add 4.5% surcharge to quote and address grand total

if $quoteObject->getPayment()->getMethod() == ‘cc’

Here are the steps I would have followed to implement the above request -:

Step 1 –

Create two fields (surcharge and base surcharge) against quote address and order

startSetup();

$installer->run(“ALTER TABLE ".$this->getTable('sales/quote_address')." ADD surcharge DECIMAL( 10, 2 ) NOT NULL”);
$installer->run(“ALTER TABLE ".$this->getTable('sales/quote_address')." ADD base_surcharge DECIMAL( 10, 2 ) NOT NULL”);

$installer->run(“ALTER TABLE ".$this->getTable('sales/order')." ADD surcharge DECIMAL( 10, 2 ) NOT NULL”);
$installer->run(“ALTER TABLE ".$this->getTable('sales/order')." ADD base_surcharge DECIMAL( 10, 2 ) NOT NULL”);

$installer->endSetup();

Step 2 – Add the following to config.xml


0.0.1 CompanyName_Surcharge_Block CompanyName_Surcharge_Block_Sales_Order_Total Company_Surcharge_Helper CompanyName_Surcharge_Model CompanyName_Surcharge_Model_Total_Quote subtotal tax.

Step 3 – Create Model app\code\community\CompanyName\Surcharge\Model\Total\Quote.php

<?php

class CompanyName_Surcharge_Model_Total_Quote extends Mage_Sales_Model_Quote_Address_Total_Abstract {

protected $_code = 'surcharge';

/**
 * Initialize surcharge totals collector
 */
public function __construct()
{
    $this->setCode($this->_code);
}

public function collect(Mage_Sales_Model_Quote_Address $address) 
{
    parent::collect($address);

    $this->_setAmount(0);
    $this->_setBaseAmount(0);

    $items = $this->_getAddressItems($address);
    if (!count($items)) {
        return $this;
    }

    $grandTotal= Mage::getModel('sales/quote_address')->load($address->getId())->getData('grand_total');            
    $baseSurcharge = 0;
    $surcharge = 0;

    $baseSurcharge += (($grandTotal) * (3.5 / 100));
    $surcharge += Mage::app()->getStore()->convertPrice($baseSurcharge);

    $address->setSurcharge($surcharge);
    $address->setBaseSurcharge($baseSurcharge);

    $this->_addAmount($address->getSurcharge());
    $this->_addBaseAmount($address->getBaseSurcharge());

    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address) {
    $amount = $address->getSurcharge();
    if ($amount > 0) {
        $address->addTotal(array(
            'code' => $this->getCode(),
            'title' => 'Surcharge',
            'value' => $amount
        ));
    }

    return $this;
}

}

Step 4 – Create Block app\code\community\CompanyName\Surcharge\Block\Sales\Order\Total.php

<?php

class CompanyName_Surcharge_Block_Sales_Order_Total extends Mage_Sales_Block_Order_Totals {

protected function _initTotals() {
    parent::_initTotals();
    $surcharge = $this->getSource()->getSurcharge();
    $baseSurcharge = $this->getSource()->getBaseSurcharge();

    if ($surcharge != 0) {
        $this->addTotal(new Varien_Object(array(
            'code' => 'surcharge',
            'value' => $surcharge,
            'base_value' => $baseSurcharge,
            'label' => Mage::helper('surcharge')->getLabel(),
                )), 'surcharge');
    }
    return $this;
}

}

Leave a comment

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