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

Posted

技术标签:

【中文标题】PayPal REST API 送货地址不适用于 cURL 请求【英文标题】:PayPal REST API shipping address not working upon a cURL request 【发布时间】:2014-12-06 01:32:06 【问题描述】:

我想这是一个两部分的问题,所以基本上我正在使用 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);

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

嗯,它适用于我目前正在做的事情,我真的不明白这一点。 我认为您没有阅读我的问题,“送货地址对象有问题,因为一旦我删除它,一切都会顺利进行” 对不起,我一定错过了。但是,根据您链接到地址的文档,应该是 payer_info 对象的属性。您的 php 看起来像是 transactionsitems_list.. 的一部分。 @user3788675 很模糊,说可以在item_list,payer_info里面,在“创建协议”下。而且这些示例也没有显示如何做到这一点...... @user3788675,如果送货地址与付款人对象中的地址不同,您只能将其用作item-list 的预操作。 Payer 对象应包含另一个名为 Payer_info 的对象,其中应包含地址。

以上是关于PayPal REST API 送货地址不适用于 cURL 请求的主要内容,如果未能解决你的问题,请参考以下文章

根据用户的送货地址设置运费 Paypal REST API

Paypal Rest API - 提交帐单地址

Paypal REST api 调用适用于 cURL,但不适用于 C# 代码

PayPal REST SDK:删除送货地址和付款授权,使其进入待处理状态[关闭]

PayPal REST API 未返回 payment_info

PayPal API 通过交易 ID 获取送货地址