Regarding stripe extension for firebase: https://firebase.google.com/products/extensions/firestore-stripe-invoices I haven't found anything related in the documentation yet, and source code didn't reveal much to me (at least to my understanding).
In the configuration of the extension it asks for a collection to listen for invoices. Is it possible to use a collection group instead? So instead of listening for invoices at invoices
I want it to listen at users/{uid}/invoices
From the extension code,
let invoicesInFirestore = await admin
.firestore()
.collection(config.invoicesCollectionPath)
.where('stripeInvoiceId', '==', invoice.id)
.get();
it appears that, at the time of writing, it is not foreseen to handle multi subcollections.
I can see two solutions:
You copy the extension code and modify it in such a way it handles multi subcollections, in order to create your own Cloud Functions.
Note that the extension code declares the Cloud Function with export const sendInvoice = functions.handler.firestore.document.onCreate(...)
. As explained in the doc "The HandlerBuilder
class facilitates the writing of functions by developers building Firebase Extensions... Do not use HandlerBuilder
when writing normal functions for deployment via the Firebase CLI."
So you should adapt it as follows:
export const sendInvoice = functions.firestore
.document('users/{uid}/invoices')
.onCreate((snap, context) => {...}
However, note that by writing your own Cloud Function, you will loose potential future improvements to the extension.
The idea is to have a Cloud Function listening to all the users/{uid}/invoices
subcollections (see above) that copies the new doc and creates a copy in the "central" Extension collection. Nothing prevent you to add some extra fields in the copied doc, like the user's uid.
Note that, if you want to get the feedback from Stripe, you may need another Cloud Function (which listens to the "central" Extension collection) to copy/paste the results of the Stripe webhook calls to the original documents in the users/{uid}/invoices
subcollection.
Personally I would go for this second approach.