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

Stripe: Identify the payment for a paid invoice

See more Stripe Examples

List the payments allocated to an invoice and identify a paid charge or PaymentIntent for refund.

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

    var invoiceId: String? = "in_FROM_BILLING_RECORD"

    let pageSuffix = CkoStringBuilder()!
    var more: Bool = true
    var cursor: String? = ""
    while (more == true) {
        //  curl -X GET "https://api.stripe.com/v1/invoice_payments?invoice={invoiceId}&limit=100" \
        //    -u "$STRIPE_API_KEY:" \
        //    -H "Stripe-Version: 2026-10-28.endive"
        cmd.setString(value: "curl -X GET \"https://api.stripe.com/v1/invoice_payments?invoice=@ID@&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: "@ID@", replacement: invoiceId).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].status")!) | \(json.string(of: "data[i].amount_paid")!) | \(json.string(of: "data[i].currency")!) | \(json.string(of: "data[i].payment.type")!)")
            if json.string(of: "data[i].payment.type") == "payment_intent" {
                print("  PaymentIntent: \(json.string(of: "data[i].payment.payment_intent")!)")
            }

            if json.string(of: "data[i].payment.type") == "charge" {
                print("  Charge: \(json.string(of: "data[i].payment.charge")!)")
            }

            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)
        }

    }

    //  Select the paid allocation belonging to the order before issuing its refund.

}