Magento Forum

   
Page 1 of 2
Paypal Express - Rounding issue causes NVP gateway error
 
barnyshergold
Member
 
Avatar
Total Posts:  53
Joined:  2011-02-05
 

We are using magento 1.4.1.1

We just got a spate of errors in the log : PayPal NVP gateway errors: The totals of the cart item amounts do not match order amounts

This occurs on our mobile site and we are using the Magento Express gateway. This problem doesn’t seem to occur on the main site which uses Paypal Standard.

It seems to be because of rounding - especially when we have a special deal and the customer gets a discount. I managed to log the REQUEST and here is an example snippet:

[L_NUMBER0] => PRODUCT
[L_NAME0] => Product x 4
[L_QTY0] => 1
[L_AMT0] => 99.83
[L_NUMBER1] =>
[L_NAME1] => Discount
[L_QTY1] => 1
[L_AMT1] => -49.92
[ITEMAMT] => 49.92
[SHIPPINGAMT] => 3.25
[TAXAMT] => 9.98

ITEMAMT should total all the L_AMT values. However 99.83 + (-49.92) = 49.91

I got the code to do the values to 4DP :

[L_QTY0] => 1
[L_AMT0] => 99.8332
[L_NUMBER1] =>
[L_NAME1] => Discount
[L_QTY1] => 1
[L_AMT1] => -49.9166
[ITEMAMT] => 49.9166
[SHIPPINGAMT] => 3.2500
[TAXAMT] => 9.9833

The problem is caused buy removing the VAT and then doing the sums. As UK VAT is 20% it makes for some horrible EXVAT prices.
In this case the full price is £29.95, so EXVAT = £21.9583!

Has anyone else had this problem and solved it? I ask as I’d rather not spend several hours working out a solution if someone else
has done it!

Barny

 
Magento Community Magento Community
Magento Community
Magento Community
 
chrisandy
Sr. Member
 
Total Posts:  92
Joined:  2007-10-17
 

Would love to know if anyone fixed this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
barnyshergold
Member
 
Avatar
Total Posts:  53
Joined:  2011-02-05
 

Hi,

I meant to post this last week.

In Mage\PayPal\Model\Api\Nvp.php (make a local copy!!)

Locate the method callSetExpressCheckout.

Immediately before the line

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

Insert this code :

// Check discount
        $lineAmt 0;
        
$discount 0;
        
$line 0;
        
$discountIndex 0;
        while(isset(
$request["L_AMT{$line}"])) {
            
if($request["L_NAME{$line}"== 'Discount'{
                $discount 
+= $request["L_AMT{$line}"];
                
$discountIndex $line;
            
}
            
else {
                $lineAmt 
+= $request["L_AMT{$line}"];
            
}
            $line
++;
        
}
        
        
if($discount{
            
if($discount $lineAmt != $request['ITEMAMT']{
                
// Do correction
                
$request["L_AMT{$discountIndex}"= ($request['ITEMAMT'$lineAmt);
            
}    
        } 
else {
            
// Check item amount adds up
            
$correctItemAmt $request['AMT'$request['SHIPPINGAMT'$request['TAXAMT'];
            if(
$correctItemAmt != $request['ITEMAMT']{
                $request[
'ITEMAMT'$correctItemAmt
            
}
        }

So far this has solved the problem .

DISCLAIMER : CODE ABOVE IS SUPPLIED WITHOUT ANY GUARANTEES OR WARRANTIES. YOU SHOULD FULL CHECK IT WORKS IN YOUR SYSTEM BEFORE USING ON A LIVE PLATFORM.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

Going to give this a try in 1.7.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

It kind of works… except it sends over the pre-tax amounts.

But it does get me to Paypal, which is the perfect start smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

Attached is the fixed that has worked for me with Tax Settings

Prices Include Tax
Discount After Tax HAs Been Applied
Shipping Prices Exclude Tax

Fixes when a percentage discount is applied to an item or items in the basket.

Enjoy

Can’t take the credit on this one, found the code on a Stakeoverflow page

File Attachments
Paypal Fix.zip  (File Size: 12KB - Downloads: 340)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

Hi elfing…

Thanks for the fix.
If you don’t mind, could you post your detailed tax settings (i.e. row total/unit price etc) to go with this please? - I’m getting a PayPal gateway reject without discounts now too! :(

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
creativemgroup
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2010-01-12
UK & USA
 

Fix attached was from a 1.7x build but you can search through it and find the bits changed to update lower versions.

Updated Core/Store to use 4 decimal place rounding

Tax setting based on Total
Catalog includes Tax

Paypal Setting - Aggregated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

Hi,

Thanks for that. I am actually using the latest version 1.7.2

EDIT: I’d changed another file which must have been having an impact on it. It’s now as per your setup and goes to PayPal no problems.
Awesome job - This is what the community’s about.

Thanks Elfling and Creative Media Group (I think you’re the same person anyway rasberry)

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

No probs. Yeah, I’d logged into the wrong account smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

How much have you stress tested this fix? Hopefully this cures it for good but I just have a feeling that somewhere down the line, there’ll be another 1 penny out....My prices are all relatively small, i.e. £2.99/3.50/5.20 and a typical basket contains over 30 items so I fear that the higher the basket amount and the more the contents, the more prone the calculation is to spit an error.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

I’m finding no errors with actually going to Paypal. So you don’t get a rejected gateway error anymore due to miss match amount.

However I am finding that occasionally that amount sent over to Paypal is actually 1p less than the amount Magento believes it should have. I believe this is down to my 4 DP rounding.

Looking into a fix for that though.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

Yeah same for me. Either 1p up or down.

Strange really - I have a feeling it might be also down to the fact that a lot of my prices have odd pence amounts (i.e. 5.99, 3.71, 2.95 etc) and then by virtue of the way these are added, this 1p gets deducted or added. I wonder if rounding to 3 places would make this better?

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  899
Joined:  2008-10-21
 

Are you currently also rounding to 4 dp?

It’s giving me a royal headache :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spez86
Jr. Member
 
Total Posts:  22
Joined:  2010-02-14
 

Yup, 4 decimal places. The only other method I found that worked properly within Magento, but not with Paypal Express Checkout (website payments standard works fine strangely enough), was to set to Unit Price, 4dp rounding, and two other modifications...one to payment.php and another to tax.php. But as I say, Paypal express checkout refuses no matter what when I have it setup this way.

 
Magento Community Magento Community
Magento Community
Magento Community
 
husby
Member
 
Avatar
Total Posts:  70
Joined:  2009-01-07
Roseville, MN
 

I’m having the same rounding issues.  Seems to be related to implementing a 10% shopping cart price rule.  I’ve tried the 4 decimal place rounding fix, along with the Nvp code specified earlier in this thread, but still have issues.  Has anyone figured this out yet?  Are there Mage bugs written for this yet?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2