【问题标题】:JavaScript Google Cloud Function: write Stripe values to FirebaseJavaScript Google Cloud Function:将 Stripe 值写入 Firebase
【发布时间】:2019-11-12 16:27:04
【问题描述】:

我是 JavaScript 新手,我在各种资源的帮助下编写了以下 JS Google Cloud Function。

此函数处理 Stripe invoice.payment_succeeded 事件,而不是写入整个 data,我试图将发送的 period_startperiod_end 值都保存回我的 Firebase DB 中的正确位置(参见结构下面)。

如何在同一个函数调用中写入这两个值?

exports.reocurringPaymentWebhook = functions.https.onRequest((req, res) => {

  const hook  = req.body.type;
  const data  = req.body.data.object;
  const status = req.body.data.object.status;
  const customer = req.body.data.object.customer;
  const period_start = req.body.data.object.period_start;
  const period_end = req.body.data.object.period_end;

  console.log('customer', customer);
  console.log('hook:', hook);
  console.log('status', status);
  console.log('data:', data);
  console.log('period_start:', period_start);
  console.log('period_end:', period_end);


return admin.database().ref(`/stripe_ids/${customer}`).once('value').then(snapshot => snapshot.val()).then((userId) => {
  const ref = admin.database().ref(`/stripe_customers/${userId}/subscription/response`)
    return ref.set(data);
})
.then(() => res.status(200).send(`(200 OK) - successfully handled ${hook}`))
.catch((error) => {
  // We want to capture errors and render them in a user-friendly way, while
  // still logging an exception with StackDriver
  return snap.ref.child('error').set(userFacingMessage(error));
})
.then((error) => {
  return reportError(error, {user: context.params.userId});
});

});//End

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions stripe-payments


    【解决方案1】:

    HTTP 类型的函数在响应发送后立即终止。在您的代码中,您正在发送响应,然后尝试做更多的工作。您必须在发送响应之前完成所有工作,否则可能会被切断。

    【讨论】:

      【解决方案2】:

      如果您只想保存period_startperiod_end 值,而不是整个data 对象,您可以使用update() 方法(请参阅https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields)。

      然后您应该如下修改您的代码。 (请注意,不清楚您从哪里收到 userId 值,因为您没有在问题中显示 stripe_ids 数据库节点。我假设它是 /stripe_ids/${customer} 的值. 你可以适应它。)

      exports.reocurringPaymentWebhook = functions.https.onRequest((req, res) => {
      
        const hook  = req.body.type;
        const data  = req.body.data.object;
        const status = req.body.data.object.status;
        const customer = req.body.data.object.customer;
        const period_start = req.body.data.object.period_start;
        const period_end = req.body.data.object.period_end;
      
      
        admin.database().ref(`/stripe_ids/${customer}`).once('value')
        .then(snapshot => {
           const userId = snapshot.val();
           let updates = {};
           updates[`/stripe_customers/${userId}/subscription/response/period_start`] = period_start;
           updates[`/stripe_customers/${userId}/subscription/response/period_end`] = period_end;
      
           return admin.database().ref().update(updates);
        })
        .then(() => res.status(200).send(`(200 OK) - successfully handled ${hook}`))
        .catch((error) => {...});
      
      });
      

      【讨论】:

      猜你喜欢
      • 2019-06-11
      • 2019-02-14
      • 2020-05-05
      • 2018-03-08
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2020-02-11
      相关资源
      最近更新 更多