Zafiera Davids
Zafiera Davids

Reputation: 221

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

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?

Upvotes: 1

Views: 573

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

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

Upvotes: 3

Related Questions