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

PayPal: Find invoices for a recipient

See more PayPal Examples

Filter by a sandbox recipient email address in the JSON body. Pagination belongs in the URL. Read every matching invoice ID; several invoices can share the same recipient.

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

  // Use a JSON search body on every page; the recipient email does not belong in the
  // URL.
  final body = CkJsonObject();
  body.updateString('recipient_email', 'buyer@example.com');
  var page = 1;
  var lastPage = 1;
  while (page <= lastPage) {
    final url = CkStringBuilder();
    url.append('https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=20&page=');
    url.appendInt(page);
    try {
      http.httpJson('POST', url.getAsString(), body, 'application/json', 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++;
  }
}