2014-10-10 62 views
0

我想這是一個兩部分問題,所以基本上我正在使用PayPal的REST API,並且我想要設置付款。我創建了這個數組,我使用了json_encode,並將其轉換爲下面的json代碼,所以基本上運輸地址對象有問題,因爲一旦我刪除它,一切都可以順利進行。當我通過the documentation查找時,我無法真正發現問題,我已經將它放入item_list對象中,就像它應該填寫所有必填字段,以便我猜測可能是錯誤的語法PayPal REST API發貨地址不適用於cURL請求

$json_object = array(
    "intent" => "sale", 
    "redirect_urls" => array(
     "return_url" => "localhost/oauth2/src/OAuth2/success.php", 
     "cancel_url" => "localhost" 
    ), 
    "payer" => array(
     "payment_method" => "paypal" 
    ), 
    "transactions" => array(
      0 => array(
      "amount" => array(
       "total" => "12.00", 
       "currency" => "USD" 
      ), 
      "description" => "payment description", 
      "item_list" => array(
       "items" => array(
        0 => array(
         "quantity" => "1", 
         "name" => "jacket", 
         "price" => "12.00", 
         "sku" => "dasd", 
         "currency" => "USD", 
         "description" => "blue" 
        ) 
       ), 
        "shipping_address" => array(
        "recipient_name" => "John Johnson", 
        "line1" => "Whatever street 2", 
        "line2" => "Another street 2", 
        "city" => "London", 
        "country_code" => "UK", 
        "postal_code" => "NR30 1LY", 
        "state" => "England", 
        "phone" => "3123123123" 
       ) 
      ) 
     ) 
    ) 
); 

編碼到

{ 
    "intent": "sale", 
    "redirect_urls": { 
    "return_url": "localhost/oauth2/src/OAuth2/success.php", 
    "cancel_url": "localhost" 
    }, 
    "payer": { 
    "payment_method": "paypal" 
    }, 
    "transactions": [ 
    { 
     "amount": { 
     "total": "12.00", 
     "currency": "USD" 
     }, 
     "description": "payment description", 
     "item_list": { 
     "items": [ 
      { 
      "quantity": "1", 
      "name": "jacket", 
      "price": "12.00", 
      "sku": "dasd", 
      "currency": "USD", 
      "description": "blue" 
      } 
     ], 
     "shipping_address": { 
      "recipient_name": "John Johnson", 
      "line1": "Whatever street 2", 
      "line2": "Another street 2", 
      "city": "London", 
      "country_code": "UK", 
      "postal_code": "NR30 1LY", 
      "state": "England", 
      "phone": "3123123123" 
     } 
     } 
    } 
    ] 
} 

另一個問題我想請教的是我怎麼能找出的恰恰是錯誤後,我的var_dump()捲曲請求時,它的反應要麼返回一個空字符串,在這種情況下它不起作用,或者它返回包含JSON對象的所需請求,我從來沒有收到錯誤或任何東西

這是我用來提交上述

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment"); 
curl_setopt($ch, CURLOPT_HEADER, $token); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", $token)); 
$response = curl_exec($ch); 
curl_close($ch); 

回答

1

的JSON對象的代碼你已經走你的對象有點失靈,試試這樣:

$json_object = array(
    "intent" => "sale", 
    "redirect_urls" => array(
     "return_url" => "localhost/oauth2/src/OAuth2/success.php", 
     "cancel_url" => "localhost" 
    ), 
    "payer" => array(
     "payment_method" => "paypal", 
     "payer_info" => array(
      "shipping_address" => array(
       "recipient_name" => "John Johnson", 
       "line1" => "Whatever street 2", 
       "line2" => "Another street 2", 
       "city" => "London", 
       "country_code" => "UK", 
       "postal_code" => "NR30 1LY", 
       "state" => "England", 
       "phone" => "3123123123" 
      ) 
     ) 
    ), 
    "transactions" => array(
      0 => array(
      "amount" => array(
       "total" => "12.00", 
       "currency" => "USD" 
      ), 
      "description" => "payment description", 
      "item_list" => array(
       "items" => array(
        0 => array(
         "quantity" => "1", 
         "name" => "jacket", 
         "price" => "12.00", 
         "sku" => "dasd", 
         "currency" => "USD", 
         "description" => "blue" 
        ) 
       ) 
      ) 
     ) 
    ) 
); 
+0

那麼它工作得很好我現在正在做的事情,我真的不明白這一點。 – user3788675 2014-10-10 19:46:40

+0

@ user3788675看到我的編輯 – 2014-10-10 19:49:50

+0

我不認爲你閱讀我的問題,「運送地址對象有問題,因爲只要我將其刪除,一切順利」 – user3788675 2014-10-10 19:51:16