更新:自2018年1月左右以来,现在可以使用Stripe上的email
参数进行搜索。请参阅已接受的答案。
我想知道在使用Stripe API时是否可以仅通过客户的电子邮件地址搜索客户。
文档仅指示搜索依据:
created,
ending_before,
limit,
starting_after
但不是email
。
我想避免列出所有的客户来查找哪些客户的电子邮件地址相同。
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放在一个交易中,所以你需要建立一些故障保险箱,在创建新客户之前检查是否存在特定客户。在这方面,通过电子邮件搜索客户非常重要。
我使用以下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。虽然它可能在今天发挥作用,但不能保证它在未来会继续发挥作用
您需要在数据库中检索并存储Stripe客户ID以及其他客户详细信息。然后,您可以在数据库中搜索电子邮件地址,并使用Stripe客户ID从Stripe检索客户记录。
更新:Stripe现在允许通过电子邮件地址进行搜索
https://stripe.com/docs/api/php#list_customershttps://stripe.com/docs/api/customers/search
/**
* 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 = StripeCustomer::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 [];
}
}
您不能直接通过电子邮件进行搜索。
然而,你可以破解一点,列出所有用户,并处理你的电子邮件。
这是我的代码(PHP):
$last_customer = NULL;
$email = "EmailYou@AreLooking.for";
while (true) {
$customers = StripeCustomer::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);
}
您只需要写这行
StripeCustomer::all(["email" => "YourDesiredEmail"]);
在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.customs.list。如果电子邮件存在于我们的stripe帐户中,则返回的对象将包含一个名为data
使用"list()"和";search()",您可以通过电子邮件获得客户,使用下面的Python代码:
customers = stripe.Customer.list(
email="example@gmail.com",
)
customer = stripe.Customer.search(
query="email:'example@gmail.com'"
)
您还可以限制客户使用"极限;参数如下所示:
customers = stripe.Customer.list(
email="example@gmail.com",
limit=3
)
customer = stripe.Customer.search(
query="email:'example@gmail.com'",
limit=3
)
因为您指定了
文档只指示按created、ending_before、limit和starting_after进行搜索,而不指示"电子邮件"。
你是对的,你不能用电子邮件搜索。
如果你仍然想这样做,你可以做的是获得所有客户的列表,并使用email
过滤你得到的响应。
例如,在ruby
中,您可以按如下方式执行:
customers = Stripe::Customer.all
customer_i_need = customers.select do |c|
c.email == "foo@bar.com"
end
PS:Stripe可以有多个客户与一个电子邮件地址关联。
使用Stripe API时请记住,它是区分大小写的电子邮件(这有点愚蠢)。希望他们能改变这一点。
Stripe API不支持任何电子邮件搜索功能。他们在他们的仪表板中有这个搜索,但没有向API发布任何搜索;从架构概念上看,stripe似乎不可能或计划将其包含在API中;API中的每个对象都只能通过stripe在创建时给定的特定对象id来检索。也许,他们一直将其作为第三方应用程序开发人员参与的范围!!
因此,显而易见的解决方案是将客户存储在您自己的数据库中,以便在未来进行搜索——正如Simeon Visser在上所说
顺便说一句,如果你已经大量使用stripe API,并且有许多客户数据需要搜索,那么唯一的方法就是通过API&为自己的目的建立数据库;当然,您必须使用分页快捷方式来遍历整个列表
$customers = StripeCustomer::all(array("limit" => 3));
foreach ($customers->autoPagingIterator() as $customer) {
// Do something with $customer
}
您可以尝试一下。它对我有效。如果没有找到与电子邮件匹配的数据,下面的代码将返回空列表。
$stripe = new StripeStripeClient("YOUR_STRIPE_SECRET");
$customers = $stripe->customers->all(['email'=>'jane.doe@example.com',
'limit' => 15,
]);
以下是异步-等待方式此方法可用于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。。。非常酷和非常简单的
Stripe允许有多个客户使用同一封电子邮件。也就是说,如果您愿意,可以向list
方法传递一个过滤器散列参数,以返回单个客户或客户数组。
Stripe::Customer.list(email: user.email).data
上面将返回一个Stripe::Customer
实例数组,其中包含电子邮件
Stripe Search API Beta现在可用
youtube链接