【问题标题】:Making Paypal payment using REST API in PHP在 PHP 中使用 REST API 进行 Paypal 支付
【发布时间】:2013-04-25 00:20:33
【问题描述】:

我不清楚如何将此代码示例放入 PHP 的 CURL 结构中,特别是 -d 句柄。

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK' \
-d '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://<return URL here>",
    "cancel_url":"http://<cancel URL here>"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      },
      "description":"This is the payment transaction description."
    }
  ]
}'

我已经使用了以下测试调用,但不知道如何将其扩展到上面的示例。

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json->access_token);
}

【问题讨论】:

    标签: php curl paypal


    【解决方案1】:

    试试这样,-d 是你要发布的数据。自定义标头使用 CURLOPT_HTTPHEADER 设置。

    $data = '{
      "intent":"sale",
      "redirect_urls":{
        "return_url":"http://<return URL here>",
        "cancel_url":"http://<cancel URL here>"
      },
      "payer":{
        "payment_method":"paypal"
      },
      "transactions":[
        {
          "amount":{
            "total":"7.47",
            "currency":"USD"
          },
          "description":"This is the payment transaction description."
        }
      ]
    }';
    
    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      "Content-Type: application/json",
      "Authorization: Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK", 
      "Content-length: ".strlen($data))
    );
    

    看看这里的选项,http://php.net/manual/en/function.curl-setopt.php 设置 curl_setopt($ch, CURLOPT_HEADER, false);例如,如果您不需要返回的标头。

    【讨论】:

    • 这是一个完整的解决方案吗?我没有得到回应。我将授权更改为从与第一个示例非常相似的代码中检索到的类型和访问令牌。
    • 它给出错误:没有响应。
    • 它只给出 0 作为响应...请建议正确的代码
    • 对于那些遇到错误的人:没有响应。如果你使用的是旧的 php 版本,由于 OpenSSL 握手问题,它不会工作。我只是用 php 5.3.x 进行测试,它无法切换到 PHP 版本 5.6.28 并且它可以工作,因为它具有更新的 OpenSSL 版本......即使你使用 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );还是不行
    • 为我工作。谢谢
    猜你喜欢
    • 2016-04-03
    • 2014-11-01
    • 2021-12-03
    • 2013-09-01
    • 2019-05-15
    • 1970-01-01
    • 2011-06-10
    • 2018-06-12
    • 2017-02-16
    相关资源
    最近更新 更多