【问题标题】:Stripe, is it possible to search a customer by their email?Stripe,是否可以通过电子邮件搜索客户?
【发布时间】:2015-01-02 05:34:33
【问题描述】:

更新:从 2018 年 1 月左右开始,现在可以在 Stripe 上使用 email 参数进行搜索。请参阅accepted answer


我想知道在使用 Stripe API 时是否可以仅通过电子邮件地址搜索客户。

documentation 仅表示搜索:

created,
ending_before,
limit,
starting_after 

但不是email

我不想列出所有客户来查找哪些客户具有相同的电子邮件地址。

【问题讨论】:

  • 此外,最大限制为 100,因此,如果您有超过 100 个客户,则无法保证通过电子邮件搜索会找到通过电子邮件找到的客户。
  • 假设您现在可以通过电子邮件进行搜索,但 Stripe 仍然愚蠢地允许多个客户使用同一电子邮件,因此我们需要处理结果中的重复项:stackoverflow.com/a/40482496/470749
  • 更新:Stripe 现在允许搜索电子邮件地址:stackoverflow.com/a/38492724/470749
  • 请注意,搜索不限于 100 条记录。每页最多有 100 条记录。您可以使用starting_after参数检索更多

标签: stripe-payments


【解决方案1】:

Stripe 现在允许您通过电子邮件过滤客户。

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);
    ...

这对于确保您不会创建重复客户非常重要。因为您不能将在 Stripe 中创建客户并将 Stripe 客户 ID 存储在您的系统中的单个事务中,您需要构建一些故障保险柜,在您创建新客户之前检查特定客户是否存在。在这方面,通过电子邮件搜索客户很重要。

【讨论】:

  • 如果您不喜欢通用地图,可以使用当前的 Java API: List customers = Customer.list(CustomerListParams.builder().setEmail(email).build() ).getData();
  • 在python中stripe.Customer.list(email=user_email)
  • 虽然此解决方案可能有效,但我认为最好在本地存储客户信息,包括他们的条带 ID 和电子邮件。对条带 API 的调用总是比对本地数据库的查询更慢且更不可靠。每当创建新客户时,您都可以使用 webhook 进行处理,并将 Stripe 的客户 ID 和任何关联的电子邮件保存在您的数据库中。然后,使用任何可能触发创建客户的操作来查询他们,如果找到客户,则在相关调用中将 ID 传递给 Stripe。
【解决方案2】:

我通过使用以下 API 请求来做到这一点。这在条带文档中不可用。我通过使用浏览器开发工具在仪表板区域中跟踪他们的搜索得到了这个。

    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",
    }

警告 这使用了特定于仪表板的未记录 API。虽然它今天可能有效,但不能保证它将来会继续有效。

【讨论】:

  • 我认为这个答案值得一看stackoverflow.com/questions/26767150/…,因为它不使用黑客
  • 在没有 Content-Type 的情况下从 Python 库 Requests 工作。
  • 我喜欢你的。。while 循环遍历 300,000 个用户的前景似乎不能很好地利用网络流量。
【解决方案3】:

您需要检索并存储 Stripe 客户 ID 以及数据库中的其他客户详细信息。然后,您可以使用 Stripe 客户 ID 在您的数据库中搜索电子邮件地址和 retrieve the customer record from Stripe

