0

我正在尝试使用 php 创建贝宝付款。我不断收到 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 调用时。有谁看到我做错了什么?谢谢!

4

1 回答 1

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));

我希望这将有所帮助。

于 2013-11-11T09:00:30.410 回答