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

前端 未结 12 1418
暗喜
暗喜 2020-12-08 06:32

Update: Since around January 2018, it is now possible to search using the email parameter on Stripe. See the accepted answer.


I

相关标签:
12条回答
  • 2020-12-08 06:40

    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 [];
        }
    }
    
    0 讨论(0)
  • 2020-12-08 06:40

    Since you specified that

    The documentation only indicate to search by created, ending_before, limit and starting_after, but no "email".

    You are right, you can't search using emails.

    If you still wish to do that, What you can do instead is to get a list of all the customer and filter on the response you get using email.

    For Example, in ruby you can do it as follows:

    customers = Stripe::Customer.all
    
    customer_i_need = customers.select do |c|
      c.email == "foo@bar.com"
    end
    

    PS: Stripe can have multiple customers associated with one email address.

    0 讨论(0)
  • 2020-12-08 06:40

    Please bear in mind when using Stripe API that it is case sensitive email (which is a bit stupid). Hopefully they change this.

    0 讨论(0)
  • 2020-12-08 06:44

    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.

    0 讨论(0)
  • 2020-12-08 06:46

    You only need to write this line

    \Stripe\Customer::all(["email" => "YourDesiredEmail"]);

    0 讨论(0)
  • 2020-12-08 06:48

    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.

    0 讨论(0)
提交回复
热议问题