Visual FoxPro 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

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loSbResponseBody
LOCAL loJsonResponse
LOCAL lcObject
LOCAL lcUrl
LOCAL lnHas_more
LOCAL i
LOCAL lnCount_i
LOCAL lcId
LOCAL lnAmount_off
LOCAL lnCreated
LOCAL lnCurrency
LOCAL lcDuration
LOCAL lnDuration_in_months
LOCAL lnLivemode
LOCAL lnMax_redemptions
LOCAL lnPercent_off
LOCAL lnRedeem_by
LOCAL lnTimes_redeemed
LOCAL lnValid

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  URL: https://api.stripe.com/v1/coupons?limit=3
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("api.stripe.com",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? "ConnectFailReason: " + STR(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

loRest.SetAuthBasic("STRIPE_SECRET_KEY","")

loSbResponseBody = CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess = loRest.FullRequestNoBodySb("GET","/v1/coupons?limit=3",loSbResponseBody)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loSbResponseBody
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonObject')
loJsonResponse.LoadSb(loSbResponseBody)

lcObject = loJsonResponse.StringOf("object")
lcUrl = loJsonResponse.StringOf("url")
lnHas_more = loJsonResponse.BoolOf("has_more")
i = 0
lnCount_i = loJsonResponse.SizeOfArray("data")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    lcId = loJsonResponse.StringOf("data[i].id")
    lcObject = loJsonResponse.StringOf("data[i].object")
    lnAmount_off = loJsonResponse.IsNullOf("data[i].amount_off")
    lnCreated = loJsonResponse.IntOf("data[i].created")
    lnCurrency = loJsonResponse.IsNullOf("data[i].currency")
    lcDuration = loJsonResponse.StringOf("data[i].duration")
    lnDuration_in_months = loJsonResponse.IntOf("data[i].duration_in_months")
    lnLivemode = loJsonResponse.BoolOf("data[i].livemode")
    lnMax_redemptions = loJsonResponse.IsNullOf("data[i].max_redemptions")
    lnPercent_off = loJsonResponse.IntOf("data[i].percent_off")
    lnRedeem_by = loJsonResponse.IsNullOf("data[i].redeem_by")
    lnTimes_redeemed = loJsonResponse.IntOf("data[i].times_redeemed")
    lnValid = loJsonResponse.BoolOf("data[i].valid")
    i = i + 1
ENDDO

RELEASE loRest
RELEASE loSbResponseBody
RELEASE loJsonResponse

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
    }
  ]
}