Paypal setExpressCheckout nvp错误

时间:2012-08-15 14:46:38

标签: php curl paypal paypal-sandbox

我有一些代码为Paypal构建请求URL,但在某些项目上它返回以下错误(在响应转换为数组后):

Array
(
    [TIMESTAMP] => 2012-08-15T14:23:10Z
    [CORRELATIONID] => XXXXXXXXXXXXXXXXX
    [ACK] => Failure
    [VERSION] => 87.0
    [BUILD] => 3435050
    [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
)

这是我通过cURL发送的网址:

https://api.sandbox.paypal.com/nvp?
METHOD=setExpressCheckout
&VERSION=87.0
&USER=test_user.mysite.co.uk
&PWD=1340704478&SIGNATURE=AxhCd62FkHm-KuS.WvqjUnQ.l8WyAuePJQjoYT0C5lGiwjpcVIJHURjI
&RETURNURL=http://www.mysite.co.uk/checkout/order/confirm
&CANCELURL=http://www.mysite.co.uk/checkout/order/error
&L_PAYMENTREQUEST_0_NAME0=Likit+Snaks+&L_PAYMENTREQUEST_0_NUMBER0=LIKIT-SNAKS
&L_PAYMENTREQUEST_0_DESC0=Flavour%3A+Apple+&+Cinnamon
&L_PAYMENTREQUEST_0_AMT0=2.08
&L_PAYMENTREQUEST_0_QTY0=1
&PAYMENTREQUEST_0_CURRENCYCODE=GBP
&PAYMENTREQUEST_0_ITEMAMT=2.08
&PAYMENTREQUEST_0_SHIPPINGAMT=3.65
&PAYMENTREQUEST_0_TAXAMT=0.52
&PAYMENTREQUEST_0_AMT=6.25

但不是总数加起来了吗?

如果有帮助,这里是构建网址的php:

private function buildRequestItemsURL()
{

    $this->db->select("bi_id, prod_id, bi_psc_id, prod_title, prod_sku, bi_price, bi_qty, basket_delivery");
    $query = $this->db->get_where("basket_views", array("basket_session" => session_id()));

    $i = 0;
    $total = 0;
    $url = "";

    foreach($query->result() as $row)
    {

        $row->options = "";

        if($i==0)
        {

            if(number_format($row->basket_delivery, 2) == 0.00)
            {

                header("location: http://www.mysite.co.uk/checkout/basket");

            }
            else
            {

                $delivery = $row->basket_delivery;

            }

        }

        $this->db->select("opt_1_name AS Colour, opt_2_name AS Size, opt_3_name AS Flavour, opt_4_name AS Material, opt_5_name AS Style");
        $this->db->where("psc_id", $row->bi_psc_id);
        $query = $this->db->get("product_stock_view bio", 1);

        foreach($query->first_row() as $key => $value)
        {


                $row->options .= (!is_null($value)) ? "{$key}: {$value}, " : "";                

        }
        $row->bi_price = number_format($row->bi_price * 0.8, 2);
        $row->options = substr($row->options, 0, -2);
        $url .= "&L_PAYMENTREQUEST_0_NAME{$i}={$row->prod_title}&L_PAYMENTREQUEST_0_NUMBER{$i}={$row->prod_sku}&L_PAYMENTREQUEST_0_DESC{$i}={$row->options}&L_PAYMENTREQUEST_0_AMT{$i}={$row->bi_price}&L_PAYMENTREQUEST_0_QTY{$i}={$row->bi_qty}";

        $total += (($row->bi_price)*$row->bi_qty);
        ++$i;

    }

    $itemTotal = $total;
    $total += $delivery;
    $total += ($itemTotal * 0.25);

    $total = number_format($total, 2);
    $itemTotal = number_format($itemTotal, 2);
    $vat = $total - $itemTotal - $delivery;
    $vat = number_format($vat, 2);

    $url .= "&PAYMENTREQUEST_0_CURRENCYCODE=GBP&PAYMENTREQUEST_0_ITEMAMT={$itemTotal}&PAYMENTREQUEST_0_SHIPPINGAMT={$delivery}&PAYMENTREQUEST_0_TAXAMT={$vat}&PAYMENTREQUEST_0_AMT=".$total;

    $url = urlencode($url);
    $url = str_replace("%3F", "?", $url);
    $url = str_replace("%26", "&", $url);
    $url = str_replace("%3D", "=", $url);

    return $url;

}

$url = "?METHOD=setExpressCheckout&VERSION=87.0&USER=user.mysite.co.uk&PWD=1340704478&SIGNATURE=AxhCd62FkHm-KuS.WvqjUnQ.l8WyAuePJQjoYT0C5lGiwjpcVIJHURjI&RETURNURL=http://www.mysite.co.uk/checkout/order/confirm&CANCELURL=http://www.mysite.co.uk/checkout/order/error";      
$url .= $this->buildRequestItemsURL();
$url = "https://api.sandbox.paypal.com/nvp".$url;

1 个答案:

答案 0 :(得分:2)

问题在于&在产品说明中。我更改了代码,以便它将产品属性分别编码为$ url字符串,从而删除&amp ;.这是新的buildRequestItemsURL函数代码:

private function buildRequestItemsURL()
{

    $this->db->select("bi_id, prod_id, bi_psc_id, prod_title, prod_sku, bi_price, bi_qty, basket_delivery");
    $query = $this->db->get_where("basket_views", array("basket_session" => session_id()));

    foreach($query->result() as $row)
    {

        $row->prod_title = urlencode($row->prod_title);
        $row->prod_sku = urlencode($row->prod_sku);

    }

    $i = 0;
    $total = 0;
    $url = "";

    foreach($query->result() as $row)
    {

        $row->options = "";

        if($i==0)
        {

            if(number_format($row->basket_delivery, 2) == 0.00)
            {

                header("location: http://www.crowstonstack.co.uk/checkout/basket");

            }
            else
            {

                $delivery = $row->basket_delivery;

            }

        }

        $this->db->select("opt_1_name AS Colour, opt_2_name AS Size, opt_3_name AS Flavour, opt_4_name AS Material, opt_5_name AS Style");
        $this->db->where("psc_id", $row->bi_psc_id);
        $query = $this->db->get("product_stock_view bio", 1);

        foreach($query->first_row() as $key => $value)
        {

                $row->options .= (!is_null($value)) ? urlencode("{$key}: {$value}, ") : "";             

        }
        $row->bi_price = number_format($row->bi_price * 0.8, 2);
        $row->options = substr($row->options, 0, -2);
        $row->prod_title = str_replace("&", "%26", $row->prod_title);
        $url .= "&L_PAYMENTREQUEST_0_NAME{$i}={$row->prod_title}&L_PAYMENTREQUEST_0_NUMBER{$i}={$row->prod_sku}&L_PAYMENTREQUEST_0_DESC{$i}={$row->options}&L_PAYMENTREQUEST_0_AMT{$i}={$row->bi_price}&L_PAYMENTREQUEST_0_QTY{$i}={$row->bi_qty}";

        $total += (($row->bi_price)*$row->bi_qty);
        ++$i;

    }

    $itemTotal = $total;
    $total += $delivery;
    $total += ($itemTotal * 0.25);

    $total = number_format($total, 2);
    $itemTotal = number_format($itemTotal, 2);
    $vat = $total - $itemTotal - $delivery;
    $vat = number_format($vat, 2);

    $url .= "&PAYMENTREQUEST_0_CURRENCYCODE=GBP&PAYMENTREQUEST_0_ITEMAMT={$itemTotal}&PAYMENTREQUEST_0_SHIPPINGAMT={$delivery}&PAYMENTREQUEST_0_TAXAMT={$vat}&PAYMENTREQUEST_0_AMT=".$total;

    return $url;

}