C Requires Chilkat v11.7.0+
C
Stripe: Create a Stripe Customer for an Existing Billing Account
See more Stripe Examples
Create a Stripe Customer for a local billing account. Send its internal reference in metadata and read the customer ID to save with that account.
Stripe documentation and cURL example
Chilkat C Downloads
#include <C_CkHttpCurl.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
const char *apiKey;
const char *operationId;
HCkHttpCurl curl;
HCkStringBuilder cmd;
HCkJsonObject json;
int replaced;
HCkStringBuilder businessName;
HCkStringBuilder email;
const char *accountRef;
const char *customerId;
success = FALSE;
// Requires an unlocked Chilkat bundle. See Global Unlock Sample.
// Supply a restricted sandbox key from merchant-controlled configuration.
apiKey = "YOUR_STRIPE_TEST_KEY";
// Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
operationId = "YOUR_SAVED_OPERATION_UUID";
curl = CkHttpCurl_Create();
cmd = CkStringBuilder_Create();
json = CkJsonObject_Create();
// For an account already linked to Stripe, reuse its saved customer ID.
businessName = CkStringBuilder_Create();
CkStringBuilder_SetString(businessName,"Acme & Sons");
email = CkStringBuilder_Create();
CkStringBuilder_SetString(email,"billing+east@example.com");
accountRef = "ACME-1042";
// curl -X POST "https://api.stripe.com/v1/customers" \
// -u "$STRIPE_API_KEY:" \
// -H "Stripe-Version: 2026-10-28.endive" \
// -H "Idempotency-Key: {operationId}-customer" \
// -d "name={businessName}" \
// -d "email={email}" \
// -d "metadata[account_ref]={accountRef}"
CkStringBuilder_SetString(cmd,"curl -X POST \"https://api.stripe.com/v1/customers\"");
CkStringBuilder_Append(cmd," -u \"@KEY@:\"");
CkStringBuilder_Append(cmd," -H \"Stripe-Version: 2026-10-28.endive\"");
CkStringBuilder_Append(cmd," -H \"Idempotency-Key: @OP@-customer\"");
CkStringBuilder_Append(cmd," -d \"name=@NAME@\"");
CkStringBuilder_Append(cmd," -d \"email=@EMAIL@\"");
CkStringBuilder_Append(cmd," -d \"metadata[account_ref]=@REF@\"");
replaced = CkStringBuilder_Replace(cmd,"@NAME@",CkStringBuilder_getEncoded(businessName,"url_rfc3986","utf-8"));
replaced = CkStringBuilder_Replace(cmd,"@EMAIL@",CkStringBuilder_getEncoded(email,"url_rfc3986","utf-8"));
replaced = CkStringBuilder_Replace(cmd,"@REF@",accountRef);
replaced = CkStringBuilder_Replace(cmd,"@OP@",operationId);
replaced = CkStringBuilder_Replace(cmd,"@KEY@",apiKey);
success = CkHttpCurl_DoYourThing(curl,CkStringBuilder_getAsString(cmd));
if (success != TRUE) {
printf("No HTTP response. Chilkat failure code: %d\n",CkHttpCurl_getFailReason(curl));
CkHttpCurl_Dispose(curl);
CkStringBuilder_Dispose(cmd);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(businessName);
CkStringBuilder_Dispose(email);
return;
}
success = CkHttpCurl_GetResponseJson(curl,json);
if ((CkHttpCurl_getStatusCode(curl) != 200) || (success != TRUE)) {
printf("Stripe HTTP %d Request-Id: %s\n",CkHttpCurl_getStatusCode(curl),CkHttpCurl_getResponseHeaderField(curl,"Request-Id"));
if (success == TRUE) {
printf("%s\n",CkJsonObject_stringOf(json,"error.message"));
}
CkHttpCurl_Dispose(curl);
CkStringBuilder_Dispose(cmd);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(businessName);
CkStringBuilder_Dispose(email);
return;
}
// Save this string on the existing billing account after a successful create.
customerId = CkJsonObject_stringOf(json,"id");
printf("Stripe customer: %s\n",customerId);
CkHttpCurl_Dispose(curl);
CkStringBuilder_Dispose(cmd);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(businessName);
CkStringBuilder_Dispose(email);
}