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

Lua
local chilkat = require("chilkat")

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

local itemId = "si_SELECTED_ITEM"

local priceId = "price_FROM_CATALOG"

local now = chilkat.DateTime.new()
now:set_from_current_system_time()
local prorationDate = now:get_as_unix_time_str(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:set_string("curl -X POST \"https://api.stripe.com/v1/invoices/create_preview\"")
cmd:append(" -u \"@KEY@:\"")
cmd:append(" -H \"Stripe-Version: 2026-10-28.endive\"")
cmd:append(" -d \"subscription=@SUB@\"")
cmd:append(" -d \"subscription_details[items][0][id]=@ITEM@\"")
cmd:append(" -d \"subscription_details[items][0][price]=@PRICE@\"")
cmd:append(" -d \"subscription_details[items][0][quantity]=5\"")
cmd:append(" -d \"subscription_details[proration_behavior]=create_prorations\"")
cmd:append(" -d \"subscription_details[proration_date]=@DATE@\"")
local replaced = cmd:replace("@SUB@", subscriptionId)
replaced = cmd:replace("@ITEM@", itemId)
replaced = cmd:replace("@PRICE@", priceId)
replaced = cmd:replace("@DATE@", prorationDate)
replaced = cmd:replace("@KEY@", apiKey)
local 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

print("total = " .. json:string_of("total"))
print("currency = " .. json:string_of("currency"))

print("Save this preview time with the selected changes: " .. prorationDate)
local i = 0
while i < json:size_of_array("lines.data") do
    json.i = i
    if json:bool_of("lines.data[i].parent.subscription_item_details.proration") then
        print(json:string_of("lines.data[i].description") .. "  " .. json:string_of("lines.data[i].amount"))
    end

    i = i + 1
end

if json:bool_of("lines.has_more") then
    print("Only the first page of preview lines is shown. The total covers all lines.")
end