大家好,我是Stripe的新手,所以我有一个关于如何通过firebase云功能创建条形客户的问题。我已经读过条纹标准集成和很少的教程。这个例子告诉您如何设置firebase云函数。问题是,每当处理收费或输入金额时,该示例就会创建客户。Stripe文档说,在没有支付信息的情况下创建客户用户是很好的。因此,我的想法是,每当我为firebase创建用户时,我就触发云功能,同时创建条带客户。有人能教我如何做到这一点,并告诉我如何更新付款信息绑定到该客户。非常感谢。
发布于 2018-06-29 05:41:51
这其实是一个2部分的问题,但我认为这比你想象的要容易得多。这里有一些关于如何解决打字本问题的指导。
创建客户
若要创建客户,请通过create触发器执行以下操作:
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函数(当您拥有条带令牌时,您调用此函数)代码的重要部分可能如下所示:
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`);
}一般考虑事项
try catch来捕获意外错误https://stackoverflow.com/questions/51090348
复制相似问题