Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

-- Requires an unlocked Chilkat bundle. See Global Unlock Sample.
-- Supply a restricted sandbox key from merchant-controlled configuration.
local apiKey = "YOUR_STRIPE_TEST_KEY"

local curl = chilkat.HttpCurl.new()
local cmd = chilkat.StringBuilder.new()
local json = chilkat.JsonObject.new()

local replaced = 0

-- Example UTC interval: 1 October 2026 inclusive through 1 November exclusive.
local fromTime = "1790812800"
local untilTime = "1793491200"

local selectedPayout = "po_SELECTED_BANK_DEPOSIT"

local expectedAmount = ""
local currency = ""

local pageSuffix = chilkat.StringBuilder.new()
local more = true
local cursor = ""
while more do
    -- 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:set_string("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:get_as_string())
    replaced = cmd:replace("@FROM@", fromTime)
    replaced = cmd:replace("@UNTIL@", untilTime)
    replaced = cmd:replace("@KEY@", apiKey)
    success = curl:do_your_thing(cmd:get_as_string())
    if not success then
        print("No HTTP response. Chilkat failure code: " .. curl.fail_reason)
        return
    end

    success = curl:get_response_json(json)
    if (curl.status_code ~= 200) or (not success) then
        print("Stripe HTTP " .. curl.status_code .. "  Request-Id: " .. curl:get_response_header_field("Request-Id"))
        if success then
            print(json:string_of("error.message"))
        end

        return
    end

    local i = 0
    local count = json:size_of_array("data")
    while i < count do
        json.i = i
        print(json:string_of("data[i].id") .. " | " .. json:string_of("data[i].amount") .. " | " .. json:string_of("data[i].currency") .. " | " .. json:string_of("data[i].reconciliation_status"))
        if json:string_of("data[i].id") == selectedPayout then
            if (not json:bool_of("data[i].automatic")) or (json:string_of("data[i].reconciliation_status") ~= "completed") then
                print("Select an automatic payout with completed reconciliation.")
                return
            end

            expectedAmount = json:string_of("data[i].amount")
            currency = json:string_of("data[i].currency")
        end

        i = i + 1
    end

    more = json:bool_of("has_more")
    if more then
        local next_ = json:string_of("data[i].id")
        if (count < 1) or ((next_ == "") or (next_ == cursor)) then
            print("Invalid pagination cursor.")
            return
        end

        cursor = next_
        pageSuffix:set_string("&starting_after=")
        pageSuffix:append(cursor)
    end
end

if expectedAmount == "" then
    print("The selected payout was not in this date range.")
    return
end

local 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.
local number = chilkat.StringBuilder.new()

local pageSuffixTxn = chilkat.StringBuilder.new()
local moreTxn = true
local cursorTxn = ""
while moreTxn do
    -- 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:set_string("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:get_as_string())
    replaced = cmd:replace("@PAYOUT@", selectedPayout)
    replaced = cmd:replace("@KEY@", apiKey)
    success = curl:do_your_thing(cmd:get_as_string())
    if not success then
        print("No HTTP response. Chilkat failure code: " .. curl.fail_reason)
        return
    end

    success = curl:get_response_json(json)
    if (curl.status_code ~= 200) or (not success) then
        print("Stripe HTTP " .. curl.status_code .. "  Request-Id: " .. curl:get_response_header_field("Request-Id"))
        if success then
            print(json:string_of("error.message"))
        end

        return
    end

    local iTxn = 0
    local countTxn = json:size_of_array("data")
    while iTxn < countTxn do
        json.i = iTxn
        print(json:string_of("data[i].id") .. "  " .. json:string_of("data[i].net"))
        if json:string_of("data[i].currency") ~= currency then
            print("Currency mismatch.")
            return
        end

        -- Exclude the bank withdrawal itself if it appears in this batch.
        if json:string_of("data[i].source") ~= selectedPayout then
            local netText = json:string_of("data[i].net")
            local entryNet = json:int_of("data[i].net")
            number.int_value = entryNet
            if number:get_as_string() ~= netText then
                print("Entry exceeds the supported int range: " .. netText)
                return
            end

            if entryNet > 0 then
                if totalNet > (2147483647 - entryNet) then
                    print("Total exceeds the supported int range.")
                    return
                end
            end

            if entryNet < 0 then
                if totalNet < ((0 - 2147483647) - (entryNet + 1)) then
                    print("Total exceeds the supported int range.")
                    return
                end
            end

            totalNet = totalNet + entryNet
        end

        iTxn = iTxn + 1
    end

    moreTxn = json:bool_of("has_more")
    if moreTxn then
        local nextTxn = json:string_of("data[i].id")
        if (countTxn < 1) or ((nextTxn == "") or (nextTxn == cursorTxn)) then
            print("Invalid pagination cursor.")
            return
        end

        cursorTxn = nextTxn
        pageSuffixTxn:set_string("&starting_after=")
        pageSuffixTxn:append(cursorTxn)
    end
end

number.int_value = totalNet
print("Settlement net: " .. totalNet .. "  Bank payout: " .. expectedAmount .. " " .. currency)
if number:get_as_string() == expectedAmount then
    print("Payout reconciled.")
else
    print("Investigate the difference.")
end