发送Paypal付款电话返回400格式错误的请求错误

时间:2013-10-06 22:32:14

标签: php json curl paypal

我正在尝试使用php创建paypal付款。我一直收到400错误的请求错误。我相信我的问题是使用以下字符串:

$postData ='
{
    "intent": "sale"
    "redirect_urls": {
        "return_url": ' . $url_success .',
        "cancel_url": ' . $url_cancel .'
    },
    "payer": {
        "payment_method": "paypal"
    },
    "transactions": [
        {
            "amount": {
                "total": "' . $saleTotal .'",
                "currency": "USD"
            },
            "description": "Test payment."
        }
    ]
}
';

然后我在下一行使用cURL发送字符串

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

这是打印出的字符串:

{ "intent": "sale" "redirect_urls": { "return_url": 
"http://localhost/~user/test/controller/PayPalTestView.php", "cancel_url": 
"http://localhost/~user/test/controller/CancelTestView.php" }, "payer": { 
"payment_method": "paypal" }, "transactions": [ { "amount": { "total": "8.31782", 
"currency": "USD" }, "description": "Test payment." } ] } 

以下是我得到的回复:

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well 
formed.","information_link":
"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST",
"debug_id":"7d56ae815e584"}

我很确定问题出在$ postData中,虽然我不知道出了什么问题。我尝试过这个例子:Paypal REST API Bad Request,但它仍然无效。我的问题类似于该示例,因为我正在成功发送和接收身份验证令牌。我的问题是在尝试发送付款API调用时。有谁看到我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

之前我遇到了同样的问题。我所做的是将数据放在数组中:

$data = array(  "intent"=>"sale", 
            "redirect_urls"=>array(
                "return_url"=>"<return site>", 
                "cancel_url"=>"<cancel site>"
            ),
            "payer"=>array(
                "payment_method"=>"paypal"
            ),
            "transactions"=>array(
                array(
                    "amount"=>array(
                        "total"=>"7.47",
                        "currency"=>"USD"
                    ),
                    "description"=>"This is the payment transaction description."
                )
            )
        );

和http标题:

$header = array(
"Content-Type:application/json",
"Authorization:Bearer <token here>"

);

然后在postfield上,使用json_encode对数据进行编码:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

我希望这会有所帮助。

相关问题