更新:从2018年1月左右开始,现在可以使用Stripe上的email
参数进行搜索。请参阅accepted answer。
我想知道在使用Stripe API时是否可以仅通过他们的电子邮件地址搜索客户。
documentation仅表示搜索:
created,
ending_before,
limit,
starting_after
但不是 email
。
我希望避免列出所有客户,以找出哪些客户拥有相同的电子邮件地址。
答案 0 :(得分:25)
我是通过使用以下API请求完成此操作的。这在条带docs中不可用。我通过使用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",
}
警告 这会使用特定于仪表板的未记录的API。虽然今天可能会有效,但无法保证它将来会继续发挥作用。
答案 1 :(得分:20)
您需要检索并存储Stripe客户ID以及数据库中的其他客户详细信息。然后,您可以使用条带客户ID在数据库和retrieve the customer record from Stripe中搜索电子邮件地址。
答案 2 :(得分:12)
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存储在系统中,所以您需要构建一些故障保险柜,以便在创建新客户之前检查特定客户是否存在一。在这方面,通过电子邮件搜索客户非常重要。
答案 3 :(得分:8)
您无法直接通过电子邮件进行搜索。
但是,你可以破解一下列出所有用户,并照看你的电子邮件。
这是我的代码(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);
}
答案 4 :(得分:7)
https://stripe.com/docs/api/php#list_customers
/**
* Remember that Stripe unfortunately allows multiple customers to have the same email address.
* If we ever have more customers with a matching email address than can be returned by a single 'page' of the API request, we'll need a more complicated approach.
* @see https://stackoverflow.com/a/38492724/470749
* @param string $emailAddress
* @return array
*/
public function getCustomersByEmailAddress($emailAddress) {
try {
$response = \Stripe\Customer::all(["limit" => 100, "email" => $emailAddress]);
return $response->data;
} catch (\Exception $e) {
Log::error($e);
return [];
}
}
答案 5 :(得分:2)
因为你指定了
文档仅指示按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可以有多个客户与一个电子邮件地址相关联。
答案 6 :(得分:1)
Stripe API不支持任何电子邮件搜索功能。他们在仪表板中进行了搜索,但没有向API发布;从架构概念来看,似乎没有任何可能性或计划来将条件包含在API中; API中的每个对象只能由创建时由stripe给出的特定对象id检索。可能是,他们将其作为第三方应用程序开发人员参与的范围!
因此,显而易见的解决方案是将客户存储在您希望以后可搜索的数据库中 - 正如Simeon Visser所说above
顺便说一句,对于一种变通方法,如果你已经经常使用条带API并且现在需要搜索许多客户数据 - 唯一的方法是通过'List all customers' functionality of API&amp;为您自己的目的建立数据库;当然,你要use pagination shortcut通过整个列表进行迭代。$customers = \Stripe\Customer::all(array("limit" => 3));
foreach ($customers->autoPagingIterator() as $customer) {
// Do something with $customer
}
答案 7 :(得分:0)
以下是异步 - 等待方式此方法可用于所有具有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 ...非常酷且简单
答案 8 :(得分:0)
您只需要写此行
\Stripe\Customer::all(["email" => "YourDesiredEmail"]);
答案 9 :(得分:0)
条带化功能可以让多个客户拥有同一封电子邮件。也就是说,如果您愿意,可以将过滤器哈希参数传递给list
方法,以返回单个客户或一组客户。
Stripe::Customer.list(email: user.email).data
上面的代码将返回Stripe::Customer
个实例的电子邮件数组
答案 10 :(得分:0)
在使用Stripe API时请记住,它是区分大小写的电子邮件(有点愚蠢)。希望他们能改变这一点。
答案 11 :(得分:0)
您可以尝试一下。它为我工作。如果找不到与电子邮件匹配的数据,则下面的代码将返回空列表。
$stripe = new \Stripe\StripeClient("YOUR_STRIPE_SECRET");
$customers = $stripe->customers->all(['email'=>'jane.doe@example.com',
'limit' => 15,
]);
答案 12 :(得分:0)
在NodeJ中,我们可以使用其电子邮件地址搜索所需的客户,如下所示:
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 的元素。