我尝试将此功能部署到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 };
});
});
我遵循了本教程
我运行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
有人知道可能出什么问题吗?
答案 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;
...
});