Delphi DLL 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

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
strResponseBody: PWideChar;
jsonResponse: HCkJsonObject;
id: PWideChar;
object: PWideChar;
amount_off: Boolean;
created: Integer;
currency: Boolean;
duration: PWideChar;
duration_in_months: Integer;
livemode: Boolean;
max_redemptions: Boolean;
percent_off: Integer;
redeem_by: Boolean;
times_redeemed: Integer;
valid: Boolean;

begin
rest := CkRest_Create();

//  URL: https://api.stripe.com/v1/coupons
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'api.stripe.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_SetAuthBasic(rest,'STRIPE_SECRET_KEY','');

CkRest_AddQueryParam(rest,'percent_off','25');
CkRest_AddQueryParam(rest,'duration','repeating');
CkRest_AddQueryParam(rest,'duration_in_months','3');
CkRest_AddQueryParam(rest,'id','25OFF');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/v1/coupons');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

id := CkJsonObject__stringOf(jsonResponse,'id');
object := CkJsonObject__stringOf(jsonResponse,'object');
amount_off := CkJsonObject_IsNullOf(jsonResponse,'amount_off');
created := CkJsonObject_IntOf(jsonResponse,'created');
currency := CkJsonObject_IsNullOf(jsonResponse,'currency');
duration := CkJsonObject__stringOf(jsonResponse,'duration');
duration_in_months := CkJsonObject_IntOf(jsonResponse,'duration_in_months');
livemode := CkJsonObject_BoolOf(jsonResponse,'livemode');
max_redemptions := CkJsonObject_IsNullOf(jsonResponse,'max_redemptions');
percent_off := CkJsonObject_IntOf(jsonResponse,'percent_off');
redeem_by := CkJsonObject_IsNullOf(jsonResponse,'redeem_by');
times_redeemed := CkJsonObject_IntOf(jsonResponse,'times_redeemed');
valid := CkJsonObject_BoolOf(jsonResponse,'valid');

CkRest_Dispose(rest);
CkJsonObject_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
}