【问题标题】:Add Payment source to Stripe with firebase-cloud-functions?使用 firebase-cloud-functions 将付款来源添加到 Stripe?
【发布时间】:2019-06-28 10:22:47
【问题描述】:

我正在尝试将条带支付与我的 Firestore Firebase 数据库集成。我无法弄清楚 firebase 文档示例中给出的添加支付源功能。我在这里想念什么?

exports.addPaymentSource = functions.firestore
.document('Customers/{userId}/paymentSources/{paymentId}')
.onWrite((change, context) => {
    let newPaymentSource = change.after.data();
    if (newPaymentSource === null){
        return null;
    }
    return admin.firestore().collection("Customers").doc(`${context.params.userId}`).get('customer_id')
        .then((snapshot) => {
          return snapshot.val();
        }).then((customer) => {
          return stripe.customers.createSource(customer, {newPaymentSource});
        }).then((response) => {
          return change.after.ref.parent.set(response);
        }, (error) => {
          return change.after.ref.parent.child('error').set(userFacingMessage(error));
        }).then(() => {
          return reportError(error, {user: context.params.userId});
        });
   });

我试过了

console.log(snapshot.val())

它给了我一个类型错误。

Firestore database Image

Error Log Image

【问题讨论】:

  • 请不要在问题中包含图像和链接 - 链接中断,如果我们想在答案中重新使用您的结构,我们将不得不重新输入它。包括代码和结构作为文本。要获取您的 Firebase 结构,请使用 Firebase 控制台->导出 JSON 并复制粘贴您的结构的 sn-p。见images and links are evil

标签: javascript firebase google-cloud-firestore google-cloud-functions stripe-payments


【解决方案1】:

您正在从 Cloud Firestore 读取数据,但正在使用用于实时数据库的变量名称和方法调用。虽然这两个数据库都是 Firebase 的一部分,但它们是完全独立的,并且具有不同的 API。

Firestore 的等效代码为:

return admin.firestore().collection("Customers").doc(`${context.params.userId}`).get()
    .then((doc) => {
      return doc.data();
    }).then((customer) => {
      ...

另见:

【讨论】:

  • 谢谢!绝对是使用firestore和RTDB的区别
猜你喜欢
  • 2020-08-16
  • 2018-12-13
  • 2017-12-24
  • 2015-05-20
  • 2016-05-22
  • 1970-01-01
  • 2020-12-12
  • 2019-04-15
  • 2020-09-30
相关资源
最近更新 更多