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

Stripe: Draft an invoice and attach line items

See more Stripe Examples

Create a draft for a customer and attach a priced line item to that specific invoice.

Stripe documentation and cURL example

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
string ls_ApiKey
string ls_OperationId
oleobject loo_Curl
oleobject loo_Cmd
oleobject loo_Json
long li_Replaced
string ls_CustomerId
string ls_PriceId
string ls_InvoiceId

li_Success = 0

//  Requires an unlocked Chilkat bundle. See Global Unlock Sample.
//  Supply a restricted sandbox key from merchant-controlled configuration.
ls_ApiKey = "YOUR_STRIPE_TEST_KEY"
//  Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
ls_OperationId = "YOUR_SAVED_OPERATION_UUID"

loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")
if li_rc < 0 then
    destroy loo_Curl
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Cmd = create oleobject
li_rc = loo_Cmd.ConnectToNewObject("Chilkat.StringBuilder")

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

ls_CustomerId = "cus_FROM_BILLING_ACCOUNT"

ls_PriceId = "price_FROM_CATALOG"

//  curl -X POST "https://api.stripe.com/v1/invoices" \
//    -u "$STRIPE_API_KEY:" \
//    -H "Stripe-Version: 2026-10-28.endive" \
//    -H "Idempotency-Key: {operationId}-draft" \
//    -d "customer={customerId}" \
//    -d "collection_method=send_invoice" \
//    -d "days_until_due=30" \
//    -d "auto_advance=false" \
//    -d "pending_invoice_items_behavior=exclude"
loo_Cmd.SetString("curl -X POST ~"https://api.stripe.com/v1/invoices~"")
loo_Cmd.Append(" -u ~"@KEY@:~"")
loo_Cmd.Append(" -H ~"Stripe-Version: 2026-10-28.endive~"")
loo_Cmd.Append(" -H ~"Idempotency-Key: @OP@-draft~"")
loo_Cmd.Append(" -d ~"customer=@CUSTOMER@~"")
loo_Cmd.Append(" -d ~"collection_method=send_invoice~"")
loo_Cmd.Append(" -d ~"days_until_due=30~"")
loo_Cmd.Append(" -d ~"auto_advance=false~"")
loo_Cmd.Append(" -d ~"pending_invoice_items_behavior=exclude~"")
li_Replaced = loo_Cmd.Replace("@CUSTOMER@",ls_CustomerId)
li_Replaced = loo_Cmd.Replace("@OP@",ls_OperationId)
li_Replaced = loo_Cmd.Replace("@KEY@",ls_ApiKey)
li_Success = loo_Curl.DoYourThing(loo_Cmd.GetAsString())
if li_Success <> 1 then
    Write-Debug "No HTTP response. Chilkat failure code: " + string(loo_Curl.FailReason)
    destroy loo_Curl
    destroy loo_Cmd
    destroy loo_Json
    return
end if

li_Success = loo_Curl.GetResponseJson(loo_Json)
if (loo_Curl.StatusCode <> 200) OR (li_Success <> 1) then
    Write-Debug "Stripe HTTP " + string(loo_Curl.StatusCode) + "  Request-Id: " + loo_Curl.GetResponseHeaderField("Request-Id")
    if li_Success = 1 then
        Write-Debug loo_Json.StringOf("error.message")
    end if

    destroy loo_Curl
    destroy loo_Cmd
    destroy loo_Json
    return
end if

ls_InvoiceId = loo_Json.StringOf("id")
Write-Debug "Save draft invoice ID: " + ls_InvoiceId
//  Attach this line to the draft. Use a distinct stable key per local line on retries.

//  curl -X POST "https://api.stripe.com/v1/invoiceitems" \
//    -u "$STRIPE_API_KEY:" \
//    -H "Stripe-Version: 2026-10-28.endive" \
//    -H "Idempotency-Key: {operationId}-line-1" \
//    -d "invoice={invoiceId}" \
//    -d "customer={customerId}" \
//    -d "pricing[price]={priceId}" \
//    -d "quantity=2"
loo_Cmd.SetString("curl -X POST ~"https://api.stripe.com/v1/invoiceitems~"")
loo_Cmd.Append(" -u ~"@KEY@:~"")
loo_Cmd.Append(" -H ~"Stripe-Version: 2026-10-28.endive~"")
loo_Cmd.Append(" -H ~"Idempotency-Key: @OP@-line-1~"")
loo_Cmd.Append(" -d ~"invoice=@INVOICE@~"")
loo_Cmd.Append(" -d ~"customer=@CUSTOMER@~"")
loo_Cmd.Append(" -d ~"pricing[price]=@PRICE@~"")
loo_Cmd.Append(" -d ~"quantity=2~"")
li_Replaced = loo_Cmd.Replace("@INVOICE@",ls_InvoiceId)
li_Replaced = loo_Cmd.Replace("@CUSTOMER@",ls_CustomerId)
li_Replaced = loo_Cmd.Replace("@PRICE@",ls_PriceId)
li_Replaced = loo_Cmd.Replace("@OP@",ls_OperationId)
li_Replaced = loo_Cmd.Replace("@KEY@",ls_ApiKey)
li_Success = loo_Curl.DoYourThing(loo_Cmd.GetAsString())
if li_Success <> 1 then
    Write-Debug "No HTTP response. Chilkat failure code: " + string(loo_Curl.FailReason)
    destroy loo_Curl
    destroy loo_Cmd
    destroy loo_Json
    return
end if

li_Success = loo_Curl.GetResponseJson(loo_Json)
if (loo_Curl.StatusCode <> 200) OR (li_Success <> 1) then
    Write-Debug "Stripe HTTP " + string(loo_Curl.StatusCode) + "  Request-Id: " + loo_Curl.GetResponseHeaderField("Request-Id")
    if li_Success = 1 then
        Write-Debug loo_Json.StringOf("error.message")
    end if

    destroy loo_Curl
    destroy loo_Cmd
    destroy loo_Json
    return
end if

Write-Debug "invoice = " + loo_Json.StringOf("invoice")
Write-Debug "id = " + loo_Json.StringOf("id")
Write-Debug "amount = " + loo_Json.StringOf("amount")
Write-Debug "currency = " + loo_Json.StringOf("currency")


destroy loo_Curl
destroy loo_Cmd
destroy loo_Json