Unicode C 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

Unicode C Example

#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    const wchar_t *strResponseBody;
    HCkJsonObjectW jsonResponse;
    const wchar_t *id;
    const wchar_t *object;
    BOOL amount_off;
    int created;
    BOOL currency;
    const wchar_t *duration;
    int duration_in_months;
    BOOL livemode;
    BOOL max_redemptions;
    int percent_off;
    BOOL redeem_by;
    int times_redeemed;
    BOOL valid;

    rest = CkRestW_Create();

    //  URL: https://api.stripe.com/v1/coupons
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"api.stripe.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    CkRestW_SetAuthBasic(rest,L"STRIPE_SECRET_KEY",L"");

    CkRestW_AddQueryParam(rest,L"percent_off",L"25");
    CkRestW_AddQueryParam(rest,L"duration",L"repeating");
    CkRestW_AddQueryParam(rest,L"duration_in_months",L"3");
    CkRestW_AddQueryParam(rest,L"id",L"25OFF");

    strResponseBody = CkRestW_fullRequestFormUrlEncoded(rest,L"POST",L"/v1/coupons");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonResponse,strResponseBody);

    id = CkJsonObjectW_stringOf(jsonResponse,L"id");
    object = CkJsonObjectW_stringOf(jsonResponse,L"object");
    amount_off = CkJsonObjectW_IsNullOf(jsonResponse,L"amount_off");
    created = CkJsonObjectW_IntOf(jsonResponse,L"created");
    currency = CkJsonObjectW_IsNullOf(jsonResponse,L"currency");
    duration = CkJsonObjectW_stringOf(jsonResponse,L"duration");
    duration_in_months = CkJsonObjectW_IntOf(jsonResponse,L"duration_in_months");
    livemode = CkJsonObjectW_BoolOf(jsonResponse,L"livemode");
    max_redemptions = CkJsonObjectW_IsNullOf(jsonResponse,L"max_redemptions");
    percent_off = CkJsonObjectW_IntOf(jsonResponse,L"percent_off");
    redeem_by = CkJsonObjectW_IsNullOf(jsonResponse,L"redeem_by");
    times_redeemed = CkJsonObjectW_IntOf(jsonResponse,L"times_redeemed");
    valid = CkJsonObjectW_BoolOf(jsonResponse,L"valid");


    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(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
}