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

Stripe: Schedule or undo a period-end cancellation

See more Stripe Examples

Schedule cancellation at the end of the billing period or undo a pending cancellation.

Stripe documentation and cURL example

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.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"
    ;  Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
    operationId.s = "YOUR_SAVED_OPERATION_UUID"

    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"

    ;  Use false to undo a pending cancellation while the subscription is active.

    ;  curl -X POST "https://api.stripe.com/v1/subscriptions/{subscriptionId}" \
    ;    -u "$STRIPE_API_KEY:" \
    ;    -H "Stripe-Version: 2026-10-28.endive" \
    ;    -H "Idempotency-Key: {operationId}-period-end" \
    ;    -d "cancel_at_period_end=true"
    CkStringBuilder::ckSetString(cmd,"curl -X POST " + Chr(34) + "https://api.stripe.com/v1/subscriptions/@SUB@" + 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," -H " + Chr(34) + "Idempotency-Key: @OP@-period-end" + Chr(34))
    CkStringBuilder::ckAppend(cmd," -d " + Chr(34) + "cancel_at_period_end=true" + Chr(34))
    replaced = CkStringBuilder::ckReplace(cmd,"@SUB@",subscriptionId)
    replaced = CkStringBuilder::ckReplace(cmd,"@OP@",operationId)
    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)
        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)
        ProcedureReturn
    EndIf

    Debug "id = " + CkJsonObject::ckStringOf(json,"id")
    Debug "status = " + CkJsonObject::ckStringOf(json,"status")

    Debug "Cancel at period end: " + Str(CkJsonObject::ckBoolOf(json,"cancel_at_period_end"))


    CkHttpCurl::ckDispose(curl)
    CkStringBuilder::ckDispose(cmd)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure