C Stripe: Create a Customer

Back to Index

Creates a new customer object.

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

CURL Command

curl -X POST https://api.stripe.com/v1/customers \
   -u STRIPE_SECRET_KEY: \
   -d description="Customer for isabella.williams@example.com" \
   -d source=tok_amex

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;
    int account_balance;
    int created;
    const char *currency;
    BOOL default_source;
    BOOL delinquent;
    BOOL description;
    BOOL discount;
    BOOL email;
    BOOL livemode;
    BOOL shipping;
    const char *sourcesObject;
    BOOL sourcesHas_more;
    int sourcesTotal_count;
    const char *sourcesUrl;
    const char *subscriptionsObject;
    BOOL subscriptionsHas_more;
    int subscriptionsTotal_count;
    const char *subscriptionsUrl;
    int i;
    int count_i;

    rest = CkRest_Create();

    //  URL: https://api.stripe.com/v1/customers
    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,"description","Customer for isabella.williams@example.com");
    CkRest_AddQueryParam(rest,"source","tok_amex");

    strResponseBody = CkRest_fullRequestFormUrlEncoded(rest,"POST","/v1/customers");
    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");
    account_balance = CkJsonObject_IntOf(jsonResponse,"account_balance");
    created = CkJsonObject_IntOf(jsonResponse,"created");
    currency = CkJsonObject_stringOf(jsonResponse,"currency");
    default_source = CkJsonObject_IsNullOf(jsonResponse,"default_source");
    delinquent = CkJsonObject_BoolOf(jsonResponse,"delinquent");
    description = CkJsonObject_IsNullOf(jsonResponse,"description");
    discount = CkJsonObject_IsNullOf(jsonResponse,"discount");
    email = CkJsonObject_IsNullOf(jsonResponse,"email");
    livemode = CkJsonObject_BoolOf(jsonResponse,"livemode");
    shipping = CkJsonObject_IsNullOf(jsonResponse,"shipping");
    sourcesObject = CkJsonObject_stringOf(jsonResponse,"sources.object");
    sourcesHas_more = CkJsonObject_BoolOf(jsonResponse,"sources.has_more");
    sourcesTotal_count = CkJsonObject_IntOf(jsonResponse,"sources.total_count");
    sourcesUrl = CkJsonObject_stringOf(jsonResponse,"sources.url");
    subscriptionsObject = CkJsonObject_stringOf(jsonResponse,"subscriptions.object");
    subscriptionsHas_more = CkJsonObject_BoolOf(jsonResponse,"subscriptions.has_more");
    subscriptionsTotal_count = CkJsonObject_IntOf(jsonResponse,"subscriptions.total_count");
    subscriptionsUrl = CkJsonObject_stringOf(jsonResponse,"subscriptions.url");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jsonResponse,"sources.data");
    while (i < count_i) {
        CkJsonObject_putI(jsonResponse,i);
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jsonResponse,"subscriptions.data");
    while (i < count_i) {
        CkJsonObject_putI(jsonResponse,i);
        i = i + 1;
    }



    CkRest_Dispose(rest);
    CkJsonObject_Dispose(jsonResponse);

    }

Sample JSON Response Body

{
  "id": "cus_CBbgVLJqv487Oq",
  "object": "customer",
  "account_balance": 0,
  "created": 1516662781,
  "currency": "usd",
  "default_source": null,
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "livemode": false,
  "metadata": {},
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [
    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_CBbgVLJqv487Oq/sources"
  },
  "subscriptions": {
    "object": "list",
    "data": [
    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_CBbgVLJqv487Oq/subscriptions"
  }
}