Visual FoxPro Stripe: Update a Coupon

Back to Index

Updates the metadata of a coupon. Other coupon details (currency, duration, amount_off) are, by design, not editable.

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

CURL Command

curl https://api.stripe.com/v1/coupons/25OFF \
   -u STRIPE_SECRET_KEY: \
   -d metadata[order_id]=6735 \
   -X POST

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL lcStrResponseBody
LOCAL loJsonResponse
LOCAL lcId
LOCAL lcObject
LOCAL lnAmount_off
LOCAL lnCreated
LOCAL lnCurrency
LOCAL lcDuration
LOCAL lnDuration_in_months
LOCAL lnLivemode
LOCAL lnMax_redemptions
LOCAL lcMetadataOrder_id
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/25OFF
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","")

loRest.AddQueryParam("metadata[order_id]","6735")

lcStrResponseBody = loRest.FullRequestFormUrlEncoded("POST","/v1/coupons/25OFF")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonObject')
loJsonResponse.Load(lcStrResponseBody)

lcId = loJsonResponse.StringOf("id")
lcObject = loJsonResponse.StringOf("object")
lnAmount_off = loJsonResponse.IsNullOf("amount_off")
lnCreated = loJsonResponse.IntOf("created")
lnCurrency = loJsonResponse.IsNullOf("currency")
lcDuration = loJsonResponse.StringOf("duration")
lnDuration_in_months = loJsonResponse.IntOf("duration_in_months")
lnLivemode = loJsonResponse.BoolOf("livemode")
lnMax_redemptions = loJsonResponse.IsNullOf("max_redemptions")
lcMetadataOrder_id = loJsonResponse.StringOf("metadata.order_id")
lnPercent_off = loJsonResponse.IntOf("percent_off")
lnRedeem_by = loJsonResponse.IsNullOf("redeem_by")
lnTimes_redeemed = loJsonResponse.IntOf("times_redeemed")
lnValid = loJsonResponse.BoolOf("valid")

RELEASE loRest
RELEASE loJsonResponse

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": {
    "order_id": "6735"
  },
  "percent_off": 25,
  "redeem_by": null,
  "times_redeemed": 0,
  "valid": true
}