PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.pb"
IncludeFile "CkDateTime.pb"
Procedure ChilkatExample()
success.i = 0
; Requires an unlocked Chilkat bundle. See Global Unlock Sample.
; Supply a restricted sandbox key from merchant-controlled configuration.
apiKey.s = "YOUR_STRIPE_TEST_KEY"
curl.i = CkHttpCurl::ckCreate()
If curl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
cmd.i = CkStringBuilder::ckCreate()
If cmd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
replaced.i
subscriptionId.s = "sub_FROM_ACCOUNT"
itemId.s = "si_SELECTED_ITEM"
priceId.s = "price_FROM_CATALOG"
now.i = CkDateTime::ckCreate()
If now.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkDateTime::ckSetFromCurrentSystemTime(now)
prorationDate.s = CkDateTime::ckGetAsUnixTimeStr(now,0)
; 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}"
CkStringBuilder::ckSetString(cmd,"curl -X POST " + Chr(34) + "https://api.stripe.com/v1/invoices/create_preview" + Chr(34))
CkStringBuilder::ckAppend(cmd," -u " + Chr(34) + "@KEY@:" + Chr(34))
CkStringBuilder::ckAppend(cmd," -H " + Chr(34) + "Stripe-Version: 2026-10-28.endive" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription=@SUB@" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription_details[items][0][id]=@ITEM@" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription_details[items][0][price]=@PRICE@" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription_details[items][0][quantity]=5" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription_details[proration_behavior]=create_prorations" + Chr(34))
CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "subscription_details[proration_date]=@DATE@" + Chr(34))
replaced = CkStringBuilder::ckReplace(cmd,"@SUB@",subscriptionId)
replaced = CkStringBuilder::ckReplace(cmd,"@ITEM@",itemId)
replaced = CkStringBuilder::ckReplace(cmd,"@PRICE@",priceId)
replaced = CkStringBuilder::ckReplace(cmd,"@DATE@",prorationDate)
replaced = CkStringBuilder::ckReplace(cmd,"@KEY@",apiKey)
success = CkHttpCurl::ckDoYourThing(curl,CkStringBuilder::ckGetAsString(cmd))
If success <> 1
Debug "No HTTP response. Chilkat failure code: " + Str(CkHttpCurl::ckFailReason(curl))
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(cmd)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(now)
ProcedureReturn
EndIf
success = CkHttpCurl::ckGetResponseJson(curl,json)
If (CkHttpCurl::ckStatusCode(curl) <> 200) OR (success <> 1)
Debug "Stripe HTTP " + Str(CkHttpCurl::ckStatusCode(curl)) + " Request-Id: " + CkHttpCurl::ckGetResponseHeaderField(curl,"Request-Id")
If success = 1
Debug CkJsonObject::ckStringOf(json,"error.message")
EndIf
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(cmd)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(now)
ProcedureReturn
EndIf
Debug "total = " + CkJsonObject::ckStringOf(json,"total")
Debug "currency = " + CkJsonObject::ckStringOf(json,"currency")
Debug "Save this preview time with the selected changes: " + prorationDate
i.i = 0
While (i < CkJsonObject::ckSizeOfArray(json,"lines.data"))
CkJsonObject::setCkI(json, i)
If CkJsonObject::ckBoolOf(json,"lines.data[i].parent.subscription_item_details.proration") = 1
Debug CkJsonObject::ckStringOf(json,"lines.data[i].description") + " " + CkJsonObject::ckStringOf(json,"lines.data[i].amount")
EndIf
i = i + 1
Wend
If CkJsonObject::ckBoolOf(json,"lines.has_more") = 1
Debug "Only the first page of preview lines is shown. The total covers all lines."
EndIf
CkHttpCurl::ckDispose(curl)
CkStringBuilder::ckDispose(cmd)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(now)
ProcedureReturn
EndProcedure