Swift 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

Swift Example


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

    //  URL: https://api.stripe.com/v1/coupons
    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: "")

    rest.AddQueryParam("percent_off", value: "25")
    rest.AddQueryParam("duration", value: "repeating")
    rest.AddQueryParam("duration_in_months", value: "3")
    rest.AddQueryParam("id", value: "25OFF")

    var strResponseBody: String? = rest.FullRequestFormUrlEncoded("POST", uriPath: "/v1/coupons")
    if rest.LastMethodSuccess != true {
        print("\(rest.LastErrorText)")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.Load(strResponseBody)

    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
}