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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let curl = CkoHttpCurl()!
    let cmd = CkoStringBuilder()!
    let json = CkoJsonObject()!

    var replaced: Int

    //  Example UTC interval: 1 October 2026 inclusive through 1 November exclusive.
    var fromTime: String? = "1790812800"
    var untilTime: String? = "1793491200"

    var selectedPayout: String? = "po_SELECTED_BANK_DEPOSIT"

    var expectedAmount: String? = ""
    var currency: String? = ""

    let pageSuffix = CkoStringBuilder()!
    var more: Bool = true
    var cursor: String? = ""
    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(value: "curl -X GET \"https://api.stripe.com/v1/payouts?created%5Bgte%5D=@FROM@&created%5Blt%5D=@UNTIL@&limit=100@CURSOR@\"")
        cmd.append(value: " -u \"@KEY@:\"")
        cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
        replaced = cmd.replace(value: "@CURSOR@", replacement: pageSuffix.getAsString()).intValue
        replaced = cmd.replace(value: "@FROM@", replacement: fromTime).intValue
        replaced = cmd.replace(value: "@UNTIL@", replacement: untilTime).intValue
        replaced = cmd.replace(value: "@KEY@", replacement: apiKey).intValue
        success = curl.doYourThing(targetCurl: cmd.getAsString())
        if success != true {
            print("No HTTP response. Chilkat failure code: \(curl.failReason.intValue)")
            return
        }

        success = curl.getResponseJson(json: json)
        if (curl.statusCode.intValue != 200) || (success != true) {
            print("Stripe HTTP \(curl.statusCode.intValue)  Request-Id: \(curl.getResponseHeaderField(name: "Request-Id")!)")
            if success == true {
                print("\(json.string(of: "error.message")!)")
            }

            return
        }

        var i: Int = 0
        var count: Int = json.size(ofArray: "data").intValue
        while (i < count) {
            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 {
                if (json.bool(of: "data[i].automatic") != true) || (json.string(of: "data[i].reconciliation_status") != "completed") {
                    print("Select an automatic payout with completed reconciliation.")
                    return
                }

                expectedAmount = json.string(of: "data[i].amount")
                currency = json.string(of: "data[i].currency")
            }

            i = i + 1
        }

        more = json.bool(of: "has_more")
        if more == true {
            var next: String? = json.string(of: "data[i].id")
            if (count < 1) || (next == "") || (next == cursor) {
                print("Invalid pagination cursor.")
                return
            }

            cursor = next
            pageSuffix.setString(value: "&starting_after=")
            pageSuffix.append(value: cursor)
        }

    }

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

    var totalNet: Int = 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.
    let number = CkoStringBuilder()!

    let pageSuffixTxn = CkoStringBuilder()!
    var moreTxn: Bool = true
    var cursorTxn: String? = ""
    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(value: "curl -X GET \"https://api.stripe.com/v1/balance_transactions?payout=@PAYOUT@&limit=100@CURSOR@\"")
        cmd.append(value: " -u \"@KEY@:\"")
        cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
        replaced = cmd.replace(value: "@CURSOR@", replacement: pageSuffixTxn.getAsString()).intValue
        replaced = cmd.replace(value: "@PAYOUT@", replacement: selectedPayout).intValue
        replaced = cmd.replace(value: "@KEY@", replacement: apiKey).intValue
        success = curl.doYourThing(targetCurl: cmd.getAsString())
        if success != true {
            print("No HTTP response. Chilkat failure code: \(curl.failReason.intValue)")
            return
        }

        success = curl.getResponseJson(json: json)
        if (curl.statusCode.intValue != 200) || (success != true) {
            print("Stripe HTTP \(curl.statusCode.intValue)  Request-Id: \(curl.getResponseHeaderField(name: "Request-Id")!)")
            if success == true {
                print("\(json.string(of: "error.message")!)")
            }

            return
        }

        var iTxn: Int = 0
        var countTxn: Int = json.size(ofArray: "data").intValue
        while (iTxn < countTxn) {
            json.i = iTxn
            print("\(json.string(of: "data[i].id")!)  \(json.string(of: "data[i].net")!)")
            if json.string(of: "data[i].currency") != currency {
                print("Currency mismatch.")
                return
            }

            //  Exclude the bank withdrawal itself if it appears in this batch.
            if json.string(of: "data[i].source") != selectedPayout {
                var netText: String? = json.string(of: "data[i].net")
                var entryNet: Int = json.int(of: "data[i].net").intValue
                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 = totalNet + entryNet
            }

            iTxn = iTxn + 1
        }

        moreTxn = json.bool(of: "has_more")
        if moreTxn == true {
            var nextTxn: String? = json.string(of: "data[i].id")
            if (countTxn < 1) || (nextTxn == "") || (nextTxn == cursorTxn) {
                print("Invalid pagination cursor.")
                return
            }

            cursorTxn = nextTxn
            pageSuffixTxn.setString(value: "&starting_after=")
            pageSuffixTxn.append(value: cursorTxn)
        }

    }

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


}