Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final http = CkHttp();
  http.authToken = 'SANDBOX_ACCESS_TOKEN';
  final resp = CkHttpResponse();

  http.setUrlVar('id', 'SUBSCRIPTION_ID');

  try {
    http.httpNoBody('GET', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{\$id}', resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

  if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
    final errorInfo = CkJsonObject();
    errorInfo.load(resp.bodyStr);
    print('HTTP ${resp.statusCode} ${errorInfo.stringOf('name')} ${errorInfo.stringOf('details[0].issue')}');
    print('PayPal debug ID: ${errorInfo.stringOf('debug_id')}');
    return;
  }

  final result = CkJsonObject();
  try {
    result.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON response; HTTP ${resp.statusCode}');
    return;
  }

  if (!result.stringOfEquals('status', 'ACTIVE', true)) {
    print('Subscription status is ${result.stringOf('status')}. No change made.');
    return;
  }

  final body = CkJsonObject();
  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);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

  if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
    final changeError = CkJsonObject();
    changeError.load(resp.bodyStr);
    print('HTTP ${resp.statusCode} ${changeError.stringOf('name')} ${changeError.stringOf('details[0].issue')}');
    print('PayPal debug ID: ${changeError.stringOf('debug_id')}');
    return;
  }

  print('PayPal accepted the subscription change.');
}