Tcl Stripe: List all Coupons

Back to Index

Returns a list of your coupons.

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

CURL Command

curl https://api.stripe.com/v1/coupons?limit=3 \
   -u STRIPE_SECRET_KEY: \
   -G

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://api.stripe.com/v1/coupons?limit=3
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" ""

set sbResponseBody [new_CkStringBuilder]

set success [CkRest_FullRequestNoBodySb $rest "GET" "/v1/coupons?limit=3" $sbResponseBody]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkStringBuilder $sbResponseBody
    exit
}

set jsonResponse [new_CkJsonObject]

CkJsonObject_LoadSb $jsonResponse $sbResponseBody

set object [CkJsonObject_stringOf $jsonResponse "object"]
set url [CkJsonObject_stringOf $jsonResponse "url"]
set has_more [CkJsonObject_BoolOf $jsonResponse "has_more"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResponse "data"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $jsonResponse $i
    set id [CkJsonObject_stringOf $jsonResponse "data[i].id"]
    set object [CkJsonObject_stringOf $jsonResponse "data[i].object"]
    set amount_off [CkJsonObject_IsNullOf $jsonResponse "data[i].amount_off"]
    set created [CkJsonObject_IntOf $jsonResponse "data[i].created"]
    set currency [CkJsonObject_IsNullOf $jsonResponse "data[i].currency"]
    set duration [CkJsonObject_stringOf $jsonResponse "data[i].duration"]
    set duration_in_months [CkJsonObject_IntOf $jsonResponse "data[i].duration_in_months"]
    set livemode [CkJsonObject_BoolOf $jsonResponse "data[i].livemode"]
    set max_redemptions [CkJsonObject_IsNullOf $jsonResponse "data[i].max_redemptions"]
    set percent_off [CkJsonObject_IntOf $jsonResponse "data[i].percent_off"]
    set redeem_by [CkJsonObject_IsNullOf $jsonResponse "data[i].redeem_by"]
    set times_redeemed [CkJsonObject_IntOf $jsonResponse "data[i].times_redeemed"]
    set valid [CkJsonObject_BoolOf $jsonResponse "data[i].valid"]
    set i [expr $i + 1]
}

delete_CkRest $rest
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jsonResponse

Sample JSON Response Body

{
  "object": "list",
  "url": "/v1/coupons",
  "has_more": false,
  "data": [
    {
      "id": "25OFF",
      "object": "coupon",
      "amount_off": null,
      "created": 1516662783,
      "currency": null,
      "duration": "repeating",
      "duration_in_months": 3,
      "livemode": false,
      "max_redemptions": null,
      "metadata": {},
      "percent_off": 25,
      "redeem_by": null,
      "times_redeemed": 0,
      "valid": true
    }
  ]
}