2013-10-06 57 views
0

我想創建一個使用PHP的貝寶付款。我不斷收到400錯誤的請求錯誤。我相信我的問題是與以下字符串:發送貝寶付款通話返回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_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調用。有沒有人看到我在做什麼錯了?謝謝!

+2

「'intent」後面缺少',':「sale」'。另外,你不應該手動創建json數組。在PHP中構建數組,然後使用['json_encode()'](http://php.net/manual/en/function.json-encode.php) – Sean

+0

這就實現了。我現在正在PHP中使用json_encode()構建數組。謝謝! – Jason247

回答

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

我希望這將有助於。