首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用firebase云功能创建Stripe客户

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

Stack Overflow用户
提问于 2018-06-28 19:51:48
回答 1查看 2.7K关注 0票数 0

大家好,我是Stripe的新手,所以我有一个关于如何通过firebase云功能创建条形客户的问题。我已经读过条纹标准集成和很少的教程。这个例子告诉您如何设置firebase云函数。问题是,每当处理收费或输入金额时,该示例就会创建客户。Stripe文档说,在没有支付信息的情况下创建客户用户是很好的。因此,我的想法是,每当我为firebase创建用户时,我就触发云功能,同时创建条带客户。有人能教我如何做到这一点,并告诉我如何更新付款信息绑定到该客户。非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-29 05:41:51

这其实是一个2部分的问题,但我认为这比你想象的要容易得多。这里有一些关于如何解决打字本问题的指导。

创建客户

若要创建客户,请通过create触发器执行以下操作:

代码语言:javascript
复制
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}`);
);

更新支付信息

更新付款是一个两步火箭。首先,您需要从您的客户收集条纹令牌。这是最容易获得的东西,如checkout.js从条纹(让我们安全地收集信用卡)。实际的实现是相当容易的,但取决于您的前端框架。重要的是,一旦您有了令牌,您就可以在后端更新支付信息,即云https函数(当您拥有条带令牌时,您调用此函数)代码的重要部分可能如下所示:

代码语言:javascript
复制
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`);
}

一般考虑事项

  • 考虑一下您想要在后端存储哪些信息。
  • 在更新支付信息时,考虑使用express与中间件结合来验证/提取相关信息(这样您就可以重用代码)。
  • 在代码中使用try catch来捕获意外错误
  • 记住用条纹测试键做任何事情。您可以使用firebase函数配置环境来帮助后端进行此操作。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51090348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档