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

C Example

#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    const char *strResponseBody;
    HCkJsonObject jsonResponse;
    const char *id;
    const char *object;
    const char *cardId;
    const char *cardObject;
    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;
    const char *cardBrand;
    const char *cardCountry;
    BOOL cardCvc_check;
    BOOL cardDynamic_last4;
    int cardExp_month;
    int cardExp_year;
    const char *cardFingerprint;
    const char *cardFunding;
    const char *cardLast4;
    BOOL cardName;
    BOOL cardTokenization_method;
    BOOL client_ip;
    int created;
    BOOL livemode;
    const char *type;
    BOOL used;

    rest = CkRest_Create();

    //  URL: https://api.stripe.com/v1/tokens
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"api.stripe.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    CkRest_SetAuthBasic(rest,"STRIPE_SECRET_KEY","");

    CkRest_AddQueryParam(rest,"card[number]","4242424242424242");
    CkRest_AddQueryParam(rest,"card[exp_month]","12");
    CkRest_AddQueryParam(rest,"card[exp_year]","2019");
    CkRest_AddQueryParam(rest,"card[cvc]","123");

    strResponseBody = CkRest_fullRequestFormUrlEncoded(rest,"POST","/v1/tokens");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

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

    id = CkJsonObject_stringOf(jsonResponse,"id");
    object = CkJsonObject_stringOf(jsonResponse,"object");
    cardId = CkJsonObject_stringOf(jsonResponse,"card.id");
    cardObject = CkJsonObject_stringOf(jsonResponse,"card.object");
    cardAddress_city = CkJsonObject_IsNullOf(jsonResponse,"card.address_city");
    cardAddress_country = CkJsonObject_IsNullOf(jsonResponse,"card.address_country");
    cardAddress_line1 = CkJsonObject_IsNullOf(jsonResponse,"card.address_line1");
    cardAddress_line1_check = CkJsonObject_IsNullOf(jsonResponse,"card.address_line1_check");
    cardAddress_line2 = CkJsonObject_IsNullOf(jsonResponse,"card.address_line2");
    cardAddress_state = CkJsonObject_IsNullOf(jsonResponse,"card.address_state");
    cardAddress_zip = CkJsonObject_IsNullOf(jsonResponse,"card.address_zip");
    cardAddress_zip_check = CkJsonObject_IsNullOf(jsonResponse,"card.address_zip_check");
    cardBrand = CkJsonObject_stringOf(jsonResponse,"card.brand");
    cardCountry = CkJsonObject_stringOf(jsonResponse,"card.country");
    cardCvc_check = CkJsonObject_IsNullOf(jsonResponse,"card.cvc_check");
    cardDynamic_last4 = CkJsonObject_IsNullOf(jsonResponse,"card.dynamic_last4");
    cardExp_month = CkJsonObject_IntOf(jsonResponse,"card.exp_month");
    cardExp_year = CkJsonObject_IntOf(jsonResponse,"card.exp_year");
    cardFingerprint = CkJsonObject_stringOf(jsonResponse,"card.fingerprint");
    cardFunding = CkJsonObject_stringOf(jsonResponse,"card.funding");
    cardLast4 = CkJsonObject_stringOf(jsonResponse,"card.last4");
    cardName = CkJsonObject_IsNullOf(jsonResponse,"card.name");
    cardTokenization_method = CkJsonObject_IsNullOf(jsonResponse,"card.tokenization_method");
    client_ip = CkJsonObject_IsNullOf(jsonResponse,"client_ip");
    created = CkJsonObject_IntOf(jsonResponse,"created");
    livemode = CkJsonObject_BoolOf(jsonResponse,"livemode");
    type = CkJsonObject_stringOf(jsonResponse,"type");
    used = CkJsonObject_BoolOf(jsonResponse,"used");


    CkRest_Dispose(rest);
    CkJsonObject_Dispose(jsonResponse);

    }

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
}