[英]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.