Stripe - 一键创建/检索客户 [英] Stripe - create / retrieve customer in one call

查看:45
本文介绍了Stripe - 一键创建/检索客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有条带 API 调用可用于在用户不存在时创建用户并检索新用户?

Is there a stripe API call that we can use to create a user if they don't exist, and retrieve the new user?

说我们这样做:

export const createCustomer = function (email: string) {
  return stripe.customers.create({email});
};

即使使用该电子邮件的用户已经存在,它也会始终创建一个新的客户 ID.是否有一种方法可以仅在条带中不存在用户电子邮件时才创建用户?

even if the user with that email already exists, it will always create a new customer id. Is there a method that will create a user only if the user email does not exist in stripe?

我只想避免竞争条件,在这种情况下,同一时间范围内可能会发生多个 stripe.customers.create({email}) 调用.例如,我们检查 customer.id 是否存在,不存在,两个不同的服务器请求可能会尝试创建一个新客户.

I just want to avoid a race condition where more than one stripe.customers.create({email}) calls might happen in the same timeframe. For example, we check to see if customer.id exists, and does not, two different server requests could attempt to create a new customer.

这是竞争条件:

const email = 'foo@example.com';

Promise.all([
  stripe.customers.retrieve(email).then(function(user){
   if(!user){
     return stripe.customers.create(email);
   }
  },
 stripe.customers.retrieve(email).then(function(user){
   if(!user){
     return stripe.customers.create(email);
   }
 }
])

显然,与同一个服务器请求相比,竞争条件更有可能发生在两个不同的进程或两个不同的服务器请求中,但您懂的.

obviously the race condition is more likely to happen in two different processes or two different server requests, than the same server request, but you get the idea.

推荐答案

不,在 Stripe 中没有内置的方法来做到这一点.Stripe 不要求客户的电子邮件地址是唯一的,因此您必须亲自验证它.您可以在您自己的数据库中跟踪您的用户并避免重复,或者您可以使用 Stripe API 检查给定电子邮件是否已经存在客户:

No, there is no inbuilt way to do this in Stripe. Stripe does not require that a customer's email address be unique, so you would have to validate it on your side. You can either track your users in your own database and avoid duplicates that way, or you can check with the Stripe API if customers already exist for the given email:

let email = "test@example.com";
let existingCustomers = await stripe.customers.list({email : email});
if(existingCustomers.data.length){
    // don't create customer
}else{
    let customer = await stripe.customers.create({
        email : email
    });
}

这篇关于Stripe - 一键创建/检索客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