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, it will raise a Braintree::NotFoundError 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
search_results = Braintree::Customer.search do |search|
search.company.is "Acme Inc."
search.email.is "john.doe@example.com"
search.fax.is "555-123-1234"
search.first_name.is "John"
search.id.is "the_customer_id"
search.last_name.is "Doe"
search.phone.is "555-321-4321"
search.website.is "http://www.example.com"
end
Address Fields
search_results = Braintree::Customer.search do |search|
search.address_first_name.is "John"
search.address_last_name.is "Doe"
search.address_street_address.is "111 First St."
search.address_extended_address.is "Suite #3"
search.address_locality.is "Chicago"
search.address_region.is "IL"
search.address_postal_code.is "12345"
search.address_country_name.is "USA"
end
Credit Card Fields
search_results = Braintree::Customer.search do |search|
search.cardholder_name.is "John Doe"
search.payment_method_token.is "the_payment_method_token"
end
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.”
search_results = Braintree::Customer.search do |search|
search.credit_card_number.starts_with "510510"
end
search_results = Braintree::Customer.search do |search|
search.credit_card_number.ends_with "5100"
end
Expiration Date
Expiration date also has restrictions. “is” and “is not” work, while “starts with,” “ends with,” and “contains” do not.
search_results = Braintree::Customer.search do |search|
search.credit_card_expiration_date.is "12/13"
end
search_results = Braintree::Customer.search do |search|
search.credit_card_expiration_date.is_not "12/13"
end
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:
search_results = Braintree::Customer.search do |search|
search.created_at >= Time.now - 60*60*24 # a day ago
end
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