Objective-C Stripe: Create a Card

Back to Index

Creates a new credit card belonging to a customer.

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

CURL Command

curl https://api.stripe.com/v1/customers/cus_CBbg3iRMzWBjoe/sources \
   -u STRIPE_SECRET_KEY: \
   -d source=tok_amex \
   -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/customers/cus_CBbg3iRMzWBjoe/sources
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: @"source" value: @"tok_amex"];

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

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

NSString *id = 0;
NSString *object = 0;
BOOL address_city;
BOOL address_country;
BOOL address_line1;
BOOL address_line1_check;
BOOL address_line2;
BOOL address_state;
BOOL address_zip;
BOOL address_zip_check;
NSString *brand = 0;
NSString *country = 0;
NSString *customer = 0;
BOOL cvc_check;
BOOL dynamic_last4;
int exp_month;
int exp_year;
NSString *fingerprint = 0;
NSString *funding = 0;
NSString *last4 = 0;
BOOL name;
BOOL tokenization_method;

id = [jsonResponse StringOf: @"id"];
object = [jsonResponse StringOf: @"object"];
address_city = [jsonResponse IsNullOf: @"address_city"];
address_country = [jsonResponse IsNullOf: @"address_country"];
address_line1 = [jsonResponse IsNullOf: @"address_line1"];
address_line1_check = [jsonResponse IsNullOf: @"address_line1_check"];
address_line2 = [jsonResponse IsNullOf: @"address_line2"];
address_state = [jsonResponse IsNullOf: @"address_state"];
address_zip = [jsonResponse IsNullOf: @"address_zip"];
address_zip_check = [jsonResponse IsNullOf: @"address_zip_check"];
brand = [jsonResponse StringOf: @"brand"];
country = [jsonResponse StringOf: @"country"];
customer = [jsonResponse StringOf: @"customer"];
cvc_check = [jsonResponse IsNullOf: @"cvc_check"];
dynamic_last4 = [jsonResponse IsNullOf: @"dynamic_last4"];
exp_month = [[jsonResponse IntOf: @"exp_month"] intValue];
exp_year = [[jsonResponse IntOf: @"exp_year"] intValue];
fingerprint = [jsonResponse StringOf: @"fingerprint"];
funding = [jsonResponse StringOf: @"funding"];
last4 = [jsonResponse StringOf: @"last4"];
name = [jsonResponse IsNullOf: @"name"];
tokenization_method = [jsonResponse IsNullOf: @"tokenization_method"];

Sample JSON Response Body

{
  "id": "card_1BnETKGswQrCoh0Xhu1A6BfL",
  "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",
  "customer": "cus_CBbg3iRMzWBjoe",
  "cvc_check": null,
  "dynamic_last4": null,
  "exp_month": 8,
  "exp_year": 2019,
  "fingerprint": "F9mANtIt1TaukpRJ",
  "funding": "credit",
  "last4": "4242",
  "metadata": {},
  "name": null,
  "tokenization_method": null
}