Tcl Stripe: Create an Invoice

Back to Index

Creates a customer invoice object.

Documentation: https://stripe.com/docs/api/curl#create_invoice

CURL Command

curl https://api.stripe.com/v1/invoices \
   -u STRIPE_SECRET_KEY: \
   -d customer=cus_CBbg9PmQ9sLbmo \
   -X POST

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://api.stripe.com/v1/invoices
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "api.stripe.com" $port $bTls $bAutoReconnect]
if {[expr $success != 1]} then {
    puts "ConnectFailReason: [CkRest_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

CkRest_SetAuthBasic $rest "STRIPE_SECRET_KEY" ""

CkRest_AddQueryParam $rest "customer" "cus_CBbg9PmQ9sLbmo"

set strResponseBody [CkRest_fullRequestFormUrlEncoded $rest "POST" "/v1/invoices"]
if {[expr [CkRest_LastMethodSuccess $rest] != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

set jsonResponse [new_CkJsonObject]

CkJsonObject_Load $jsonResponse $strResponseBody

set id [CkJsonObject_stringOf $jsonResponse "id"]
set object [CkJsonObject_stringOf $jsonResponse "object"]
set amount_due [CkJsonObject_IntOf $jsonResponse "amount_due"]
set application_fee [CkJsonObject_IsNullOf $jsonResponse "application_fee"]
set attempt_count [CkJsonObject_IntOf $jsonResponse "attempt_count"]
set attempted [CkJsonObject_BoolOf $jsonResponse "attempted"]
set billing [CkJsonObject_stringOf $jsonResponse "billing"]
set charge [CkJsonObject_IsNullOf $jsonResponse "charge"]
set closed [CkJsonObject_BoolOf $jsonResponse "closed"]
set currency [CkJsonObject_stringOf $jsonResponse "currency"]
set customer [CkJsonObject_stringOf $jsonResponse "customer"]
set date [CkJsonObject_IntOf $jsonResponse "date"]
set description [CkJsonObject_IsNullOf $jsonResponse "description"]
set discount [CkJsonObject_IsNullOf $jsonResponse "discount"]
set due_date [CkJsonObject_IsNullOf $jsonResponse "due_date"]
set ending_balance [CkJsonObject_IsNullOf $jsonResponse "ending_balance"]
set forgiven [CkJsonObject_BoolOf $jsonResponse "forgiven"]
set linesHas_more [CkJsonObject_BoolOf $jsonResponse "lines.has_more"]
set linesObject [CkJsonObject_stringOf $jsonResponse "lines.object"]
set linesUrl [CkJsonObject_stringOf $jsonResponse "lines.url"]
set livemode [CkJsonObject_BoolOf $jsonResponse "livemode"]
set next_payment_attempt [CkJsonObject_IntOf $jsonResponse "next_payment_attempt"]
set number [CkJsonObject_stringOf $jsonResponse "number"]
set paid [CkJsonObject_BoolOf $jsonResponse "paid"]
set period_end [CkJsonObject_IntOf $jsonResponse "period_end"]
set period_start [CkJsonObject_IntOf $jsonResponse "period_start"]
set receipt_number [CkJsonObject_IsNullOf $jsonResponse "receipt_number"]
set starting_balance [CkJsonObject_IntOf $jsonResponse "starting_balance"]
set statement_descriptor [CkJsonObject_IsNullOf $jsonResponse "statement_descriptor"]
set subscription [CkJsonObject_IsNullOf $jsonResponse "subscription"]
set subtotal [CkJsonObject_IntOf $jsonResponse "subtotal"]
set tax [CkJsonObject_IsNullOf $jsonResponse "tax"]
set tax_percent [CkJsonObject_IsNullOf $jsonResponse "tax_percent"]
set total [CkJsonObject_IntOf $jsonResponse "total"]
set webhooks_delivered_at [CkJsonObject_IsNullOf $jsonResponse "webhooks_delivered_at"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResponse "lines.data"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $jsonResponse $i
    set id [CkJsonObject_stringOf $jsonResponse "lines.data[i].id"]
    set object [CkJsonObject_stringOf $jsonResponse "lines.data[i].object"]
    set amount [CkJsonObject_IntOf $jsonResponse "lines.data[i].amount"]
    set currency [CkJsonObject_stringOf $jsonResponse "lines.data[i].currency"]
    set description_str [CkJsonObject_stringOf $jsonResponse "lines.data[i].description"]
    set discountable [CkJsonObject_BoolOf $jsonResponse "lines.data[i].discountable"]
    set livemode [CkJsonObject_BoolOf $jsonResponse "lines.data[i].livemode"]
    set periodStart [CkJsonObject_IntOf $jsonResponse "lines.data[i].period.start"]
    set periodEnd [CkJsonObject_IntOf $jsonResponse "lines.data[i].period.end"]
    set plan [CkJsonObject_IsNullOf $jsonResponse "lines.data[i].plan"]
    set proration [CkJsonObject_BoolOf $jsonResponse "lines.data[i].proration"]
    set quantity [CkJsonObject_IsNullOf $jsonResponse "lines.data[i].quantity"]
    set subscription [CkJsonObject_IsNullOf $jsonResponse "lines.data[i].subscription"]
    set type [CkJsonObject_stringOf $jsonResponse "lines.data[i].type"]
    set i [expr $i + 1]
}

delete_CkRest $rest
delete_CkJsonObject $jsonResponse

Sample JSON Response Body

{
  "id": "in_1BnETLGswQrCoh0XR7p1RCtV",
  "object": "invoice",
  "amount_due": 0,
  "application_fee": null,
  "attempt_count": 0,
  "attempted": false,
  "billing": "charge_automatically",
  "charge": null,
  "closed": false,
  "currency": "usd",
  "customer": "cus_CBbg9PmQ9sLbmo",
  "date": 1516662783,
  "description": null,
  "discount": null,
  "due_date": null,
  "ending_balance": null,
  "forgiven": false,
  "lines": {
    "data": [
      {
        "id": "ii_1BnETLGswQrCoh0XsAcSzoLf",
        "object": "line_item",
        "amount": 0,
        "currency": "usd",
        "description": "My First Invoice Item (created for API docs)",
        "discountable": true,
        "livemode": false,
        "metadata": {},
        "period": {
          "start": 1516662783,
          "end": 1516662783
        },
        "plan": null,
        "proration": false,
        "quantity": null,
        "subscription": null,
        "type": "invoiceitem"
      }
    ],
    "has_more": false,
    "object": "list",
    "url": "/v1/invoices/in_1BnETLGswQrCoh0XR7p1RCtV/lines"
  },
  "livemode": false,
  "metadata": {},
  "next_payment_attempt": 1516666383,
  "number": "36eba31eaa-0001",
  "paid": false,
  "period_end": 1516662783,
  "period_start": 1516662783,
  "receipt_number": null,
  "starting_balance": 0,
  "statement_descriptor": null,
  "subscription": null,
  "subtotal": 0,
  "tax": null,
  "tax_percent": null,
  "total": 0,
  "webhooks_delivered_at": null
}