Stripe, is it possible to search a customer by their email?

Stripe now allows you to filter customers by email.

https://stripe.com/docs/api#list_customers

Map<String, Object> options = new HashMap<>();
options.put("email", email);
List<Customer> customers = Customer.list(options).getData();

if (customers.size() > 0) {
    Customer customer = customers.get(0);
    ...

This is important to help ensure you don't create duplicate customers. Because you can't put creating a customer in Stripe and the storage of the Stripe customer ID in your system inside a single transaction you need to build in some fail safes that check to see if a particular customer exists before you create a new one. Searching customers by email is important in that regard.


I did this by using the following API request. This was not available in stripe docs.I got this by tracking down their search in the dashboard area using Browser Developer Tools.

    url :https://api.stripe.com/v1/search?query="+email+"&prefix=false",
    method: GET
    headers: {
      "authorization": "Bearer Your_seceret Key",
      "content-type": "application/x-www-form-urlencoded",
    }

Warning This uses an undocumented API that is specific to the dashboard. While it might work today, there is no guarantee it will continue to work in the future.


You need to retrieve and store the Stripe customer ID along with the other customer details in your database. You can then search for the email address in your database and retrieve the customer record from Stripe by using the Stripe customer ID.


UPDATE: Stripe now allows searching via email

https://stripe.com/docs/api/php#list_customers

/**
 * Remember that Stripe unfortunately allows multiple customers to have the same email address.
 * @see https://stackoverflow.com/a/38492724/470749
 * 
 * @param string $emailAddress
 * @return array
 */
public function getCustomersByEmailAddress($emailAddress) {
    try {
        $matchingCustomers = [];
        $lastResult = null;
        $hasMoreResults = true;
        while ($hasMoreResults) {
            $searchResults = \Stripe\Customer::all([
                        "email" => $emailAddress,
                        "limit" => 100,
                        "starting_after" => $lastResult
            ]);
            $hasMoreResults = $searchResults->has_more;
            foreach ($searchResults->autoPagingIterator() as $customer) {
                $matchingCustomers[] = $customer;
            }
            $lastResult = end($searchResults->data);
        }
        return $matchingCustomers;
    } catch (\Exception $e) {
        Log::error($e);
        return [];
    }
}