简体   繁体   English

如何使用Firebase云功能创建Stripe客户

[英]How to use firebase cloud function to create Stripe customer

Hello guys I am new to Stripe, so I am having a question about how to create stripe customer through firebase cloud function. 大家好,我是Stripe的新手,所以我有一个关于如何通过Firebase云功能创建Stripe客户的问题。 I have been read through the stripe standard integration and few tutorials. 我已经阅读了Stripe标准集成和一些教程。 This example teaches you how to set up the firebase cloud function. 本示例教您如何设置Firebase云功能。 The problem is that the example creates customer whenever the charge is being processed or the amount is entered. 问题在于,无论何时处理费用或输入金额,该示例都会创建客户。 Stripe documentation says that it is fine to create the customer user without payment information. Stripe文档说,可以创建没有付款信息的客户用户。 So my thought is whenever i create user for firebase, I trigger cloud function to create the stripe customer at the same time. 所以我的想法是,每当我为Firebase创建用户时,我都会触发云功能来同时创建条带化客户。 Can anyone teach me how to do that exactly, and Also show me how to update the payment information bind to that customer. 谁能教我如何正确执行此操作,并且还可以向我展示如何更新绑定到该客户的付款信息。 Thank you very much. 非常感谢你。

This is actually a 2 part question, but I think it's a lot easier than you think. 这实际上是一个分为两部分的问题,但是我认为这比您想象的要容易得多。 Here is some guidance on how to solve the problem in typescript. 这是有关如何解决打字稿中问题的一些指导。

Create the customer 创建客户

To create the customer, through an create-auth-trigger do the following: 要创建客户,请通过create-auth-trigger执行以下操作:

export const syncUserToStripe = functions.auth.user().onCreate(async (data, context) => 
  const stripe = new Stripe(<stripe-token>); // Initialize the stripe SDK
  const stripeCustomer = await stripe.customers.create({
        email: data.email
  }); // Now you have the stripe customer. Maybe you would like to save the stripeCustomer Id to database/firestore
  console.log(`Done syncing firestore user with id: ${data.uid} to Stripe. The stripe id is ${stripeCustomer.id}`);
);

Update payment information 更新付款信息

The update payment is a two step rocket. 更新付款是两步走的火箭。 First you need to collect the stripe token from your client. 首先,您需要从客户端收集数据条令牌。 This is most easily obtained with something like checkout.js from stripe (let's you securly collect the credit card). 这很容易通过stripe中的checkout.js东西来获得(让我们安全地收集信用卡)。 The actual implementation is pretty easy, but various depending on your front-end framework. 实际的实现很容易,但是根据您的前端框架而有所不同。 The important part is that once you have a token, you can update the payment info on your backend, ie a cloud https function (you call this function, when you have the stripe token) The important part of the code could look like this: 重要的部分是,一旦有了令牌,就可以在后端更新付款信息,即云https函数(当您拥有条带令牌时调用此函数)。代码的重要部分如下所示:

export async function updateStripePayment(req: Request, res: Response): Promise<any> {
 const stripe = new Stripe(<stripe-token>); // Initialize the stripe SDK
 // Extract the stripe token from the header
 const stripeToken = req.header('StripeToken');
 // You also need to get the stripe customer id. Here I will get it from the header, but maybe it makes more sense for you to read it from firestore/realtime db.
 const stripeCustomerId = req.header('stripeCustomerId');
 // Now you can create a new payment source
 const newSource = await stripeAPI.customers.createSource(stripeCustomerId, {source: stripeToken});
 // And you can now optionally set it as default
 await stripeAPI.customers.update(stripeCustomerId, {default_source: newSource.id});
 res.status(200).json(`Sucessfully updated stripe payment info`);
}

General considerations 一般注意事项

  • Consider what information you would like to store in you backend 考虑一下您想在后端存储什么信息
  • When updating payment info, consider using express combined with middleware to authenticate/extract relevant info (so you can reuse code) 更新付款信息时,请考虑结合使用express和中间件来认证/提取相关信息(以便您可以重复使用代码)
  • Use try catch on your code, to catch unexpected errors 在代码上使用try catch来捕获意外错误
  • Remember to do everything with stripe test keys. 记住要使用条纹测试键进行所有操作。 You can use firebase functions config environment to help with this on the backend. 您可以在后端使用Firebase函数配置环境来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM