加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用PHP中的REST API进行Paypal付款

发布时间:2020-12-13 18:11:26 所属栏目:PHP教程 来源:网络整理
导读:我不清楚如何将此代码示例放入 PHP的CURL结构中,特别是-d句柄. curl -v https://api.sandbox.paypal.com/v1/payments/payment -H 'Content-Type:application/json' -H 'Authorization:Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK' -d '{ "inten
我不清楚如何将此代码示例放入 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,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);
}
尝试这样的事情,-d是你要发布的数据.自定义标头使用CURLOPT_HTTPHEADER设置.
$data = '{
  "intent":"sale","description":"This is the payment transaction description."
    }
  ]
}';

curl_setopt($ch,"https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch,$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,false);例如,如果您不需要返回标头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读