0 Daumen
665 Aufrufe

We already learned how to subscribe the user to our subscription plan in part 1 (paypal setup) and we learned in part 2. how to access the paypal subscription from our website.


Now the client might like to cancel the subscription. Let's do it:


Essential PHP function:

function paypal_subscription_pause($subscriptionid, $accesstoken)
{
// https://developer.paypal.com/docs/business/subscriptions/add-capabilities/pause-resume/
// https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_suspend

// client subscription ID, e.g. "I-WB34X81SWXYZ"

if(empty($subscriptionid) || empty($accesstoken))
{
  return;
}

$headers = [
  'Authorization: Bearer '.$accesstoken,
  'Content-Type: application/json'
];
$postData = [
  'reason' => 'Customer requested subscription cancellation'
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/billing/subscriptions/".$subscriptionid."/suspend"); // Sandbox
// curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/billing/subscriptions/".$subscriptionid."/suspend"); // LIVE
// if you replace "/suspend" with "/cancel" it is a hard cancel, not a pause

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);

$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// var_dump($response);
// var_dump($statusCode);

// $response might be without data but status code is 204 for cancelled
if($statusCode==204)
{
  return true;
}

return;
}


Alright, now let's use the function with our handling PHP code.

The following you can link from the user profile on your website, where the user clicks a "cancel button" and is directed to your site:

// EXISTING SUBSCRIPTION HANDLING, e.g. ?cancel

$docancel = $_GET['cancel']!==null;
$userid = 1234; // from your system

if($docancel)
{
// custom functions (stored data from our database on setup), use your own!
$userdata = get_customerdata($userid);
$paydata = payprovider_get_entry($userid);
$payprovider = $paydata['payprovider'];
$subscriptionid = $paydata['payerid'];

// the paypal function we created already
$accesstoken = paypal_get_accesstoken();

// make sure we have a subscription
if(!empty($subscriptionid))
{
// the paypal function we created already
  $paypal_status = paypal_subscription_get_status($subscriptionid, $accesstoken);

  if($paypal_status=='ACTIVE')
  {
  // PAUSE-CANCEL the subscription
    paypal_subscription_pause($subscriptionid, $accesstoken);

echo '<p>Subscription cancelled. Thanks for being our client.</p>';

  // you could set status to "suspended" in your database
     
    return;
  }
}

echo '<p>The paypal subscription is not active. There will be no automatic payments.</p>';

return;
}

Great. Done!

geschlossen: Tutorial
von Kai
Avatar von

Part 1: Initial Paypal Subscription Button Setup, Paypal App Setup, Webhook PHP Script, Button on Sales Page
https://www.stacklounge.de/6325/tutorial-integrate-paypal-subscription-javascript-complete

Part 2: Check Customer Paypal Subscription status from our own Website, Go Live
https://www.stacklounge.de/6328/tutorial-integrate-paypal-subscription-javascript-complete

Part 3: Cancel the Subscription (Customer clicks "cancel" on your website)
https://www.stacklounge.de/6330/tutorial-integrate-paypal-subscription-javascript-complete

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community