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

PayPal: Look up a subscription and its recent payments

See more PayPal Examples

Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.

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

  print('Status: ${result.stringOf('status')}');
  print('Next billing: ${result.stringOf('billing_info.next_billing_time')}');
  final now = CkDateTime();
  now.setFromCurrentSystemTime();
  final query = CkJsonObject();
  query.updateString('end_time', now.getAsTimestamp(false));
  now.addDays(-30);
  query.updateString('start_time', now.getAsTimestamp(false));
  try {
    http.httpParams('GET', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{\$id}/transactions', query, resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

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

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

  var i = 0;
  while (i < history.sizeOfArray('transactions')) {
    history.i = i;
    print('${history.stringOf('transactions[i].time')}  ${history.stringOf('transactions[i].id')}  ${history.stringOf('transactions[i].status')}  ${history.stringOf('transactions[i].amount_with_breakdown.gross_amount.value')} ${history.stringOf('transactions[i].amount_with_breakdown.gross_amount.currency_code')}');
    i++;
  }
}