Tcl Stripe: Create a Coupon

Back to Index

Creates a coupon object.

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

CURL Command

curl https://api.stripe.com/v1/coupons \
   -u STRIPE_SECRET_KEY: \
   -d percent_off=25 \
   -d duration=repeating \
   -d duration_in_months=3 \
   -d id=25OFF \
   -X POST

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://api.stripe.com/v1/coupons
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 "percent_off" "25"
CkRest_AddQueryParam $rest "duration" "repeating"
CkRest_AddQueryParam $rest "duration_in_months" "3"
CkRest_AddQueryParam $rest "id" "25OFF"

set strResponseBody [CkRest_fullRequestFormUrlEncoded $rest "POST" "/v1/coupons"]
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_off [CkJsonObject_IsNullOf $jsonResponse "amount_off"]
set created [CkJsonObject_IntOf $jsonResponse "created"]
set currency [CkJsonObject_IsNullOf $jsonResponse "currency"]
set duration [CkJsonObject_stringOf $jsonResponse "duration"]
set duration_in_months [CkJsonObject_IntOf $jsonResponse "duration_in_months"]
set livemode [CkJsonObject_BoolOf $jsonResponse "livemode"]
set max_redemptions [CkJsonObject_IsNullOf $jsonResponse "max_redemptions"]
set percent_off [CkJsonObject_IntOf $jsonResponse "percent_off"]
set redeem_by [CkJsonObject_IsNullOf $jsonResponse "redeem_by"]
set times_redeemed [CkJsonObject_IntOf $jsonResponse "times_redeemed"]
set valid [CkJsonObject_BoolOf $jsonResponse "valid"]

delete_CkRest $rest
delete_CkJsonObject $jsonResponse

Sample JSON Response Body

{
  "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
}