【问题标题】:How can I add the Stripe customer id to the user's data in firestore using cloud functions?如何使用云功能将 Stripe 客户 ID 添加到 Firestore 中的用户数据?
【发布时间】:2021-12-02 12:37:11
【问题描述】:

我要做的是在新用户注册时创建一个条带客户,然后使用条带客户 ID 更新 Firestore 中的用户数据。我正在创建条带客户,但由于某种原因,我没有检索条带客户 ID 并更新用户数据。下面是我的代码。寻求帮助!

//create Stripe customer-------------------------------------------------------------
    exports.createStripeCustomer = functions.firestore
    .document('customers/{customerId}')
    .onCreate(async (doc, context) => {
      
      const stripe = require('stripe')(functions.config().stripe.testkey)

      const userData = doc.data()
      
      const customer = {
        email: userData.email,
        phone: userData.phone,
        name: userData.firstName+" "+client.lastName,  
      }
      
      let cust = null
      
      try {
        cust = await stripe.customers.create(customer)
        .then(async () => {
          const stripeCustomer = await cust
          if (stripeCustomer) {
            const stripeInfo = {
              customerId: stripeCustomer.id
            }         
            try {
               admin.firestore().collection('cusomters').doc(userData.userId)
                  .update({
                      stripeInfo
                  })
              //await snap.ref.update({ stripeInfo })
            } catch (err) {
              console.error(err)
            }
          }
        })
      } catch (err) {
        return err
      }
      return cust
    
      
    })

【问题讨论】:

  • 您遇到错误了吗?
  • @DivyaniYadav 不,这让我感到困惑......我会尝试不同的方法
  • @DivyaniYadav 我发现了一个拼写错误,哈哈……我们看看会发生什么
  • 是否创建了条带客户?哪部分不合格?另外,我不清楚您为什么在创建客户时使用混合模式 await 和 .then():cust = await stripe.customers.create(customer) .then(async () => { const stripeCustomer = await cust 这是怎么回事?
  • @AntonySanders 你能把那个集合('customers')更正为'customers',看看它是否工作正常吗?

标签: node.js firebase google-cloud-firestore google-cloud-functions stripe-payments


【解决方案1】:

您能否澄清一下您在此处使用的 async/await/promise 结构:

cust = await stripe.customers.create(customer)
        .then(async () => {
          const stripeCustomer = await cust

为什么await.then() 与另一个await?这个真的不清楚。

其次,您的收藏有一个错字:collection('cusomters') 拼写错误 customers 所以也许这最终会奏效,但在与您预期不同的地方?

【讨论】:

    猜你喜欢
    • 2018-12-07
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2023-02-17
    • 2017-03-13
    • 2019-03-28
    • 2019-09-28
    相关资源
    最近更新 更多