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

PayPal: List merchant invoices across pages

See more PayPal Examples

List invoices for the authenticated merchant. Request totals, iterate every page, and keep the PayPal ID alongside the human-readable number and current status.

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

  // Request total_pages so a merchant with more than one page sees every invoice.
  var page = 1;
  var lastPage = 1;
  while (page <= lastPage) {
    final url = CkStringBuilder();
    url.append('https://api-m.sandbox.paypal.com/v2/invoicing/invoices?total_required=true&page_size=20&page=');
    url.appendInt(page);
    try {
      http.httpNoBody('GET', url.getAsString(), 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;
    }

    var i = 0;
    final count = result.sizeOfArray('items');
    while (i < count) {
      result.i = i;
      print('${result.stringOf('items[i].id')}  ${result.stringOf('items[i].detail.invoice_number')}  ${result.stringOf('items[i].status')}');
      i++;
    }

    lastPage = result.intOf('total_pages');
    page++;
  }
}