The customer object is an important component of the Braintree gateway. Use customers to store and organize payment methods. A single customer can have multiple payment methods.

This guide will walk you through the basics. See Customer Request and Customer Response references for complete details.

Create

Use Customer::create() to add new customers.

You can create just a customer without an associated payment method, and the operation will be successful if all customer validations pass:

PHP
Copy
Copied
$result = $gateway->customer()->create([
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'email' => 'mike.jones@example.com',
    'phone' => '281.330.8004',
    'fax' => '419.555.1235',
    'website' => 'http://example.com'
]);

$result->success;
# true

$result->customer->id;
# Generated customer id
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

Create with payment method

You can also create a customer with an associated payment method:

PHP
Copy
Copied
$result = $gateway->customer()->create([
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'paymentMethodNonce' => nonceFromTheClient
]);
if ($result->success) {
    echo($result->customer->id);
    echo($result->customer->paymentMethods[0]->token);
} else {
    foreach($result->errors->deepAll() AS $error) {
        echo($error->code . ": " . $error->message . "\n");
    }
}
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

Success depends on both customer validations and payment method validations, and whether the payment method is verified (if card verification is enabled).

See the reference and more examples of creating a customer.

Update

Use Customer::update() to update an existing customer:

PHP
Copy
Copied
$updateResult = $gateway->customer()->update(
    'a_customer_id',
    [
      'firstName' => 'New First',
      'lastName' => 'New Last',
      'company' => 'New Company',
      'email' => 'new.email@example.com',
      'phone' => 'new phone',
      'fax' => 'new fax',
      'website' => 'http://new.example.com'
    ]
);

$updateResult->success
# true if update was successful
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

If the customer can't be found, you'll receive a Braintree_Exception_NotFound exception.

See the reference and more examples of updating a customer.

Find

Use Customer::find() to find a customer by ID:

PHP
Copy
Copied
$customer = $gateway->customer()->find('a_customer_id');
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

If the customer can't be found, you'll receive a Braintree_Exception_NotFound exception.

The return value of this call will be a Customer response object.

Delete

Use Customer::delete() to delete a customer and its payment methods using the customer's ID:

PHP
Copy
Copied
$result = $gateway->customer()->delete('a_customer_id');

$result->success
#=> true
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

If the customer can't be found, you'll receive a Braintree_Exception_NotFound exception.

Still have questions?

If you can’t find an answer, contact our Support team