Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.HttpCurl,
Chilkat.StringBuilder,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
apiKey: string;
curl: THttpCurl;
cmd: TStringBuilder;
json: TJsonObject;
replaced: Integer;
fromTime: string;
untilTime: string;
selectedPayout: string;
expectedAmount: string;
currency: string;
pageSuffix: TStringBuilder;
more: Boolean;
cursor: string;
i: Integer;
count: Integer;
next: string;
totalNet: Integer;
number: TStringBuilder;
pageSuffixTxn: TStringBuilder;
moreTxn: Boolean;
cursorTxn: string;
iTxn: Integer;
countTxn: Integer;
netText: string;
entryNet: Integer;
nextTxn: string;
begin
success := False;
// Requires an unlocked Chilkat bundle. See Global Unlock Sample.
// Supply a restricted sandbox key from merchant-controlled configuration.
apiKey := 'YOUR_STRIPE_TEST_KEY';
curl := THttpCurl.Create;
cmd := TStringBuilder.Create;
json := TJsonObject.Create;
// Example UTC interval: 1 October 2026 inclusive through 1 November exclusive.
fromTime := '1790812800';
untilTime := '1793491200';
selectedPayout := 'po_SELECTED_BANK_DEPOSIT';
expectedAmount := '';
currency := '';
pageSuffix := TStringBuilder.Create;
more := True;
cursor := '';
while (more = True) do
begin
// 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 := curl.DoYourThing(cmd.GetAsString());
if (success <> True) then
begin
WriteLn('No HTTP response. Chilkat failure code: ' + curl.FailReason);
Exit;
end;
success := curl.GetResponseJson(json);
if ((curl.StatusCode <> 200) or (success <> True)) then
begin
WriteLn('Stripe HTTP ' + curl.StatusCode + ' Request-Id: ' + curl.GetResponseHeaderField('Request-Id'));
if (success = True) then
begin
WriteLn(json.StringOf('error.message'));
end;
Exit;
end;
i := 0;
count := json.SizeOfArray('data');
while (i < count) do
begin
json.I := i;
WriteLn(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) then
begin
if ((json.BoolOf('data[i].automatic') <> True) or (json.StringOf('data[i].reconciliation_status') <> 'completed')) then
begin
WriteLn('Select an automatic payout with completed reconciliation.');
Exit;
end;
expectedAmount := json.StringOf('data[i].amount');
currency := json.StringOf('data[i].currency');
end;
i := i + 1;
end;
more := json.BoolOf('has_more');
if (more = True) then
begin
next := json.StringOf('data[i].id');
if ((count < 1) or (next = '') or (next = cursor)) then
begin
WriteLn('Invalid pagination cursor.');
Exit;
end;
cursor := next;
pageSuffix.SetString('&starting_after=');
pageSuffix.Append(cursor);
end;
end;
if (expectedAmount = '') then
begin
WriteLn('The selected payout was not in this date range.');
Exit;
end;
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.
number := TStringBuilder.Create;
pageSuffixTxn := TStringBuilder.Create;
moreTxn := True;
cursorTxn := '';
while (moreTxn = True) do
begin
// 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 := curl.DoYourThing(cmd.GetAsString());
if (success <> True) then
begin
WriteLn('No HTTP response. Chilkat failure code: ' + curl.FailReason);
Exit;
end;
success := curl.GetResponseJson(json);
if ((curl.StatusCode <> 200) or (success <> True)) then
begin
WriteLn('Stripe HTTP ' + curl.StatusCode + ' Request-Id: ' + curl.GetResponseHeaderField('Request-Id'));
if (success = True) then
begin
WriteLn(json.StringOf('error.message'));
end;
Exit;
end;
iTxn := 0;
countTxn := json.SizeOfArray('data');
while (iTxn < countTxn) do
begin
json.I := iTxn;
WriteLn(json.StringOf('data[i].id') + ' ' + json.StringOf('data[i].net'));
if (json.StringOf('data[i].currency') <> currency) then
begin
WriteLn('Currency mismatch.');
Exit;
end;
// Exclude the bank withdrawal itself if it appears in this batch.
if (json.StringOf('data[i].source') <> selectedPayout) then
begin
netText := json.StringOf('data[i].net');
entryNet := json.IntOf('data[i].net');
number.IntValue := entryNet;
if (number.GetAsString() <> netText) then
begin
WriteLn('Entry exceeds the supported int range: ' + netText);
Exit;
end;
if (entryNet > 0) then
begin
if (totalNet > (2147483647 - entryNet)) then
begin
WriteLn('Total exceeds the supported int range.');
Exit;
end;
end;
if (entryNet < 0) then
begin
if (totalNet < ((0 - 2147483647) - (entryNet + 1))) then
begin
WriteLn('Total exceeds the supported int range.');
Exit;
end;
end;
totalNet := totalNet + entryNet;
end;
iTxn := iTxn + 1;
end;
moreTxn := json.BoolOf('has_more');
if (moreTxn = True) then
begin
nextTxn := json.StringOf('data[i].id');
if ((countTxn < 1) or (nextTxn = '') or (nextTxn = cursorTxn)) then
begin
WriteLn('Invalid pagination cursor.');
Exit;
end;
cursorTxn := nextTxn;
pageSuffixTxn.SetString('&starting_after=');
pageSuffixTxn.Append(cursorTxn);
end;
end;
number.IntValue := totalNet;
WriteLn('Settlement net: ' + totalNet + ' Bank payout: ' + expectedAmount + ' ' + currency);
if (number.GetAsString() = expectedAmount) then
begin
WriteLn('Payout reconciled.');
end
else
begin
WriteLn('Investigate the difference.');
end;
curl.Free;
cmd.Free;
json.Free;
pageSuffix.Free;
number.Free;
pageSuffixTxn.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.