Question

I have used this PayPal/Payment Fee extension to add a charge for a payment method.

enter image description here

The problem is that tax (21%) is not calculated for Payment Charge. The plugin does not support it. Could you give me some hints how could I extend the plugin so that the tax is calculated for the payment charge and included in the Tax row (can be seen in the image).

EDIT: I believe, this is the class where I need to modify the collect method:

class FlyWebdesign_PaypalFee_Model_Sales_Quote_Address_Total_Paymentcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function __construct()
    {
        $this->setCode('payment_charge');
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        $address->setPaymentCharge(0);
        $address->setBasePaymentCharge(0);

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

        $paymentMethod = $address->getQuote()->getPayment()->getMethod();
        if ($paymentMethod) { 
            $amount = Mage::helper('paymentcharge')->getPaymentCharge($paymentMethod, $address->getQuote());
            $address->setPaymentCharge($amount);
            $address->setBasePaymentCharge($amount);
        }

        $address->setGrandTotal($address->getGrandTotal() + $address->getPaymentCharge());
        $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBasePaymentCharge());
        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        $amount = $address->getPaymentCharge();
        if (($amount!=0)) {
            $address->addTotal(array(
                'code' => $this->getCode(),
                'title' => Mage::helper('sales')->__('Payment Charge'),
                'full_info' => array(),
                'value' => $amount
            ));
        }
        return $this;
    }
}

Probably also these classes needs to be customized:

FlyWebdesign_PaypalFee_Model_Sales_Order_Creditmemo_Total_Paymentcharge
FlyWebdesign_PaypalFee_Model_Sales_Order_Invoice_Total_Paymentcharge
Was it helpful?

Solution

Allright, this is what I have got

I have modified FlyWebdesign_PaypalFee_Model_Sales_Quote_Address_Total_Paymentcharge like this (see the four comments marked clime):

<?php

/**
 * @category   FlyWebdesign
 * @package    FlyWebdesign_PaypalFee
 */
class FlyWebdesign_PaypalFee_Model_Sales_Quote_Address_Total_Paymentcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    protected $_calculator = null;

    public function __construct()
    {
        $this->setCode('payment_charge');
        $this->_calculator  = Mage::getSingleton('tax/calculation');
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        $address->setPaymentCharge(0);
        $address->setBasePaymentCharge(0);

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

        $paymentMethod = $address->getQuote()->getPayment()->getMethod();
        if ($paymentMethod) { 
            $amount = Mage::helper('paymentcharge')->getPaymentCharge($paymentMethod, $address->getQuote());
            $address->setPaymentCharge($amount);
            $address->setBasePaymentCharge($amount);

            /* clime: tax calculation start */
            $calc               = $this->_calculator;
            $store              = $address->getQuote()->getStore();
            $addressTaxRequest  = $calc->getRateRequest(
                $address->getQuote()->getShippingAddress(),
                $address->getQuote()->getBillingAddress(),
                $address->getQuote()->getCustomerTaxClassId(),
                $store
            );

            $paymentTaxClass = Mage::getStoreConfig('payment/'.$paymentMethod.'/payment_tax_class');
            $addressTaxRequest->setProductClassId($paymentTaxClass);

            $rate          = $calc->getRate($addressTaxRequest);
            $taxAmount     = $calc->calcTaxAmount($amount, $rate, false, true);
            $baseTaxAmount = $calc->calcTaxAmount($amount, $rate, false, true);

            $address->setPaymentTaxAmount($taxAmount);
            $address->setBasePaymentTaxAmount($baseTaxAmount);

            $address->setTaxAmount($address->getTaxAmount() + $taxAmount);
            $address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseTaxAmount);
            /* clime: tax calculation end */
        }
        //clime: tax added
        $address->setGrandTotal($address->getGrandTotal() + $address->getPaymentCharge() + $address->getPaymentTaxAmount());
        //clime: tax added
        $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBasePaymentCharge() + $address->getBasePaymentTaxAmount()); 

        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        $amount = $address->getPaymentCharge();
        if (($amount!=0)) {
            $address->addTotal(array(
                'code' => $this->getCode(),
                'title' => Mage::helper('sales')->__('Payment Charge'),
                'full_info' => array(),
                'value' => $amount
            ));
        }
        return $this;
    }
}

