【问题标题】:How to use firebase cloud function to create Stripe customer如何使用 Firebase 云功能创建 Stripe 客户
【发布时间】:2018-12-07 23:26:23
【问题描述】:

大家好,我是 Stripe 的新手,所以我有一个关于如何通过 Firebase 云功能创建 Stripe 客户的问题。我已经阅读了条带标准集成和一些教程。 This example teaches you how to set up the firebase cloud function. 问题是该示例在处理费用或输入金额时创建客户。 Stripe 文档说创建没有付款信息的客户用户是可以的。所以我的想法是每当我为firebase创建用户时,我都会触发云功能来同时创建条纹客户。谁能教我如何做到这一点,并告诉我如何更新绑定到该客户的付款信息。非常感谢。

【问题讨论】:

    标签: ios firebase stripe-payments google-cloud-functions


    【解决方案1】:

    这实际上是一个两部分的问题,但我认为它比你想象的要容易得多。这里有一些关于如何在 typescript 中解决问题的指导。

    创建客户

    要创建客户,请通过 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}`);
    );
    

    更新付款信息

    更新付款是两步火箭。首先,您需要从客户端收集条带令牌。这很容易通过checkout.jsfrom stripe 之类的方式获得(让您安全地收集信用卡)。实际实现非常简单,但取决于您的前端框架。重要的部分是,一旦你有了一个令牌,你就可以在你的后端更新支付信息,即一个云 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`);
    }
    

    一般注意事项

    • 考虑您希望在后端存储哪些信息
    • 更新支付信息时,请考虑使用 express 结合中间件来验证/提取相关信息(以便您可以重复使用代码)
    • 在您的代码中使用try catch,以捕获意外错误
    • 请记住使用条带测试键进行所有操作。您可以在后端使用 firebase 函数配置环境来帮助解决此问题。

    【讨论】:

    • 感谢您的回复。但是是否有可能制作一个功能,只将支付信息存储给该客户。我还不想付款,但让用户可以选择存储卡以备将来使用
    • 以上与支付无关。您只需将支付信息(即信用卡)存储在 sripe 客户身上。
    • 但这也是 updatePaymentRight 的一部分,这是我最困惑的部分。因为我不知道文档哦 paymentTextDelete , addCardController 或 PaymentMethodController 是否应该以这种方式使用。用于存储信用信息以备将来使用。
    • @MaoLi 我在“更新付款信息”一节中向您展示的内容实际上存储了信用卡信息以供将来使用。
    猜你喜欢
    • 2019-03-28
    • 2018-08-19
    • 2021-12-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2018-06-04
    • 2021-12-02
    • 2014-03-30
    相关资源
    最近更新 更多