Subscription API doesn't seem to remember subscription IDs

Gopher1
Contributor
Contributor

Hello, I'm a developer working for a site looking to integrate paypal subscriptions into our services.  I'm running into a problem where I can create subscriptions, but not access them after the fact.

 

To demonstrate, I wrote the following script, intended to create, load information about, and then cancel a subscription.  Only the first step seems to succeed.

$header =
	[
		"Content-Type:application/json",
		"Authorization:Bearer ".$accessToken
	];

	$post =
	[
		"plan_id" => $planId
	];
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/");
	curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($post));
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_USERPWD, "$paypalUsername:$paypalPassword");
	$result = curl_exec($ch);
	var_dump( $result);
	echo "\n\n";
	
	$res = json_decode($result,true);
	$subscriptionId = $res["id"];
	echo "Subscription id:".$subscriptionId."\n\n";
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/".$subscriptionId."/");
	curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($post));
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_USERPWD, "$paypalUsername:$paypalPassword");
	$result = curl_exec($ch);
	
	var_dump( $result);
	echo "\n\n";
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/".$subscriptionId."/cancel");
	curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($post));
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_USERPWD, "$paypalUsername:$paypalPassword");
	$result = curl_exec($ch);
	
	var_dump ($result);
	echo "\n\n";

This returns the following:

string(457) "{"status":"APPROVAL_PENDING","id":"I-YUVAH3F0SG96","create_time":"2021-04-01T21:58:14Z","links":[{"href":"https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-8CP59302DD939213F","rel":"approve","method":"GET"},{"href":"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-YUVAH3F0SG96","rel":"edit","method":"PATCH"},{"href":"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-YUVAH3F0SG96","rel":"self","method":"GET"}]}"


Subscription id:I-YUVAH3F0SG96

string(0) ""


string(347) "{"name":"RESOURCE_NOT_FOUND","message":"The specified resource does not exist.","debug_id":"5276bf36b7c2e","details":[{"issue":"INVALID_RESOURCE_ID","description":"Requested resource ID was not found."}],"links":[{"href":"https://developer.paypal.com/docs/api/v1/billing/subscriptions#RESOURCE_NOT_FOUND","rel":"information_link","method":"GET"}]}"

As you can see, the subscription does get created, but any attempt to do anything with it fails.  Can anyone tell me what I'm doing wrong?

Login to Me Too
1 ACCEPTED SOLUTION

Accepted Solutions
Solved

Gopher1
Contributor
Contributor

Found the solution.  I needed to remove the CURLOPT_POSTFIELDS option.  I guess it only works as a GET request.  Thanks for reading, and I hope this helps someone who is as clueless as I am.

View solution in original post

Login to Me Too
2 REPLIES 2

Gopher1
Contributor
Contributor

Small update:  I tried approving the payment through my sandbox personal account to see if that did anything.  It did.  It seems like the "cancel" endpoint works if the subscription is approved by the subscriber.  However, the "details" endpoints still just returns an empty string, even though I can see through the sandbox business account that the subscription is there and active, and the id is correct.  Very strange.

 

EDIT:  It's not just subscriptions.  Plan also gives me a 404 error with no body even though I know the plan is correct because I can make subscriptions with it.

Login to Me Too
Solved

Gopher1
Contributor
Contributor

Found the solution.  I needed to remove the CURLOPT_POSTFIELDS option.  I guess it only works as a GET request.  Thanks for reading, and I hope this helps someone who is as clueless as I am.

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.