Searching for Customers

Single Customer

If you want to look up a single customer using its ID, use the find method.

$customer = Braintree_Customer::find('a_customer_id');

If the customer cannot be found, you’ll receive a Braintree_Exception_NotFound exception.

Other Customer Attributes

You can search for customers using a variety of criteria. For operators available on search fields, see the search fields page. For conciseness, we’re going to show searching on several fields at once, but you can search on as many or as few as you would like in a single search.

Customer Fields

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::company()->is("Acme Inc."),
    Braintree_CustomerSearch::email()->is("john.doe@example.com"),
    Braintree_CustomerSearch::fax()->is("555-123-1234"),
    Braintree_CustomerSearch::firstName()->is("John"),
    Braintree_CustomerSearch::id()->is("the_customer_id"),
    Braintree_CustomerSearch::lastName()->is("Doe"),
    Braintree_CustomerSearch::phone()->is("555-321-4321"),
    Braintree_CustomerSearch::website()->is("http://www.example.com")
));

Address Fields

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::addressFirstName()->is("John"),
    Braintree_CustomerSearch::addressLastName()->is("Doe"),
    Braintree_CustomerSearch::addressStreetAddress()->is("111 First St.")
    Braintree_CustomerSearch::addressExtendedAddress()->is("Suite #3"),
    Braintree_CustomerSearch::addressLocality()->is("Chicago"),
    Braintree_CustomerSearch::addressRegion()->is("IL"),
    Braintree_CustomerSearch::addressPostalCode()->is("12345"),
    Braintree_CustomerSearch::addressCountryName()->is("USA")
));

Credit Card Fields

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::cardholderName()->is("John Doe"),
    Braintree_CustomerSearch::paymentMethodToken()->is("the_payment_method_token")
));

Credit Card Number

Searching on credit card number has a few restrictions. If you search using “starts with” you can only enter up to the first 6 digits. If you search using “ends with” you can only enter the last 4 digits. And you can’t search on “contains.”

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::creditCardNumber()->startsWith("411111")
));

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::creditCardNumber()->endsWith("1111")
));

Expiration Date

Expiration date also has restrictions. “is” and “is not” work, while “starts with,” “ends with,” and “contains” do not.

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::creditCardExpirationDate()->is("12/13")
));

$collection = Braintree_Customer::search(array(
    Braintree_CustomerSearch::creditCardExpirationDate()->isNot("12/13")
));

Created At

You can search for customers that were created at a certain time. For instance, you can find all customers which were created in the past 3 days.

An example:

$now = new Datetime();
$past = clone $now;
$past = $past->modify("-1 hour");

$collection = Braintree_Customer::search(array(
  Braintree_CustomerSearch::createdAt()->between($past, $now)
));

All Customers

You can get a list of all customers stored in the vault. This will also return the customers addresses and masked credit card data. Search results are currently capped so this may not work for everybody.

$customers = Braintree_Customer::all();