Paypal PHP NVP API - The totals of the cart item amounts do not match order amounts

1.5k views Asked by At

I am having an issue with the totals on my custom paypal shopping cart. Using the PHP NVP API I get:

TIMESTAMP: 2012-01-18T07:27:10Z
CORRELATIONID: d1fac4fa784ef
ACK: Failure
VERSION: 84.0
BUILD: 2271164
L_ERRORCODE0: 10413
L_SHORTMESSAGE0: Transaction refused because of an invalid argument. See additional error messages for details.
L_LONGMESSAGE0: The totals of the cart item amounts do not match order amounts.
L_SEVERITYCODE0: Error

However, I know my totals should be correct. Here is the PHP:

$count = sizeof($cartItem);
$subtotal = 0;
$itmStr = '';
foreach($cartItem as $k = $ci)
{
  $sub = $ci['cart_amt']*$ci['cart_price'];
  $subtotal = $sub + $subtotal;

  $itmStr .= '&L_PAYMENTREQUEST_'.$k.'_NUMBER0='.$ci['cart_id'].'&L_PAYMENTREQUEST_'.$k.'_NAME0='.$ci['cart_item'].'&L_PAYMENTREQUEST_'.$k.'_AMT0='.$ci['cart_price'].'&L_PAYMENTREQUEST_'.$k.'_QTY0='.$ci['cart_amt'].'&L_PAYMENTREQUEST_'.$k.'_DESC0='.truncate($ci['cart_desc'], 25);
}

$subtotal = (float) $subtotal; 
if(round($subtotal) < 25)
{
  $ups_rate = (float) 9.95; 
  $usps_rate = (float) 7.50; 
} 

$ups_rate = number_format($ups_rate, 2, '.', ''); 
$usps_rate = number_format($usps_rate, 2, '.', ''); 
$_SESSION['ups_rate'] = $ups_rate; 
$_SESSION['usps_rate'] = $usps_rate; 
$ups_tot = $ups_rate + $subtotal; 
$usps_tot = $usps_rate + $subtotal; 
$_SESSION['ups_tot'] = $ups_tot; 
$_SESSION['usps_tot'] = $usps_tot; 
$_SESSION['subtotal'] = $subtotal; 
$base_total = $subtotal+$usps_rate; 
$handling = number_format((float) ($base_total*0.029) + .30, 2, '.', ''); 
$total = number_format($handling+$base_total, 2, '.', ''); 

$returnURL = append_sid("{$url}cart.php", 'mode=paypal_transaction&currencyCodeType='.urlencode($currencyCodeType).'&paymentType='.urlencode($paymentType)); 
$cancelURL = append_sid("{$url}cart.php", 'paymentType='.urlencode($paymentType)); 

$nvpstr = ""; 
$shiptoAddress = "&PAYMENTREQUEST_0_SHIPTONAME=$personName&PAYMENTREQUEST_0_SHIPTOSTREET=$SHIPTOSTREET&PAYMENTREQUEST_0_SHIPTOCITY=$SHIPTOCITY&PAYMENTREQUEST_0_SHIPTOSTATE=$SHIPTOSTATE&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=$SHIPTOCOUNTRYCODE&PAYMENTREQUEST_0_SHIPTOZIP=$SHIPTOZIP"; 

$nvpstr = '&ADDRESSOVERRIDE=1'.$shiptoAddress.'&MAXAMT='.(string)$total."&PAYMENTREQUEST_0_AMT=".(string)$total."&PAYMENTREQUEST_0_ITEMAMT=".(string)$subtotal.'&CALLBACKTIMEOUT=4&PAYMENTRREQUEST_0_HANDLINGAMT='.$handling.'&PAYMENTREQUEST_0_SHIPPINGAMT='.$usps_rate.'&ReturnUrl='.$returnURL.'&CANCELURL='.$cancelURL.'&PAYMENTINFO_0_CURRENCYCODE='.$currencyCodeType.'&PAYMENTREQUEST_0_PAYMENTACTION='.$paymentType.$itmStr; 

Using the code above, I call SetExpressCheckout with the following parameters:

&ADDRESSOVERRIDE=1&PAYMENTREQUEST_0_SHIPTONAME=test user&PAYMENTREQUEST_0_SHIPTOSTREET=123 somewhere st&PAYMENTREQUEST_0_SHIPTOCITY=mytown&PAYMENTREQUEST_0_SHIPTOSTATE=NC&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=US&PAYMENTREQUEST_0_SHIPTOZIP=12345&MAXAMT=64.71&PAYMENTREQUEST_0_AMT=64.71&PAYMENTREQUEST_0_ITEMAMT=50.64&CALLBACKTIMEOUT=4&PAYMENTRREQUEST_0_HANDLINGAMT=2.12&PAYMENTREQUEST_0_SHIPPINGAMT=11.95&ReturnUrl=http://xxxx/cart.php?mode=paypal_transaction&sid=XXX&CANCELURL=http://therealmsofwickedry.com/cart.php?mode=paypal_transaction&sid=XXX&PAYMENTINFO_0_CURRENCYCODE=USD&PAYMENTREQUEST_0_PAYMENTACTION=Sale&L_PAYMENTREQUEST_0_NUMBER0=3320&L_PAYMENTREQUEST_0_NAME0=ITem number 1&L_PAYMENTREQUEST_0_AMT0=47.32&L_PAYMENTREQUEST_0_QTY0=1&L_PAYMENTREQUEST_0_DESC0=No description available&L_PAYMENTREQUEST_1_NUMBER0=4605&L_PAYMENTREQUEST_1_NAME0=Item number 2&L_PAYMENTREQUEST_1_AMT0=3.32&L_PAYMENTREQUEST_1_QTY0=1&L_PAYMENTREQUEST_1_DESC0=No description available

Am I not passing a parameter? Have I encoded them incorrectly somehow? Any pointers would be greatly appreciated!

1

There are 1 answers

3
Mark On BEST ANSWER

You are repeating the same list item each time. For example, where you have 'NUMBER0', you should have 'NUMBER'.$c.'='...etc (where $c starts at 0 an increments each time thru the loop) The same holds for each of your AMT0, QTY0, and so on. As it is PayPal will only add one item to get its total, so you get a mathematical error if you have more than one in your total. I'm pretty new to this, so apologies if I've misunderstood