0
votes

I am trying to process my payment with firebase and stripe and have come across a problem when trying to deploy my function to the cloud saying 'Promises must be handled appropriately. I know this is a tlint compilation error but can't figure out why the error is being triggered.

Here is my code

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')(functions.config().stripe.testkey);

exports.stripeCharge = functions.firestore
.document('/payments/{userId}/mypayments/{paymentId}')
.onCreate((snap,event) => {
const payment = snap.data()
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 null;

return admin.firestore()
  .doc(`/users/${userId}`)
  .get()
  .then(snapshot => {
    return snapshot
  })
  .then(customer => {
    const amount = payment.price * 100 // amount must be in cents
    const idempotency_key = paymentId  // prevent duplicate charges
    const source = payment.token.id
    const currency = 'usd'
    const charge = {amount, currency, source}

    return stripe.charges.create(charge, { idempotency_key })
 })
 .then((charge) => {
   admin.firestore()//The error keeps referring me to this line
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
    .set({
      charge: charge
    }, { merge: true })
 })
})

The line generating the error is stated above

2

2 Answers

2
votes

Actually, with the latest version(s) of Cloud Functions you are not obliged to include a catch() in your Promises chaining. The platform where the Cloud Function runs will handle the error itself.

Based on this post What could this be about? [TsLint Error: "Promises must be handled appropriately"] it is apparently an error generated by TsLint (EsLint?).

However, independently of this "error" detected by TsLint, I think you may encounter problems with your Cloud Function because you don't return the last promise of your chain:

return admin.firestore()     //HERE YOU RETURN CORRECTLY
  .doc(`/users/${userId}`)
  .get()
  .then(snapshot => {
    return snapshot         //HERE YOU RETURN CORRECTLY
  })
  .then(customer => {
    const amount = payment.price * 100 // amount must be in cents
    const idempotency_key = paymentId  // prevent duplicate charges
    const source = payment.token.id
    const currency = 'usd'
    const charge = {amount, currency, source}

    return stripe.charges.create(charge, { idempotency_key })    //HERE YOU RETURN CORRECTLY
 })
 .then((charge) => {
   return admin.firestore()    //HERE, IN YOUR CODE, YOU DON'T RETURN
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
    .set({
      charge: charge
    }, { merge: true })
 })
})
0
votes

finally figure it out Whenever you make a promise function, it has to end with an error handler so i fixed this by using a simple catch

.then((charge) => {
   admin.firestore()
    .collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
    .set({
      charge: charge
    }, { merge: true })
    .catch(er=>{
        console.log(er);
        return er
    }
    )
 })