Objective-C Stripe: List all Coupons

Back to Index

Returns a list of your coupons.

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

CURL Command

curl https://api.stripe.com/v1/coupons?limit=3 \
   -u STRIPE_SECRET_KEY: \
   -G

Objective-C Example

#import <CkoRest.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//  URL: https://api.stripe.com/v1/coupons?limit=3
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"api.stripe.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

[rest SetAuthBasic: @"STRIPE_SECRET_KEY" password: @""];

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/v1/coupons?limit=3" sb: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse LoadSb: sbResponseBody];

NSString *object = 0;
NSString *url = 0;
BOOL has_more;
int i;
int count_i;
NSString *id = 0;
BOOL amount_off;
int created;
BOOL currency;
NSString *duration = 0;
int duration_in_months;
BOOL livemode;
BOOL max_redemptions;
int percent_off;
BOOL redeem_by;
int times_redeemed;
BOOL valid;

object = [jsonResponse StringOf: @"object"];
url = [jsonResponse StringOf: @"url"];
has_more = [jsonResponse BoolOf: @"has_more"];
i = 0;
count_i = [[jsonResponse SizeOfArray: @"data"] intValue];
while (i < count_i) {
    jsonResponse.I = [NSNumber numberWithInt: i];
    id = [jsonResponse StringOf: @"data[i].id"];
    object = [jsonResponse StringOf: @"data[i].object"];
    amount_off = [jsonResponse IsNullOf: @"data[i].amount_off"];
    created = [[jsonResponse IntOf: @"data[i].created"] intValue];
    currency = [jsonResponse IsNullOf: @"data[i].currency"];
    duration = [jsonResponse StringOf: @"data[i].duration"];
    duration_in_months = [[jsonResponse IntOf: @"data[i].duration_in_months"] intValue];
    livemode = [jsonResponse BoolOf: @"data[i].livemode"];
    max_redemptions = [jsonResponse IsNullOf: @"data[i].max_redemptions"];
    percent_off = [[jsonResponse IntOf: @"data[i].percent_off"] intValue];
    redeem_by = [jsonResponse IsNullOf: @"data[i].redeem_by"];
    times_redeemed = [[jsonResponse IntOf: @"data[i].times_redeemed"] intValue];
    valid = [jsonResponse BoolOf: @"data[i].valid"];
    i = i + 1;
}

Sample JSON Response Body

{
  "object": "list",
  "url": "/v1/coupons",
  "has_more": false,
  "data": [
    {
      "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
    }
  ]
}