【讨论】:

    【解决方案4】:

    更新:Stripe 现在允许通过电子邮件进行搜索

    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 [];
        }
    }
    

    【讨论】:

    • 如果您知道日期,您不妨存储和使用 customerID
    • @MartijnScheffer 我正在使用 ClickFunnels,我正在通过电子邮件向新买家发送一个链接来管理她的订阅详情。 ClickFunnels 不会公开 CustomerID。另外,我不希望仅为 1 或 2 列信息创建和管理数据库。所以,虽然我希望 Stripe 能处理这个问题,但我发现除了我的 hack 之外别无他法。
    【解决方案5】:

    您不能直接通过电子邮件搜索。

    但是,您可以稍作修改以列出所有用户,并管理您的电子邮件。

    这是我的代码(PHP):

    $last_customer = NULL;
    $email = "EmailYou@AreLooking.for";
    while (true) {
        $customers = \Stripe\Customer::all(array("limit" => 100, "starting_after" => $last_customer));
        foreach ($customers->autoPagingIterator() as $customer) {
            if ($customer->email == $email) {
                $customerIamLookingFor = $customer;
                break 2;
            }
        }
        if (!$customers->has_more) {
            break;
        }
        $last_customer = end($customers->data);
    }
    

    【讨论】:

    • 完美,非常感谢:)。看起来这是您对 SO 的唯一答案,我认为这是对这个问题的最佳答案。
    • 我真的很讨厌带有 break 语句的无限循环……但它奏效了。
    • 自发布此问题以来,他们可能已经更改了 API 规则:我在第一个循环中收到错误 You passed an empty string for 'starting_after'. We assume empty values are an attempt to unset a parameter; however 'starting_after' cannot be unset. You should remove 'starting_after' from your request or supply a non-empty value.;因此我必须插入 if($last_customer){ // ... }else { // .... } 才能让它工作。
    • if($last_customer){ $customers = $stripe_client-&gt;customers-&gt;all(array("limit" =&gt; 100, "starting_after" =&gt; $last_customer)); }else { $customers = $stripe_client-&gt;customers-&gt;all(array("limit" =&gt; 100)); }
    【解决方案6】:

    你只需要写这行

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

    【讨论】:

      【解决方案7】:

      既然你指定了

      文档只说明了按created、ending_before、limit和starting_after搜索,没有“email”。

      你是对的,你不能使用电子邮件搜索。

      如果您仍然希望这样做,您可以做的是获取所有客户的列表,并使用 email 过滤您获得的响应。

      例如,在ruby 你可以这样做:

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

      PS:Stripe 可以有多个客户与一个电子邮件地址相关联。

      【讨论】:

        【解决方案8】:

        在使用 Stripe API 时请记住,它是区分大小写的电子邮件(这有点愚蠢)。希望他们能改变这一点。

        【讨论】:

        • 这给我带来了问题。我现在正在尝试找出解决方法。
        • 我唯一能想到的就是一次抓取一百个并在比较之前转换为小写。我必须把它变成一个 laravel 工作。谈低效
        【解决方案9】:

        在 NodeJs 中,我们可以使用他们的电子邮件地址搜索我们想要的客户,如下所示:

        const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
        const stripe = require('stripe')(stripeSecretKey);
        
        const findCustomerByEmail = async (email) => {
          try {
            const customer = await stripe.customers.list( {
              email: email,
              limit: 1
          });
           if(customer.data.length !== 0){
            return customer.data[0].id;
           }
          } catch (e) {
           return (e);
         }
        };
        

        stripe 的实际调用是使用 stripe.customers.list。如果电子邮件存在于我们的条带帐户中,则返回的对象将包含一个名为 data 的元素。

        【讨论】:

          【解决方案10】:

          Stripe API 不支持任何电子邮件搜索功能。他们在仪表板中有此搜索,但未向 API 发布任何内容;从架构概念来看,stripe 似乎没有可能或计划将其包含在 API 中;其 API 中的每个对象只能通过 stripe 在创建时给出的特定对象 ID 来检索。可能是,他们将其作为第三方应用程序开发人员参与的范围!!

          因此,显而易见的解决方案是将客户存储在您自己的数据库中,以便将来可以搜索 - 正如 Simeon Visser 所说的 above

          顺便说一句,如果您已经大量使用了 Stripe API,并且您现在需要搜索许多客户数据 - 唯一的方法是通过 'List all customers' functionality of API 并为您构建数据库自己的目的;当然,您必须 use pagination shortcut 遍历整个列表才能这样做。

          $customers = \Stripe\Customer::all(array("limit" => 3));
          foreach ($customers->autoPagingIterator() as $customer) {
            // Do something with $customer
          }
          

          【讨论】:

            【解决方案11】:

            你可以试试这个。它对我有用。如果找不到与电子邮件匹配的数据,下面的代码将返回空列表。

                 $stripe = new \Stripe\StripeClient("YOUR_STRIPE_SECRET");
            
                 $customers = $stripe->customers->all(['email'=>'jane.doe@example.com',
                    'limit' => 15,
                  ]);
            

            【讨论】:

              【解决方案12】:

              这是 Async-Await 方式,此方法可用于所有第三方特别是 Nodejs 的点击

                  const configuration = {
                          headers: {
                              "authorization": `Bearer ${Your stripe test key}`,
                              "content-type": "application/x-www-form-urlencoded",
                          }
                        };
                        const requestUrl = `https://api.stripe.com/v1/search?
                  query=${'email you are to use'} &prefix=false`
                      const emailPayment = await axios.get(requestUrl, 
                  configuration)
              

              Axiom 是用于创建 http 请求的 Npm...非常酷而且非常简单

              【讨论】:

                【解决方案13】:

                Stripe 允许有多个客户使用相同的电子邮件。也就是说,如果您愿意,可以将过滤器哈希参数传递给 list 方法以返回单个客户或客户数组。

                Stripe::Customer.list(email: user.email).data
                

                上面将返回带有电子邮件的Stripe::Customer 实例数组

                【讨论】:

                  【解决方案14】:

                  Stripe Search API Beta 现已推出

                  youtube link

                  【讨论】:

                    猜你喜欢
                    • 2020-11-15
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2022-01-13
                    • 2014-12-01
                    • 1970-01-01
                    • 2022-11-25
                    相关资源
                    最近更新 更多