Unicode 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

Unicode C++ Example

#include <CkRestW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://api.stripe.com/v1/tokens
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"api.stripe.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.SetAuthBasic(L"STRIPE_SECRET_KEY",L"");

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

    const wchar_t *strResponseBody = rest.fullRequestFormUrlEncoded(L"POST",L"/v1/tokens");
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.Load(strResponseBody);

    const wchar_t *id = 0;
    const wchar_t *object = 0;
    const wchar_t *cardId = 0;
    const wchar_t *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;
    const wchar_t *cardBrand = 0;
    const wchar_t *cardCountry = 0;
    bool cardCvc_check;
    bool cardDynamic_last4;
    int cardExp_month;
    int cardExp_year;
    const wchar_t *cardFingerprint = 0;
    const wchar_t *cardFunding = 0;
    const wchar_t *cardLast4 = 0;
    bool cardName;
    bool cardTokenization_method;
    bool client_ip;
    int created;
    bool livemode;
    const wchar_t *type = 0;
    bool used;

    id = jsonResponse.stringOf(L"id");
    object = jsonResponse.stringOf(L"object");
    cardId = jsonResponse.stringOf(L"card.id");
    cardObject = jsonResponse.stringOf(L"card.object");
    cardAddress_city = jsonResponse.IsNullOf(L"card.address_city");
    cardAddress_country = jsonResponse.IsNullOf(L"card.address_country");
    cardAddress_line1 = jsonResponse.IsNullOf(L"card.address_line1");
    cardAddress_line1_check = jsonResponse.IsNullOf(L"card.address_line1_check");
    cardAddress_line2 = jsonResponse.IsNullOf(L"card.address_line2");
    cardAddress_state = jsonResponse.IsNullOf(L"card.address_state");
    cardAddress_zip = jsonResponse.IsNullOf(L"card.address_zip");
    cardAddress_zip_check = jsonResponse.IsNullOf(L"card.address_zip_check");
    cardBrand = jsonResponse.stringOf(L"card.brand");
    cardCountry = jsonResponse.stringOf(L"card.country");
    cardCvc_check = jsonResponse.IsNullOf(L"card.cvc_check");
    cardDynamic_last4 = jsonResponse.IsNullOf(L"card.dynamic_last4");
    cardExp_month = jsonResponse.IntOf(L"card.exp_month");
    cardExp_year = jsonResponse.IntOf(L"card.exp_year");
    cardFingerprint = jsonResponse.stringOf(L"card.fingerprint");
    cardFunding = jsonResponse.stringOf(L"card.funding");
    cardLast4 = jsonResponse.stringOf(L"card.last4");
    cardName = jsonResponse.IsNullOf(L"card.name");
    cardTokenization_method = jsonResponse.IsNullOf(L"card.tokenization_method");
    client_ip = jsonResponse.IsNullOf(L"client_ip");
    created = jsonResponse.IntOf(L"created");
    livemode = jsonResponse.BoolOf(L"livemode");
    type = jsonResponse.stringOf(L"type");
    used = jsonResponse.BoolOf(L"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
}