PHP Extension 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

PHP Extension Example

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

$rest = new CkRest();

//  URL: https://api.stripe.com/v1/customers
$bTls = true;
$port = 443;
$bAutoReconnect = true;
$success = $rest->Connect('api.stripe.com',$port,$bTls,$bAutoReconnect);
if ($success != true) {
    print 'ConnectFailReason: ' . $rest->get_ConnectFailReason() . "\n";
    print $rest->lastErrorText() . "\n";
    exit;
}

$rest->SetAuthBasic('STRIPE_SECRET_KEY','');

$rest->AddQueryParam('description','Customer for isabella.williams@example.com');
$rest->AddQueryParam('source','tok_amex');

$strResponseBody = $rest->fullRequestFormUrlEncoded('POST','/v1/customers');
if ($rest->get_LastMethodSuccess() != true) {
    print $rest->lastErrorText() . "\n";
    exit;
}

$jsonResponse = new CkJsonObject();
$jsonResponse->Load($strResponseBody);

$id = $jsonResponse->stringOf('id');
$object = $jsonResponse->stringOf('object');
$account_balance = $jsonResponse->IntOf('account_balance');
$created = $jsonResponse->IntOf('created');
$currency = $jsonResponse->stringOf('currency');
$default_source = $jsonResponse->IsNullOf('default_source');
$delinquent = $jsonResponse->BoolOf('delinquent');
$description = $jsonResponse->IsNullOf('description');
$discount = $jsonResponse->IsNullOf('discount');
$email = $jsonResponse->IsNullOf('email');
$livemode = $jsonResponse->BoolOf('livemode');
$shipping = $jsonResponse->IsNullOf('shipping');
$sourcesObject = $jsonResponse->stringOf('sources.object');
$sourcesHas_more = $jsonResponse->BoolOf('sources.has_more');
$sourcesTotal_count = $jsonResponse->IntOf('sources.total_count');
$sourcesUrl = $jsonResponse->stringOf('sources.url');
$subscriptionsObject = $jsonResponse->stringOf('subscriptions.object');
$subscriptionsHas_more = $jsonResponse->BoolOf('subscriptions.has_more');
$subscriptionsTotal_count = $jsonResponse->IntOf('subscriptions.total_count');
$subscriptionsUrl = $jsonResponse->stringOf('subscriptions.url');
$i = 0;
$count_i = $jsonResponse->SizeOfArray('sources.data');
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}

$i = 0;
$count_i = $jsonResponse->SizeOfArray('subscriptions.data');
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $i = $i + 1;
}


?>

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"
  }
}