php - PayPal REST API 送货地址不适用于 cURL 请求

标签 php json rest curl paypal

我想这是一个由两部分组成的问题,所以基本上我正在使用 PayPal 的 REST api 并且我想设置付款。我制作了这个数组,我在它上面使用了 json_encode 并将它转换成它下面的 json 代码,所以基本上送货地址对象有问题,因为一旦我删除它,一切都会顺利进行。当我浏览 the documentation 时,我真的找不到问题,我已经按照它说的那样将它放入 item_list 对象中,填写了所有必填字段,所以我猜测语法可能有问题

$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() 响应 curl 请求后,它要么返回一个空字符串,在这种情况下它不起作用,要么返回包含 JSON 对象的所需请求,我从来没有收到错误或任何东西

这是我用来提交上面的 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);

最佳答案

你的对象有点乱,试试这样:

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

关于php - PayPal REST API 送货地址不适用于 cURL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306886/

相关文章:

java - Spring + REST 的 Pretty-Print 参数

php - 如何突破此 SQL 注入(inject)保护

php - 如何创建以Mysql表列名命名的 session ?

php - 当我使用 PHP 发送推送通知消息时获取反斜杠 (\) 和撇号 (')

java - Jackson - 处理多种类型的 JSON

java - 如何使用postman调用REST API进行azure文件存储?

PHP MySQL utf-8 欧元符号在菱形上显示为问号

javascript - d3 v4 : Tree map is giving NaN for x and y

java - 合并来自 BigQuery 的 100 个具有相同结构的 json 文件

c# - Web API - 405 - 请求的资源不支持 http 方法 'PUT'