Yalzeee
Yalzeee

Reputation: 187

Firestore Cloud write functions not working

I am trying to set up a chain of promises that are triggered when a stripe payment is done. My functions properly lint and compile but they are not deployed but when i try the firebase deploy --only functions i get this error

+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (37.79 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 6 function stripeCharge(us-central1)...
i  functions: updating Node.js 6 function getTime(us-central1)...
+  functions[getTime(us-central1)]: Successful update operation.
!  functions[stripeCharge(us-central1)]: Deployment error.
Failed to configure trigger providers/cloud.firestore/eventTypes/[email protected] 
(__gcf__.us-central1.stripeCharge)


Functions deploy had errors. To continue deploying other features (such as 
database), run:
firebase deploy --except functions

Error: Functions did not deploy properly.

Here is my code for my function

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

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

const stripe = require('stripe')('mytestkey');

exports.stripeCharge = functions.firestore
   .document('/store{userId}/mypayments/activepayments/{paymentId}')
   .onCreate((snap, event) => {
     const payment = snap.data()
     const userId = event.params.userId
     const paymentId = event.params.paymentId
     console.log(payment);

   // 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(snapshot => {
        const amount = payment.amount // amount must be in cents
        const idempotency_key = paymentId  // prevent duplicate charges
        const source = payment.token.source.id;
        const currency = 'usd'
        const charge = { amount, currency, source }
        console.log(charge);

        return stripe.charges.create(charge, { idempotency_key })
      })
      .then((charge) => {
        admin.firestore()
          .doc(`store${userId}/mypayments/activepayments/${paymentId}`)
          .set({
            charge: charge 
          }, { merge: true })
          .then(() => {
            if (charge.status === 'succeeded') {
              if (payment.amount === 3000) {
                const validTill = Date.now() + 12 * 30 * 24 * 60 * 60 * 1000;
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    validTill
                  }).then(() => {
                    console.log('successfully updated expiration date from server');
                  }
                  )
                  .catch(er => {
                    console.log(er);
                     return er;
                   })
               }
              if (payment.amount === 2000) {
                const validTill = Date.now() + 6 * 30 * 24 * 60 * 60 * 1000;
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    exp: validTill
                  },{merge: false})
                  .catch(er => {
                    console.log(er);
                    return er;
                 })
              }
             if (payment.amount === 5100) {
                const validTill = Date.now() + 12 * 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                   .doc(`/store${userId}/stats/mystatistics/exp`)
                   .set({
                   exp: validTill
                  },{merge: false})
                   .catch(er => {
                    return er;
                  })
              }
               if (payment.amount === 2700) {
               const validTill = Date.now() + 6 * 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    exp: validTill
                   },{merge: false})
                  .catch(er => {
                    return er;
                  })
              }  
              if (payment.amount === 500) {
                const validTill = Date.now() + 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                 .doc(`/store${userId}/stats/mystatistics/exp`)
                 .set({
                    exp: validTill
                  },{merge: false})
                  .catch(er => {
                    return er;
                  })
               }
            }
           }).catch(er =>{
             console.log(er)
         })
       })
      .catch(er=>{
         console.log(er);
      })
  })  

I would really appreciate any help figuring out where the problem is coming from and why my triggers are not being saved

Upvotes: 0

Views: 925

Answers (1)

JeremyW
JeremyW

Reputation: 5272

Per my (and others) comments above, the consensus is that the route you are watching as the trigger is not valid - I don't think Firebase supports "partial" wildcards like /store{userId}/... I get what you're trying to do, I just don't think it's supported.

Try altering your trigger to watch

.document('/{storeUserId}/mypayments/activepayments/{paymentId}')

and that should deploy. You'll have to change the way you plan on storing information, but the trigger will work.

Upvotes: 2

Related Questions