Creating Customers

You can create a customer by itself, with a credit card, or with a credit card with a billing address.

This page describes creating customers using the server-to-server API. Customers can also be created using Transparent Redirect. The server-to-server API is easier to understand, so even if you’re planning to use TR, you may want to read this page first.

Just a Customer

Here is an example of creating a customer without credit card information. If all customer validations pass, success will be true.

$result = Braintree_Customer::create(array(
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'email' => 'mike.jones@example.com',
    'phone' => '419.555.1234',
    'fax' => '419.555.1235',
    'website' => 'http://example.com'
));

$result->success;
# true

$result->customer->id;
# Generated customer id

Specify Your Own Customer ID

If you do not specify the customer ID, as in the example above, the gateway will generate one. You can also choose what you would like the ID to be.

$result = Braintree_Customer::create(array(
    'id' => 'customer_123',
    'firstName' => 'Mike',
));

$result->success
# true

$result->customer->id
# Generated customer id

Blank Customer

None of the parameters are required, so if you’re only interested in storing credit card details without any customer information, you can create a blank customer.

$result = Braintree_Customer::create();

$result->success
# true

$result->customer->id
# Generated customer id

Customer with Credit Card

You can also create a credit card along with a customer. If all customer validations and credit card validations pass, and the card is verified (if requested), success will return true.

$result = Braintree_Customer::create(array(
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'creditCard' => array(
        'number' => '5105105105105100',
        'expirationDate' => '05/12',
        'cvv' => '123'
    )
));
if ($result->success) {
    echo($result->customer->id);
    echo($result->customer->creditCards[0]->token);
} else {
    foreach($result->errors->deepAll() AS $error) {
        echo($error->code . ": " . $error->message . "\n");
    }
}

If you do not specify a token for the credit card, as in the example above, the gateway will generate one. You can also choose what you want the token to be.

$result = Braintree_Customer::create(array(
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'creditCard' => array(
        'number' => '5105105105105100',
        'expirationDate' => '05/12',
        'cvv' => '123',
        'token' => 'a_token'
    )
));

$result->success # => true
$result->customer->creditCards[0]->token # => "a_token"

By default, the credit card number is not verified to be a valid, open account. If you want to check for that, use the card verification API.

Customer with Credit Card with Billing Address

You can also create a credit card with billing address along with a customer.

$result = Braintree_Customer::create(array(
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'email' => 'mike.jones@example.com',
    'phone' => '419.555.1234',
    'fax' => '419.555.1235',
    'website' => 'http://example.com',
    'creditCard' => array(
        'number' => '5105105105105100',
        'expirationDate' => '05/12',
        'cvv' => '123',
        'cardholderName' => 'Mike Jones',
        'billingAddress' => array(
            'firstName' => 'Drew',
            'lastName' => 'Smith',
            'company' => 'Smith Co.',
            'streetAddress' => '1 E Main St',
            'extendedAddress' => 'Suite 101',
            'locality' => 'Chicago',
            'region' => 'IL',
            'postalCode' => '60622',
            'countryCodeAlpha2' => 'US'
        )
    )
));

$customer = $result->customer;
$creditCard = $customer->creditCards[0];
$creditCard->bin
# '510510'
$creditCard->last4
# '5100'

# The same address, two different ways
$address = $customer->addresses[0];
$creditCard->billingAddress

$address->firstName
# 'Drew'
$address->lastName
# 'Smith'

See Also