将 stripeCharge 函数上传到 Firebase 云函数不起作用

Posted

技术标签:

【中文标题】将 stripeCharge 函数上传到 Firebase 云函数不起作用【英文标题】:Upload stripeCharge function to Firebase cloud function not working 【发布时间】:2019-07-17 09:28:00 【问题描述】:

我尝试将此功能部署到 Firebase

另外,我使用 Cloud Firestore 作为数据库

const stripe = require("stripe")("STRIPE_API_KEY");
exports.stripeCharge = functions.firestore
  .document("/payments/userId/paymentId")
  .onWrite(event => 
    const payment = event.data.val();
    const userId = event.params.userId;
    const paymentId = event.params.paymentId;

    // checks if payment exists or if it has already been charged
    if (!payment || payment.charge) return;

    return admin
      .firestore()
      .doc(`/users/$userId`)
      .once("value")
      .then(snapshot => 
        return snapshot.val();
      )
      .then(customer => 
        const amount = payment.amount;
        const idempotency_key = paymentId; // prevent duplicate charges
        const source = payment.token.id;
        const currency = "eur";
        const charge =  amount, currency, source ;

        return stripe.charges.create(charge,  idempotency_key );
      )

      .then(charge => 
        admin
          .firestore()
          .doc(`/payments/$userId/$paymentId/charge`)
          .set(charge),
           merge: true ;
      );
  );

我按照本教程进行操作

Process Stripe Payments with Firebase Cloud Functions - Part 2 YouTube · 14 000+ views · 2017/07/11 · by Fireship - AngularFirebase

当我运行firebase deploy --only functions

这出现在终端中

!  functions: failed to create function stripeCharge
HTTP Error: 400, The request has errors


Functions deploy had errors with the following functions:
        stripeCharge


To try redeploying those functions, run:
    firebase deploy --only functions:stripeCharge


To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.

我在 Firebase 日志中收到此错误 Firebase Log error

有人知道可能是什么问题吗?

【问题讨论】:

您使用的是哪个版本的 Cloud Functions for Firebase。看来您使用的是旧语法,请参阅firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore。您应该检查您的package.json 文件并确认此语法与您的版本一致。您能否将此package.json 文件的内容添加到您的问题中。无论如何,迁移到最新的 CF 版本是个好主意。 "name": "functions", "description": "Cloud Functions for Firebase", "scripts": "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" , "dependencies": "@sendgrid/mail": "^6.3.1", "cors": "^2.8.5", "firebase-admin": "~7.0.0", "firebase-functions": "^2.2.0", "stripe": "^6.25.1" , "private": true 感谢该文档的链接,您说得对,我一直在使用旧语法 【参考方案1】:

至少您的一个问题是您使用 Firebase SDK for Cloud Functions 版本 1.0.0 的语法,而您的 package.json 显示您使用的版本是 >= 2.2.0。

您应该使用新语法:

exports.stripeCharge = functions.firestore
  .document("/payments/userId/paymentId")
  .onWrite((change, context) => 

    const payment = change.after.data();
    const userId = event.params.userId;
    const paymentId = event.params.paymentId;
    ...
);

【讨论】:

@D.Hodges 你什么意思?

以上是关于将 stripeCharge 函数上传到 Firebase 云函数不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 函数上传到存储

python fire库的使用

添加连接点不会生成 fire_* 方法

Golang理解-函数变量

Angular Fire Functions 调用但未运行

未处理的承诺拒绝:this._next 不是函数:@angular/fire/messaging 中的区域