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

Stripe: Preview a plan or seat change

See more Stripe Examples

Preview a seat or plan change and display its proration lines, total, and the timestamp to retain with the quote.

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 subscriptionId: String? = "sub_FROM_ACCOUNT"

    var itemId: String? = "si_SELECTED_ITEM"

    var priceId: String? = "price_FROM_CATALOG"

    let now = CkoDateTime()!
    now.setFromCurrentSystemTime()
    var prorationDate: String? = now.get(asUnixTimeStr: false)

    //  curl -X POST "https://api.stripe.com/v1/invoices/create_preview" \
    //    -u "$STRIPE_API_KEY:" \
    //    -H "Stripe-Version: 2026-10-28.endive" \
    //    -d "subscription={subscriptionId}" \
    //    -d "subscription_details[items][0][id]={itemId}" \
    //    -d "subscription_details[items][0][price]={priceId}" \
    //    -d "subscription_details[items][0][quantity]=5" \
    //    -d "subscription_details[proration_behavior]=create_prorations" \
    //    -d "subscription_details[proration_date]={prorationDate}"
    cmd.setString(value: "curl -X POST \"https://api.stripe.com/v1/invoices/create_preview\"")
    cmd.append(value: " -u \"@KEY@:\"")
    cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
    cmd.append(value: " -d \"subscription=@SUB@\"")
    cmd.append(value: " -d \"subscription_details[items][0][id]=@ITEM@\"")
    cmd.append(value: " -d \"subscription_details[items][0][price]=@PRICE@\"")
    cmd.append(value: " -d \"subscription_details[items][0][quantity]=5\"")
    cmd.append(value: " -d \"subscription_details[proration_behavior]=create_prorations\"")
    cmd.append(value: " -d \"subscription_details[proration_date]=@DATE@\"")
    replaced = cmd.replace(value: "@SUB@", replacement: subscriptionId).intValue
    replaced = cmd.replace(value: "@ITEM@", replacement: itemId).intValue
    replaced = cmd.replace(value: "@PRICE@", replacement: priceId).intValue
    replaced = cmd.replace(value: "@DATE@", replacement: prorationDate).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
    }

    print("total = \(json.string(of: "total")!)")
    print("currency = \(json.string(of: "currency")!)")

    print("Save this preview time with the selected changes: \(prorationDate!)")
    var i: Int = 0
    while (i < json.size(ofArray: "lines.data").intValue) {
        json.i = i
        if json.bool(of: "lines.data[i].parent.subscription_item_details.proration") == true {
            print("\(json.string(of: "lines.data[i].description")!)  \(json.string(of: "lines.data[i].amount")!)")
        }

        i = i + 1
    }

    if json.bool(of: "lines.has_more") == true {
        print("Only the first page of preview lines is shown. The total covers all lines.")
    }


}