简体   繁体   English

PHP cURL 取消订阅 PayPal 问题

[英]PHP cURL Cancel Subscription PayPal issues

I will admit, I have been on this for 3 days, back and forth through documentation, and even learned how to create my own products and plans through Postman.我承认,我在这方面已经坚持了 3 天,来回翻阅文档,甚至通过 Postman 学会了如何创建自己的产品和计划。

So I have a complete working subscription button, now I learned I need to save the Subscription ID in order to cancel the subscription so now that is saving and loading perfectly.所以我有一个完整的工作订阅按钮,现在我知道我需要保存订阅 ID 才能取消订阅,所以现在保存和加载完美。

To my sad surprise, I need to get an AuthKey every time someone wants to cancel, so I need to run 2 cURL commands, 1 GET Auth Key, and 1 POST cancel a subscription.令我惊讶的是,每次有人想取消时我都需要获得一个 AuthKey,所以我需要运行 2 个 cURL 命令、1 个 GET Auth Key 和 1 个 POST 取消订阅。 How would I do this in PHP?我将如何在 PHP 中执行此操作?

I don't get any errors when these commands run just no data.当这些命令只运行没有数据时,我没有收到任何错误。

Paypal cURL Example: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel 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

Thank you so much!太感谢了!

As documented at https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel , a success response is an HTTP 204 with no data.https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel中所述,成功响应是 Z293C9EA246FF9985DC6F62A650F78986 数据。

So, it will be normal to receive an empty response, along with that status code.因此,收到空响应以及该状态代码是正常的。

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

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