繁体   English   中英

PHP cURL 取消订阅 PayPal 问题

[英]PHP cURL Cancel Subscription PayPal issues

我承认,我在这方面已经坚持了 3 天,来回翻阅文档,甚至通过 Postman 学会了如何创建自己的产品和计划。

所以我有一个完整的工作订阅按钮,现在我知道我需要保存订阅 ID 才能取消订阅,所以现在保存和加载完美。

令我惊讶的是,每次有人想取消时我都需要获得一个 AuthKey,所以我需要运行 2 个 cURL 命令、1 个 GET Auth Key 和 1 个 POST 取消订阅。 我将如何在 PHP 中执行此操作?

当这些命令只运行没有数据时,我没有收到任何错误。

Paypal cURL Example: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel

////////////////////////////// GET TEMP ACCESS TOKEN/////////////////////////////////
$ch = curl_init();
$clientId = "x";
$secret = "x";
$myIDKEY = "";

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);
    $myIDKEY = $json->access_token;
}

curl_close($ch);

/////////////////////////////// SEND CANCEL POST ////////////////////////////////
$ch = curl_init();
$headers  = [
            'Authorization: Bearer '.$myIDKEY, 
            'Content-Type: application/json'
        ];
$postData = [
    'reason' => 'clicked cancel subscription button'
];
curl_setopt($ch, CURLOPT_URL,"https://api.sandbox.paypal.com/v1/billing/subscriptions/".$_SESSION['honeybeesubID']."/cancel");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result     = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


echo $myIDKEY

太感谢了!

https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel中所述,成功响应是 Z293C9EA246FF9985DC6F62A650F78986 数据。

因此,收到空响应以及该状态代码是正常的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM