[PHP] Cancel a subscription via API
s_echelon
Contributor
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
Jun-14-2019
10:31 AM
Hi!
I need to cancel a subscription via API in PHP. But I can neither receive the information of a subscription or cancel a subscription.
This is my PHP code:
//STEP 1 - GET ACCESS TOKEN
$ch = curl_init();
$clientId = "XXXXXX";
$secret = "XXXXX";
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_SSLVERSION , 6); //NEW ADDITION
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);
$access_token=$json->access_token;
}
curl_close($ch);
//STEP 2 - GET INFO OF SUBSCRIPTION
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/billing/subscriptions/I-XXXXXX");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Authorization: Bearer $access_token'
));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
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($access_token);
}
curl_close($ch);
If I launch this command from the terminal:
curl -v -X POST https://api.sandbox.paypal.com/v1/billing/plans/P-7GL4271244454362WXNWU5NQ/deactivate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer "
Or
curl -v -X POST https://api.sandbox.paypal.com/v1/billing/plans/P-7GL4271244454362WXNWU5NQ/deactivate \
-H "Content-Type: application/json" \
-H "Authorization: Basic :"
I retrieve this:
* Closing connection 0
* TLSv1.2 (OUT), TLS alert, Client hello (1):
{"name":"RESOURCE_NOT_FOUND","message":"The specified resource does not exist.","de[Removed. Phone #s not permitted]ue":"INVALID_RESOURCE_ID","description":"Requested resource ID was not found."}],"links":[{"href":"https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID","rel":"information_lin..."}]}
But the subscription ID exists.
Where am I wrong?
Thanks so much!
Labels:
- Labels:
-
Payments REST APIs
1 REPLY 1
s_echelon
Contributor
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jun-17-2019
01:53 AM
Up.
Has anyone encountered similar difficulties or would you know where I am wrong?
Thanks a lot!
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
Related Content
- Pending Transaction - CLearly Declined by Merchant in PayPal Payments Standard
- can we assign a unique detail to every subscription creation? in NVP/SOAP APIs
- I want to implement subscription functionality. in REST APIs
- I need to integrate a recurring payment with a variable for a pay-as-you-go product in REST APIs
- Subscription notifications in PayPal Reporting