首页 > 解决方案 > 使用 firebase-cloud-functions 将付款来源添加到 Stripe?

问题描述

我正在尝试将条带支付与我的 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 数据库映像

错误日志图像

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsstripe-payments

解决方案


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

Firestore 的等效代码是:

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

另见:


推荐阅读