Ad
Upload StripeCharge Function To Firebase Cloud Function Not Working
I tried deploying this function to Firebase
Also, Im using Cloud Firestore as the database
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 };
});
});
I followed this tutorial
When I run firebase deploy --only functions
This appears in the terminal
! 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.
And I get this error in the Firebase log Firebase Log error
Anybody have a clue with what could be wrong?
Ad
Answer
At least one of your problem is that you use the syntax of the Firebase SDK for Cloud Functions for version < 1.0.0 and your package.json
shows that you use a version that is >= 2.2.0.
You should use the new syntax:
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;
...
});
Ad
source: stackoverflow.com
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad