Objective-C Stripe: Create a Card Token

Back to Index

Creates a single use token that wraps the details of a credit card. This token can be used in place of a credit card dictionary with any API method.

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

CURL Command

curl https://api.stripe.com/v1/tokens \
   -u STRIPE_SECRET_KEY: \
   -d card[number]=4242424242424242 \
   -d card[exp_month]=12 \
   -d card[exp_year]=2019 \
   -d card[cvc]=123
   -X POST

Objective-C Example

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

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

//  URL: https://api.stripe.com/v1/tokens
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: @""];

[rest AddQueryParam: @"card[number]" value: @"4242424242424242"];
[rest AddQueryParam: @"card[exp_month]" value: @"12"];
[rest AddQueryParam: @"card[exp_year]" value: @"2019"];
[rest AddQueryParam: @"card[cvc]" value: @"123"];

NSString *strResponseBody = [rest FullRequestFormUrlEncoded: @"POST" uriPath: @"/v1/tokens"];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse Load: strResponseBody];

NSString *id = 0;
NSString *object = 0;
NSString *cardId = 0;
NSString *cardObject = 0;
BOOL cardAddress_city;
BOOL cardAddress_country;
BOOL cardAddress_line1;
BOOL cardAddress_line1_check;
BOOL cardAddress_line2;
BOOL cardAddress_state;
BOOL cardAddress_zip;
BOOL cardAddress_zip_check;
NSString *cardBrand = 0;
NSString *cardCountry = 0;
BOOL cardCvc_check;
BOOL cardDynamic_last4;
int cardExp_month;
int cardExp_year;
NSString *cardFingerprint = 0;
NSString *cardFunding = 0;
NSString *cardLast4 = 0;
BOOL cardName;
BOOL cardTokenization_method;
BOOL client_ip;
int created;
BOOL livemode;
NSString *type = 0;
BOOL used;

id = [jsonResponse StringOf: @"id"];
object = [jsonResponse StringOf: @"object"];
cardId = [jsonResponse StringOf: @"card.id"];
cardObject = [jsonResponse StringOf: @"card.object"];
cardAddress_city = [jsonResponse IsNullOf: @"card.address_city"];
cardAddress_country = [jsonResponse IsNullOf: @"card.address_country"];
cardAddress_line1 = [jsonResponse IsNullOf: @"card.address_line1"];
cardAddress_line1_check = [jsonResponse IsNullOf: @"card.address_line1_check"];
cardAddress_line2 = [jsonResponse IsNullOf: @"card.address_line2"];
cardAddress_state = [jsonResponse IsNullOf: @"card.address_state"];
cardAddress_zip = [jsonResponse IsNullOf: @"card.address_zip"];
cardAddress_zip_check = [jsonResponse IsNullOf: @"card.address_zip_check"];
cardBrand = [jsonResponse StringOf: @"card.brand"];
cardCountry = [jsonResponse StringOf: @"card.country"];
cardCvc_check = [jsonResponse IsNullOf: @"card.cvc_check"];
cardDynamic_last4 = [jsonResponse IsNullOf: @"card.dynamic_last4"];
cardExp_month = [[jsonResponse IntOf: @"card.exp_month"] intValue];
cardExp_year = [[jsonResponse IntOf: @"card.exp_year"] intValue];
cardFingerprint = [jsonResponse StringOf: @"card.fingerprint"];
cardFunding = [jsonResponse StringOf: @"card.funding"];
cardLast4 = [jsonResponse StringOf: @"card.last4"];
cardName = [jsonResponse IsNullOf: @"card.name"];
cardTokenization_method = [jsonResponse IsNullOf: @"card.tokenization_method"];
client_ip = [jsonResponse IsNullOf: @"client_ip"];
created = [[jsonResponse IntOf: @"created"] intValue];
livemode = [jsonResponse BoolOf: @"livemode"];
type = [jsonResponse StringOf: @"type"];
used = [jsonResponse BoolOf: @"used"];

Sample JSON Response Body

{
  "id": "tok_1BnETKGswQrCoh0XIvDUT3mu",
  "object": "token",
  "card": {
    "id": "card_1BnETKGswQrCoh0XGXCaLCT2",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "cvc_check": null,
    "dynamic_last4": null,
    "exp_month": 8,
    "exp_year": 2019,
    "fingerprint": "F9mANtIt1TaukpRJ",
    "funding": "credit",
    "last4": "4242",
    "metadata": {},
    "name": null,
    "tokenization_method": null
  },
  "client_ip": null,
  "created": 1516662782,
  "livemode": false,
  "type": "card",
  "used": false
}