[PHP] Cancel a subscription via API

s_echelon
Contributor
Contributor

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!

Login to Me Too
1 REPLY 1

s_echelon
Contributor
Contributor

Up.
Has anyone encountered similar difficulties or would you know where I am wrong?
Thanks a lot!

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.