I have modified plugin's system.xml to add admin field Tax class for the payment fee. Next, I haven't actually changed the plugin's classes Creditmemo_Total_Paymentcharge and Invoice_Total_Paymentcharge as I had in mind previously, but instead I have add the following rewrites in plugin's config.xml (in models section):

        <sales>
            <rewrite>
                <order_invoice_total_tax>FlyWebdesign_PaypalFee_Model_Sales_Order_Invoice_Total_Tax</order_invoice_total_tax>
                <order_creditmemo_total_tax>FlyWebdesign_PaypalFee_Model_Sales_Order_Creditmemo_Total_Tax</order_creditmemo_total_tax>
            </rewrite>
        </sales>

In these two new files (copies of the original Magento source files - one of them being: /app/code/core/Mage/Sales/Model/Order/Invoice/Total/Tax.php), I have added the following into collect method of order_invoice_total_tax:

    if ($this->_canIncludePaymentTax($invoice)) {
        $totalTax           += $order->getPaymentTaxAmount();
        $baseTotalTax       += $order->getBasePaymentTaxAmount();
        $invoice->setPaymentTaxAmount($order->getPaymentTaxAmount());
        $invoice->setBasePaymentTaxAmount($order->getBasePaymentTaxAmount());
    }

And I also added this method to the class.

protected function _canIncludePaymentTax($invoice)
{
    $includePaymentTax = true;
    /**
     * Check payment amount in previous invoices
     */
    foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
        if ($previusInvoice->getPaymentAmount() && !$previusInvoice->isCanceled()) {
            $includePaymentTax = false;
        }
    }
    return $includePaymentTax;
}

I have added this into collect method of the other class order_creditmemo_total_tax:

if ($invoice = $creditmemo->getInvoice()) {
    $paymentTaxAmount     = $invoice->getPaymentTaxAmount();
    $basePaymentTaxAmount = $invoice->getBasePaymentTaxAmount();
    $totalTax            += $paymentTaxAmount;
    $baseTotalTax        += $basePaymentTaxAmount;
} else {
    $paymentTaxAmount     = $order->getPaymentTaxAmount();
    $basePaymentTaxAmount = $order->getBasePaymentTaxAmount();
    $totalTax            += $paymentTaxAmount;
    $baseTotalTax        += $basePaymentTaxAmount;
}
$creditmemo->setPaymentTaxAmount($paymentTaxAmount);
$creditmemo->setBasePaymentTaxAmount($basePaymentTaxAmount);

I also needed to make the following sql upgrade script for the plugin

<?php
/* @var $installer Mage_Customer_Model_Entity_Setup */

$installer = $this;
$installer->startSetup();

$installer->addAttribute('quote_address', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('quote_address', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('order', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('order', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('invoice', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('invoice', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('creditmemo', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('creditmemo', 'base_payment_tax_amount', array('type' => 'decimal'));

$installer->endSetup();

And I added this into plugin's config.xml - section <fieldsets><sales_convert_quote_address>

        <payment_tax_amount><to_order>*</to_order></payment_tax_amount>
        <base_payment_tax_amount><to_order>*</to_order></base_payment_tax_amount>

As far as my testing goes, this works for me. However there are still some unsolved (potential) troubles that I would like to solve.

  • potential problem with specified payment amount if base currency is not store currency (not sure how magento will behave in such a case)

  • amount specified in admin is always considered to exclude tax (there is no option to change it)

  • tax for payment fee does not occur in full tax summary (if enabled) as a row on its own

Btw my Magento version is 1.7.0.2

OTHER TIPS

I found my solution, for a similar dev.

There is a

    $this->_addAmount($address->getExtraTaxAmount());
    $this->_addBaseAmount($address->getBaseExtraTaxAmount());

in Mage_Tax_Model_Sales_Total_Quote_Tax

so just call

    $address->setExtraTaxAmount($address->getExtraTaxAmount() + $taxFeeAmount);
    $address->setBaseExtraTaxAmount($address->getBaseExtraTaxAmount() + $taxBaseFeeAmount);

in your custom Totals

Hm, It would be perhaps better to search for workarounds.

  1. You can modifiy magento shopping cart price rules to add fees insted of discounts http://php.quicoto.com/extra-fee-shopping-cart-price-rules-magento/

  2. Then you can add a rule saying: if the payment method is , add fee to the shopping cart. That fee will be dsiplayed same as your payment charge. You can translate that field.

In that way the tax will be calculated for fee too.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top