Adaption of firebase stripe sample cloud functions returning unspecified error

Viewed 99

I am implementing the firebase stripe cloud functions from this sample: https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js with some adaptions (mainly running stripe.confirmCardSetup() on the cloud function instead of front-end because I had a lot of trouble trying to import stripe correctly outside of the functions files) in my Vue webapp.

The code as is, however, produces an error with no type specification, which is why it sets the field "error" to 'An error occurred, developers have been alerted', as defined in the function userFacingMessage()

What is causing these errors? Btw, I am still running stripe in test mode and use the test cards for testing, however, I don't imagine that causes the errors.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// const { Logging } = require('@google-cloud/logging');
// const logging = new Logging({
//   projectId: "",
// });

const stripe = require('stripe')(functions.config().stripe.secret, {
    apiVersion: '2020-03-02',
  });

exports.createStripeCustomerSA = functions
.region('southamerica-east1')
.auth.user().onCreate(async (user) => {
  const customer = await stripe.customers.create({ email: user.email });
  const intent = await stripe.setupIntents.create({
      customer: customer.id,
  });
  await admin.firestore().collection('users').doc(user.email).set({
      customer_id: customer.id,
      setup_secret: intent.client_secret
  });
  return;
});

exports.createStripePaymentSA = functions
.region('southamerica-east1')  
  .firestore
  .document('users/{userId}/payments/{pushId}')
  .onCreate(async (snap, context) => {
    const { amount, payment_method } = snap.data();
    try {
      // Look up the Stripe customer id.
      const customer = (await snap.ref.parent.parent.get()).data().customer_id;
      const setup_secret = (await snap.ref.parent.parent.get()).data().setup_secret;

      const { setupIntent, error } = await stripe.confirmCardSetup(
        await setup_secret,
          {
            payment_method
          }
      );

      if (error) {
        await snap.ref.set({"confirmCardSetupError": 1});
        return;
      }

      // Create a charge using the pushId as the idempotency key
      // to protect against double charges.
      const idempotencyKey = context.params.pushId;
      const payment = await stripe.paymentIntents.create(
        {
          amount: amount,
          currency: 'BRL',
          customer: customer,
          payment_method: setupIntent.payment_method,
          off_session: false,
          confirm: true,
          confirmation_method: 'manual',
        },
        { idempotencyKey }
      );
      // If the result is successful, write it back to the database.
      await snap.ref.set(payment);

      //CREAT THE COLLECTION OF PAID ITEMS
      await snap.ref.parent.parent.collection("wantedCourses").map(function (course) {
        return course.set("paid", 1);
      })

    } catch (error) {
      // We want to capture errors and render them in a user-friendly way, while
      // still logging an exception with StackDriver
      
      await snap.ref.set({ error: userFacingMessage(error) }, { merge: true });
      // await reportError(error, { user: context.params.userId });
    }
});

exports.confirmStripePaymentSA = functions
.region('southamerica-east1')
  .firestore
  .document('stripe_customers/{userId}/payments/{pushId}')
  .onUpdate(async (change, context) => {
    if (change.after.data().status === 'requires_confirmation') {
      const payment = await stripe.paymentIntents.confirm(
        change.after.data().id
      );
      change.after.ref.set(payment);
    }
});

function userFacingMessage(error) {
    return error.type
      ? error.message
      : 'An error occurred, developers have been alerted';
  }
0 Answers
Related