Search code examples
flutterfirebasegoogle-cloud-firestorein-app-purchasebilling

Cloud Function Firebase solution to adding in app purchases


I am building a Flutter app that uses Firebase as its backend, everything works with cloud functions. I am trying to implement in-app purchases. I want the app to work like this:

The user buys a subscription -> User in Firestore becomes a Premium User

When any event happens with his subscription like it reaching the end it should update the status of the user in Firestore.

I've searched a lot with solutions like Adapty but I do not quite understand how to implement a server-side call to a cloud function of mine that will update the user on Firestore depending on its subscription state. I see there should be some client-side code that checks if the user is still subscribed, but I do not want that at all, I want everything to be done server-side, so it's secure. After the user pays, the server should handle all the updates in Firestore, the client just needs to get the data about the user being premium in Firestore or not.

I thought that you could do a cloud function that takes care of billing directly, but I do not know how I could connect Flutter purchases to a cloud function without going through the insecure client side.

How should I model this? It seems to be very simple but I do not quite understand how to implement such a simple subscription system with Firebase Cloud Functions. I think it all goes down to being able to implement some kind of automated call to a cloud function when something happens to the user subscription, but how can I do that? I am open to any technology that implements such a billing model that works on Flutter and Firebase Cloud Functions.


Solution

  • To implement a server-side call to a cloud function and update the user on Firestore with Cloud Functions, you can handle events in Cloud Firestore with no need to update client code and can make Cloud Firestore changes via the DocumentSnapshot interface. You can find the detailed documentation here.

    Also as per your requirements you can check the codelab.