Organizational Research By

Surprising Reserch Topic

magento paypal tax rounding issue


magento paypal tax rounding issue  using -'magento,paypal,rounding'

I have some rounding issues with Paypal and Magento 1.7.0.2 - All prices on the site include tax and the tax is worked out at 20% (VAT).

I will go to checkout and everything is correct:



I will then click on place order, and Paypal will be like this, which is incorrect because the grand total is now 1p less. This appears to be cause by how the tax is rounded.



In some cases, it works ok, but in others the tax is rounded incorrectly. I have tried making changes to the tax calculation method calcTaxAmount() in app/code/core/Mage/Tax/Model/Calculation.php

I added this to the calcTaxAmount method which seemed to fix it, but it cause the prices on the product page to then be incorrect (1p less).

$amount = $this->roundUp($amount);

I'm pretty certain this is a bug, but I'm out of ideas. If anyone has come across this before and has a solution I'd be happy to hear it. Any help much appreciated.

EDIT: Here are my tax settings in Magento


    

asked Oct 11, 2015 by badhwar.rohit
0 votes
31 views



Related Hot Questions

5 Answers

0 votes

I think I've found the solution to this issue plaguing the community.

If your prices include tax, the tax calculation is wrong.

Here's the fix - In Mage_Tax_Model_Calculation::calcTaxAmount():

change the condition:

if ($priceIncludeTax)...

to:

if ( ! $priceIncludeTax ) ...

So the condition looks like:

if ( ! $priceIncludeTax ) { 
    $amount = $price*(1-1/(1+$taxRate)); 
} else { 
    $amount = $price*$taxRate; 
}

For details, check out my comments: http://www.magentocommerce.com/boards/viewthread/247201/P45/

Remember not to modify the core files - create a copy in local

answered Oct 11, 2015 by girisha
0 votes

This problem has been plaguing me (and by looks of it the magento community for ages), thanks to ShopWorks push in the right direction (inclusive of his code snippet, thanks mate! However it would bug out if going back to the cart from express checkout, added a check in to prevent this.) of the $request parameters I came up with the following fix (/hack):

On line 606 of Nvp.php place the following:

$totalValue = $request['TAXAMT'] + $request['ITEMAMT'];
$finalValue = $totalValue - $request['AMT'];

if($request['SHIPPINGAMT'] > 0) {
    $request['SHIPPINGAMT'] = ($request['AMT'] - ($request['TAXAMT'] + $request['ITEMAMT']));
    $totalValue = $request['TAXAMT'] + $request['ITEMAMT'] + $request['SHIPPINGAMT'];
    $finalValue = $totalValue - $request['AMT'];
}

if($request['AMT'] != $totalValue) {
    if($totalValue > $request['AMT']) {
        $request['TAXAMT'] = $request['TAXAMT'] - $finalValue;
    } elseif($totalValue < $request['AMT']) {
        $request['TAXAMT'] = $request['TAXAMT'] + $finalValue;
    } else {
        $request['AMT'] = $request['TAXAMT'] + $request['ITEMAMT'];
    }
}

Additionally, the following needs to be also placed within the call() function (line 938 of Nvp.php):

$totalValue = $request['TAXAMT'] + $request['ITEMAMT'] + $request['SHIPPINGAMT'];
$finalValue = $totalValue - $request['AMT'];

if($request['AMT'] != $totalValue) {
    if($totalValue > $request['AMT']) {
        if($finalValue > 0) {
            // its preferable that we change the tax amount over the grand total amount
            $request['TAXAMT'] = $request['TAXAMT'] - $finalValue;
        } else {
            $request['AMT'] = $totalValue;
        }
    } elseif($totalValue < $request['AMT']) {
        if($finalValue > 0) {
            // its preferable that we change the tax amount over the grand total amount
            $request['TAXAMT'] = $request['TAXAMT'] + $finalValue;
        } else {
            $request['AMT'] = $totalValue;
        }
    } else {
        $request['AMT'] = $totalValue;
    }
}

This is a hack, and treat it as such. My colleague is currently testing it but seems to be OK for the moment, it is also helpful to set the tax calculation method by unit price (our accountants are happy with this arrangement, but this is for the UK, i'm not sure if other countries will frown upon that particular tax calculation method).

The reason I am manipulating the $request['AMT'] is because occasionally the calculation of the $finalValue variable would produce a -0.9999 repeating integer which is of no use to anyone, my maths sucks so if anyone wants to improve upon this, please do so!

As always don't overwrite the nvp.php in the core directory, create a seperate rewrite module or do this in app/local/mage. First option preferably! :-)

answered Oct 11, 2015 by nikhilapatil
0 votes

I "fixed" this today for i client but not really happy with the solution. But it works.

It is better if you copy this file to your local folder: app/code/core/Mage/Paypal/Model/Api/Nvp.php

I added this code (Only for the express checkout) on line 606 so it look like this.

$request['SHIPPINGAMT'] = ($request['AMT'] - ($request['TAXAMT'] + $request['ITEMAMT']));

$response = $this->call(self::SET_EXPRESS_CHECKOUT, $request);
$this->_importFromResponse($this->_setExpressCheckoutResponse, $response);

And you need to turn of Transfer Cart Line Items in the paypal moule in the backend

If somebody knows a better solution then just overwriting the shippingcost let me know

answered Oct 11, 2015 by jekbishnoi
0 votes

There is a "bug" on Paypal in magento module (at least on my Magento 1.8.0); It resides in the Mage_Paypal_Model_Cart class.

To validate that the amounts are correct, the method _validate() on line 381 sums up all items prices from the order, adds shipping fees and taxes, and compares the result with the total value of the order (got from order method getBaseGrandTotal())

But sometimes, there is a 0.009999999999999999 difference between the amounts (it must come from different roundings methods, I don't know); so the items are not valid and the method getItems() from line 146 returns false.

In my case this resulted in customers paying a different amount and a "fraud suspicion" flag on their orders.

I fixed it by changing the comparison method (line 404) from :

if (sprintf('%.4F', $sum) == sprintf('%.4F', $referenceAmount)) {
    $this->_areItemsValid = true;
}

to

$diff = abs(sprintf('%.4F', $sum) - sprintf('%.4F', $referenceAmount));

if ($diff < 0.01) {
     $this->_areItemsValid = true;
}

I still hope that there will not be diffs of more than 0.009999999 in the future...

Hope this helps.

answered Oct 11, 2015 by deepak
0 votes

i just changed transfer cart line items to "NO", without the above code change.. and it worked.

Magento 1.9.0.1

just FYI - test if it works for you.

Dmytro

answered Oct 11, 2015 by shegokar.anjeet

...