3

I'm trying to pass Stripe errors such as (card declined, not enough money on card, card expired etc...) to Firebase Firestore then to the client.

I dont know how to make it work for Firestore

Any help would be appreciated. Thanks

Here's my code:

    exports = module.exports = functions.firestore.document('/users/{userId}/charges/{id}').onWrite(event => {
   const val = event.data.data();
   const ref =  event.data.ref;

    if (val === null) return null;
    const amount = val.amount;
    const destination = val.destination;

  return admin.firestore().collection('users').doc(`${event.params.userId}`).get().then(snapshot => {
    return snapshot.data();
  }).then(customer => {
      if (!customer.exists) { 
            console.log("NO SUCH CUSTOMER");
         } else {
        const amount = val.amount;
        const customer_id = customer.customer_id;

        stripe.charges.create({
            amount: amount * 100 ,
            currency: "usd",
            customer: customer_id,
            destination: {
               account: destination,
           },
        });
      }
  }).then(response => {
       console.log("RESPONSE", response);
      // If the result is successful, write it back to the database
      // WRITE RESPONSE BACK
       return event.data.ref.set(response, {merge: true});
    }, error => {
       // WRITE ERRORS BACK
        console.log("ERROR", error);
      return event.data.ref.set(error, {merge: true});
    }).catch((error) => {
//    // We want to capture errors and render them in a user-friendly way, while
//    // still logging an exception with Stackdriver
        return event.data.ref.set(userFacingMessage(error));
      }).then(() => {
        //return reportError(error, {user: event.params.userId});
    });
});
John
  • 799
  • 6
  • 22

0 Answers0