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

Stripe: Match an automatic bank payout to its transactions

See more Stripe Examples

Reconcile a selected automatic payout by comparing its amount with the sum of its settlement entries across all result pages.

Stripe documentation and cURL example

Chilkat Dart Downloads

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

void main() {
  var success = false;

  // Requires an unlocked Chilkat bundle. See Global Unlock Sample.
  // Supply a restricted sandbox key from merchant-controlled configuration.
  final apiKey = 'YOUR_STRIPE_TEST_KEY';

  final curl = CkHttpCurl();
  final cmd = CkStringBuilder();
  final json = CkJsonObject();

  var replaced = 0;

  // Example UTC interval: 1 October 2026 inclusive through 1 November exclusive.
  final fromTime = '1790812800';
  final untilTime = '1793491200';

  final selectedPayout = 'po_SELECTED_BANK_DEPOSIT';

  var expectedAmount = '';
  var currency = '';

  final pageSuffix = CkStringBuilder();
  var more = true;
  var cursor = '';
  while (more) {
    // curl -X GET "https://api.stripe.com/v1/payouts?created%5Bgte%5D={fromTime}&created%5Blt%5D={untilTime}&limit=100" \
    //   -u "$STRIPE_API_KEY:" \
    //   -H "Stripe-Version: 2026-10-28.endive"
    cmd.setString('curl -X GET "https://api.stripe.com/v1/payouts?created%5Bgte%5D=@FROM@&created%5Blt%5D=@UNTIL@&limit=100@CURSOR@"');
    cmd.append(' -u "@KEY@:"');
    cmd.append(' -H "Stripe-Version: 2026-10-28.endive"');
    replaced = cmd.replace('@CURSOR@', pageSuffix.getAsString());
    replaced = cmd.replace('@FROM@', fromTime);
    replaced = cmd.replace('@UNTIL@', untilTime);
    replaced = cmd.replace('@KEY@', apiKey);
    success = true;
    try {
      curl.doYourThing(cmd.getAsString());
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print('No HTTP response. Chilkat failure code: ${curl.failReason}');
      return;
    }

    success = true;
    try {
      curl.getResponseJson(json);
    } on ChilkatException {
      success = false;
    }
    if ((curl.statusCode != 200) || (!success)) {
      print('Stripe HTTP ${curl.statusCode}  Request-Id: ${curl.getResponseHeaderField('Request-Id')}');
      if (success) {
        print(json.stringOf('error.message'));
      }

      return;
    }

    var i = 0;
    final count = json.sizeOfArray('data');
    while (i < count) {
      json.i = i;
      print('${json.stringOf('data[i].id')} | ${json.stringOf('data[i].amount')} | ${json.stringOf('data[i].currency')} | ${json.stringOf('data[i].reconciliation_status')}');
      if (json.stringOf('data[i].id') == selectedPayout) {
        if ((!json.boolOf('data[i].automatic')) || (json.stringOf('data[i].reconciliation_status') != 'completed')) {
          print('Select an automatic payout with completed reconciliation.');
          return;
        }

        expectedAmount = json.stringOf('data[i].amount');
        currency = json.stringOf('data[i].currency');
      }

      i++;
    }

    more = json.boolOf('has_more');
    if (more) {
      final next = json.stringOf('data[i].id');
      if ((count < 1) || ((next == '') || (next == cursor))) {
        print('Invalid pagination cursor.');
        return;
      }

      cursor = next;
      pageSuffix.setString('&starting_after=');
      pageSuffix.append(cursor);
    }
  }

  if (expectedAmount == '') {
    print('The selected payout was not in this date range.');
    return;
  }

  var totalNet = 0;
  // This portable example uses int for arithmetic. Reject an out-of-range entry
  // or sum before adding. A larger ledger can sum the exact strings using decimal arithmetic.
  final number = CkStringBuilder();

  final pageSuffixTxn = CkStringBuilder();
  var moreTxn = true;
  var cursorTxn = '';
  while (moreTxn) {
    // curl -X GET "https://api.stripe.com/v1/balance_transactions?payout={selectedPayout}&limit=100" \
    //   -u "$STRIPE_API_KEY:" \
    //   -H "Stripe-Version: 2026-10-28.endive"
    cmd.setString('curl -X GET "https://api.stripe.com/v1/balance_transactions?payout=@PAYOUT@&limit=100@CURSOR@"');
    cmd.append(' -u "@KEY@:"');
    cmd.append(' -H "Stripe-Version: 2026-10-28.endive"');
    replaced = cmd.replace('@CURSOR@', pageSuffixTxn.getAsString());
    replaced = cmd.replace('@PAYOUT@', selectedPayout);
    replaced = cmd.replace('@KEY@', apiKey);
    success = true;
    try {
      curl.doYourThing(cmd.getAsString());
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print('No HTTP response. Chilkat failure code: ${curl.failReason}');
      return;
    }

    success = true;
    try {
      curl.getResponseJson(json);
    } on ChilkatException {
      success = false;
    }
    if ((curl.statusCode != 200) || (!success)) {
      print('Stripe HTTP ${curl.statusCode}  Request-Id: ${curl.getResponseHeaderField('Request-Id')}');
      if (success) {
        print(json.stringOf('error.message'));
      }

      return;
    }

    var iTxn = 0;
    final countTxn = json.sizeOfArray('data');
    while (iTxn < countTxn) {
      json.i = iTxn;
      print('${json.stringOf('data[i].id')}  ${json.stringOf('data[i].net')}');
      if (json.stringOf('data[i].currency') != currency) {
        print('Currency mismatch.');
        return;
      }

      // Exclude the bank withdrawal itself if it appears in this batch.
      if (json.stringOf('data[i].source') != selectedPayout) {
        final netText = json.stringOf('data[i].net');
        final entryNet = json.intOf('data[i].net');
        number.intValue = entryNet;
        if (number.getAsString() != netText) {
          print('Entry exceeds the supported int range: $netText');
          return;
        }

        if (entryNet > 0) {
          if (totalNet > (2147483647 - entryNet)) {
            print('Total exceeds the supported int range.');
            return;
          }
        }

        if (entryNet < 0) {
          if (totalNet < ((0 - 2147483647) - (entryNet + 1))) {
            print('Total exceeds the supported int range.');
            return;
          }
        }

        totalNet += entryNet;
      }

      iTxn++;
    }

    moreTxn = json.boolOf('has_more');
    if (moreTxn) {
      final nextTxn = json.stringOf('data[i].id');
      if ((countTxn < 1) || ((nextTxn == '') || (nextTxn == cursorTxn))) {
        print('Invalid pagination cursor.');
        return;
      }

      cursorTxn = nextTxn;
      pageSuffixTxn.setString('&starting_after=');
      pageSuffixTxn.append(cursorTxn);
    }
  }

  number.intValue = totalNet;
  print('Settlement net: $totalNet  Bank payout: $expectedAmount $currency');
  if (number.getAsString() == expectedAmount) {
    print('Payout reconciled.');
  } else {
    print('Investigate the difference.');
  }
}