Paypal API: The totals of the cart item amounts do not match order amounts

五迷三道 提交于 2019-11-30 08:43:49
Robert

You're missing an ITEMAMT. See also my answer in Paypal Express checkout with shipping cost and error which contains a working example with multiple quantities.

&L_PAYMENTREQUEST_0_NAME0=10% Decaf Kona Blend Coffee  
&L_PAYMENTREQUEST_0_NUMBER0=623083  
&L_PAYMENTREQUEST_0_DESC0=Size: 8.8-oz 
&L_PAYMENTREQUEST_0_AMT0=9.95  
&L_PAYMENTREQUEST_0_QTY0=2  
&L_PAYMENTREQUEST_0_NAME1=Coffee Filter bags  
&L_PAYMENTREQUEST_0_NUMBER1=623084  
&L_PAYMENTREQUEST_0_DESC1=Size: Two 24-piece boxes  
&L_PAYMENTREQUEST_0_AMT1=39.70  
&L_PAYMENTREQUEST_0_QTY1=2  
&PAYMENTREQUEST_0_ITEMAMT=99.30  
&PAYMENTREQUEST_0_TAXAMT=2.58  
&PAYMENTREQUEST_0_SHIPPINGAMT=3.00  
&PAYMENTREQUEST_0_HANDLINGAMT=2.99  
&PAYMENTREQUEST_0_SHIPDISCAMT=-3.00  
&PAYMENTREQUEST_0_INSURANCEAMT=1.00  
&PAYMENTREQUEST_0_AMT=105.87  
&PAYMENTREQUEST_0_CURRENCYCODE=USD  

Not directly related to the question, but since this comes up first in Google when searching by error message this might be useful to other people who are receiving this error.

You might be getting this error if some of the fields are not set(null/blank).

For instance in my case it was(line breaks added for clarity):

    PAYMENTREQUEST_0_PAYMENTACTION=Sale
    &REQCONFIRMSHIPPING=0
    &RETURNURL=some_url
    &CANCELURL=some_other_url
    &BRANDNAME=Brand
    &L_PAYMENTREQUEST_0_NAME0=
    &L_PAYMENTREQUEST_0_AMT0=1.00
    &L_PAYMENTREQUEST_0_QTY0=1
    &PAYMENTREQUEST_0_AMT=1.00
    &PAYMENTREQUEST_0_CURRENCYCODE=USD

And it was giving me the same error, even though the amounts are fine. I think it is a parsing issue, because if you insert even a space after L_PAYMENTREQUEST_0_NAME0= in

&L_PAYMENTREQUEST_0_NAME0=&L_PAYMENTREQUEST_0_AMT0=1.00

It works fine.

So my guess is that at the PayPal's end it just assigns "&L_PAYMENTREQUEST_0_AMT0=1.00" to L_PAYMENTREQUEST_0_NAME0, thus effectively removing it from the message and consequently triggers this error, since without that line the amounts do not add up indeed.

I had the same trouble and resolved it by formating prices in foreach by function number_format for parameter L_PAYMENTREQUEST_0_AMT$k

Example:

$items_amount = 0;
foreach ($products as $k => $product) {
    $params["L_PAYMENTREQUEST_0_NAME$k"] = $product['name'];
    $params["L_PAYMENTREQUEST_0_DESC$k"] = $product['desc'];
    $params["L_PAYMENTREQUEST_0_AMT$k"] = number_format($product['priceIVA'], 2);
    $params["L_PAYMENTREQUEST_0_QTY$k"] = $product['count'];
    $items_amount += (floatval($product['priceIVA']) * $product['count']);
}

$params['PAYMENTREQUEST_0_AMT'] = $params['PAYMENTREQUEST_0_ITEMAMT'] = number_format($items_amount, 2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!