Swift Stripe: Retrieve a Coupon

Back to Index

Retrieves the coupon with the given ID.

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

CURL Command

curl https://api.stripe.com/v1/coupons/25OFF \
   -u STRIPE_SECRET_KEY:

Swift Example


func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //  URL: https://api.stripe.com/v1/coupons/25OFF
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.Connect("api.stripe.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.integerValue)")
        print("\(rest.LastErrorText)")
        return
    }

    rest.SetAuthBasic("STRIPE_SECRET_KEY", password: "")

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/v1/coupons/25OFF", sb: sbResponseBody)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.LoadSb(sbResponseBody)

    var id: String?
    var object: String?
    var amount_off: Bool
    var created: Int
    var currency: Bool
    var duration: String?
    var duration_in_months: Int
    var livemode: Bool
    var max_redemptions: Bool
    var percent_off: Int
    var redeem_by: Bool
    var times_redeemed: Int
    var valid: Bool

    id = jsonResponse.StringOf("id")
    object = jsonResponse.StringOf("object")
    amount_off = jsonResponse.IsNullOf("amount_off")
    created = jsonResponse.IntOf("created").integerValue
    currency = jsonResponse.IsNullOf("currency")
    duration = jsonResponse.StringOf("duration")
    duration_in_months = jsonResponse.IntOf("duration_in_months").integerValue
    livemode = jsonResponse.BoolOf("livemode")
    max_redemptions = jsonResponse.IsNullOf("max_redemptions")
    percent_off = jsonResponse.IntOf("percent_off").integerValue
    redeem_by = jsonResponse.IsNullOf("redeem_by")
    times_redeemed = jsonResponse.IntOf("times_redeemed").integerValue
    valid = jsonResponse.BoolOf("valid")

}

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
}