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

时间:2019-02-23 12:08:30

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

我尝试将此功能部署到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

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

1 个答案:

答案 0 :(得分:2)

至少有一个问题,就是您使用版本为 << / strong> 1.0.0的Firebase SDK for Cloud Functions的语法,而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;
    ...
});