Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

PayPal: Suspend an active subscription

See more PayPal Examples

Pause future billing after checking that the subscription is active. Save the reason with the customer support record.

PayPal documentation

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;

// See Unlock Chilkat before making requests.
// For the merchant's internal backoffice application. Load the sandbox token
// from the merchant's credential store. Keep it out of publicly distributed apps.
// See Get a PayPal access token.
$http = new Http();
$http->AuthToken = 'SANDBOX_ACCESS_TOKEN';
$resp = new HttpResponse();

$http->SetUrlVar('id', 'SUBSCRIPTION_ID');

try {
    $http->HttpNoBody('GET', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}', $resp);
} catch (\Chilkat2\Exception $e) {
    echo 'No HTTP response: ', $e->getMessage(), "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    $errorInfo = new JsonObject();
    $errorInfo->Load($resp->BodyStr);
    echo 'HTTP ', $resp->StatusCode, ' ', $errorInfo->StringOf('name'), ' ', $errorInfo->StringOf('details[0].issue'), "\n";
    echo 'PayPal debug ID: ', $errorInfo->StringOf('debug_id'), "\n";
    exit;
}

$result = new JsonObject();
try {
    $result->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
    exit;
}

if (!$result->StringOfEquals('status', 'ACTIVE', true)) {
    echo 'Subscription status is ', $result->StringOf('status'), ". No change made.\n";
    exit;
}

$body = new JsonObject();
$body->UpdateString('reason', 'Customer requested a billing pause.');
try {
    $http->HttpJson('POST', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/suspend', $body, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
    echo 'No HTTP response: ', $e->getMessage(), "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    $changeError = new JsonObject();
    $changeError->Load($resp->BodyStr);
    echo 'HTTP ', $resp->StatusCode, ' ', $changeError->StringOf('name'), ' ', $changeError->StringOf('details[0].issue'), "\n";
    echo 'PayPal debug ID: ', $changeError->StringOf('debug_id'), "\n";
    exit;
}

echo "PayPal accepted the subscription change.\n";