1
votes

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

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

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?

1
Which version of Cloud Functions for Firebase are you using. It seems you use an old syntax, see firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore. You should check in your package.json file and confirm that this syntax is in line with your version. Can you add the content of this package.json file to your question. In any case it would be a good idea to migrate to a recent CF version. - Renaud Tarnec
{ "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 } - Zafiera Davids
Thanks for the link to that docs, you're right, I've been using the old syntax - Zafiera Davids

1 Answers

3
votes

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;
    ...
});