0

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!

chaoskreator
  • 889
  • 1
  • 17
  • 39
  • Is your URL encoding correct? It doesn't look like your `$returnURL` or `$cancelURL` are URL encoded. – Josh Jan 18 '12 at 22:43
  • @Josh I did notice that there was something was wrong with those. Looks like the URI is doubling up for some reason. I thought you just needed to encode the values, and not the whole URL? – chaoskreator Jan 18 '12 at 22:55
  • All values for each NVP must be url encoded. Because your return URL, `http://...` is the value of `returnURL`, the name, then it must be encoded as well, not just portions of it. Does that make sense? – Josh Jan 18 '12 at 22:58
  • @Josh It does, actually. But would this cause Paypal to throw that particular error concerning the numeric totals? – chaoskreator Jan 18 '12 at 23:43
  • I'm not sure, but it would be safe to URL encode any values in the NVPs that aren't encoded, and see what happens. – Josh Jan 18 '12 at 23:46
  • @Josh Well, I changed everything to use urlencoding, but the problem still persists. Thanks for pointing that out, though – chaoskreator Jan 19 '12 at 00:48
  • @Josh The urlencoding turned out to actually be the problem for me when I had a similar issue, so thanks for pointing that out. The non urlencoded variables were causing the payment amounts and quanities to be incorrectly interpreted. – Tim Seguine Oct 09 '12 at 10:40
  • @Tim Great to hear that you figured it out! – Josh Oct 09 '12 at 13:48

1 Answers1

2

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

Mark
  • 36
  • 3
  • That does seem to be the issue, as it has cleared up that particular error message. However, now I'm getting an `Internal Error: Timeout Processing Request` – chaoskreator Jan 20 '12 at 02:58
  • @chaosKreator: PayPal sometimes timesout when there is an error in the authentication data - I got stuck once with an extra space on the end after copying and pasting from the sandbox – Mark Jan 20 '12 at 06:45
  • yes, i've read about that. I double checked my info several times and it all appears correct. Not sure what is going on. And I can't seem to get an answer on X.com. :/ – chaoskreator Jan 20 '12 at 18:58