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

Ruby
require 'chilkat'

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 = Chilkat::CkHttpCurl.new()
cmd = Chilkat::CkStringBuilder.new()
json = Chilkat::CkJsonObject.new()

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

selectedPayout = "po_SELECTED_BANK_DEPOSIT"

expectedAmount = ""
currency = ""

pageSuffix = Chilkat::CkStringBuilder.new()
more = true
cursor = ""
while (more == true)
    #  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)
        print "No HTTP response. Chilkat failure code: " + curl.get_FailReason().to_s() + "\n";
        exit
    end

    success = curl.GetResponseJson(json)
    if ((curl.get_StatusCode() != 200) or (success != true))
        print "Stripe HTTP " + curl.get_StatusCode().to_s() + "  Request-Id: " + curl.getResponseHeaderField("Request-Id") + "\n";
        if (success == true)
            print json.stringOf("error.message") + "\n";
        end

        exit
    end

    i = 0
    count = json.SizeOfArray("data")
    while (i < count)
        json.put_I(i)
        print json.stringOf("data[i].id") + " | " + json.stringOf("data[i].amount") + " | " + json.stringOf("data[i].currency") + " | " + json.stringOf("data[i].reconciliation_status") + "\n";
        if (json.stringOf("data[i].id") == selectedPayout)
            if ((json.BoolOf("data[i].automatic") != true) or (json.stringOf("data[i].reconciliation_status") != "completed"))
                print "Select an automatic payout with completed reconciliation." + "\n";
                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)
        next = json.stringOf("data[i].id")
        if ((count < 1) or (next == "") or (next == cursor))
            print "Invalid pagination cursor." + "\n";
            exit
        end

        cursor = next
        pageSuffix.SetString("&starting_after=")
        pageSuffix.Append(cursor)
    end

end

if (expectedAmount == "")
    print "The selected payout was not in this date range." + "\n";
    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 = Chilkat::CkStringBuilder.new()

pageSuffixTxn = Chilkat::CkStringBuilder.new()
moreTxn = true
cursorTxn = ""
while (moreTxn == true)
    #  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)
        print "No HTTP response. Chilkat failure code: " + curl.get_FailReason().to_s() + "\n";
        exit
    end

    success = curl.GetResponseJson(json)
    if ((curl.get_StatusCode() != 200) or (success != true))
        print "Stripe HTTP " + curl.get_StatusCode().to_s() + "  Request-Id: " + curl.getResponseHeaderField("Request-Id") + "\n";
        if (success == true)
            print json.stringOf("error.message") + "\n";
        end

        exit
    end

    iTxn = 0
    countTxn = json.SizeOfArray("data")
    while (iTxn < countTxn)
        json.put_I(iTxn)
        print json.stringOf("data[i].id") + "  " + json.stringOf("data[i].net") + "\n";
        if (json.stringOf("data[i].currency") != currency)
            print "Currency mismatch." + "\n";
            exit
        end

        #  Exclude the bank withdrawal itself if it appears in this batch.
        if (json.stringOf("data[i].source") != selectedPayout)
            netText = json.stringOf("data[i].net")
            entryNet = json.IntOf("data[i].net")
            number.put_IntValue(entryNet)
            if (number.getAsString() != netText)
                print "Entry exceeds the supported int range: " + netText + "\n";
                exit
            end

            if (entryNet > 0)
                if (totalNet > (2147483647 - entryNet))
                    print "Total exceeds the supported int range." + "\n";
                    exit
                end

            end

            if (entryNet < 0)
                if (totalNet < ((0 - 2147483647) - (entryNet + 1)))
                    print "Total exceeds the supported int range." + "\n";
                    exit
                end

            end

            totalNet = totalNet + entryNet
        end

        iTxn = iTxn + 1
    end
    moreTxn = json.BoolOf("has_more")
    if (moreTxn == true)
        nextTxn = json.stringOf("data[i].id")
        if ((countTxn < 1) or (nextTxn == "") or (nextTxn == cursorTxn))
            print "Invalid pagination cursor." + "\n";
            exit
        end

        cursorTxn = nextTxn
        pageSuffixTxn.SetString("&starting_after=")
        pageSuffixTxn.Append(cursorTxn)
    end

end

number.put_IntValue(totalNet)
print "Settlement net: " + totalNet.to_s() + "  Bank payout: " + expectedAmount + " " + currency + "\n";
if (number.getAsString() == expectedAmount)
    print "Payout reconciled." + "\n";
else
    print "Investigate the difference." + "\n";
end