Firebase Stripe Extension - adding custom claims to user - firebase

I'm integrating Stripe with firebase and firestore using the Run Subscription Payments with Stripe extension. When a user subscribes to the service I need to be able to decode the JWT and see if they are subscribed or not through a custom claim.
The logs on the function show that it is invalidating the custom claim. I'm unsure why this is happening.

From the logs you've provided, it looks like you have multiple subscriptions (sub_KBpVfFzNclrhaG and sub_KBpV10rxE6jkkH) that are both tied to the same user/customer. Subscription sub_KBpVfFzNclrhaG has a status of active or trialing, and so the custom claim was correctly added. However, subscription sub_KBpV10rxE6jkkH was created shortly afterwards (I assume with a status that wasn't active or trialing) which updated the user's custom claim again to null. You can confirm that this is happening by checking the statuses of both Subscriptions in your dashboard.
You need to make sure you have only one active/trialing Subscription per-customer, which would be something you check in your app logic. There's an example of how to check for active/trialing subscriptions for a customer here (https://github.com/stripe-samples/firebase-subscription-payments/blob/4cf116c163ba69f0f46bcc782e4162e0edf452a4/public/javascript/app.js#L121)

Related

Flutter In app purchase - How allow purchase based on the user that is logged?

I'm building a flutter app that will allow users to subscribe. I'm using this plugin: in_app_purchase 0.5.2
So, my question is: I have to log my users in, and I do this using firebase Auth. But, how can I check which user is logged to deliver them the right purchase? I mean, using the firebase Auth, where or must I check if the current user has purchased something?
You can use a database like firestore to store all the purchase information.
Ex:
Users/Auth-id
then retrieve it whenever needed. you can also get the previous purchases with the API
Ex:
final QueryPurchaseDetailsResponse response = await InAppPurchaseConnection.instance.queryPastPurchases(); //returns previous purchases
Note that the App Store does not have any APIs for querying consumable products, and Google Play considers consumable products to no longer be owned once they're marked as consumed and fails to return them here. For restoring these across devices you'll need to persist them on your own server and query that as well.

Admin Notifications in Firebase Firestore

Is there any easy way admin to get notification whenever a new document added to fire store, we are completed the shopping app with android studio and fire base. user successfully placing orders but admin has to go fire base console and to check if any new order.
please help me to get notified by email or any other method. fire base functions may be the solution but i am completely new to functions.
There is nothing built into Firestore to send an email when a new document is added.
But you can use the Firestore integration with Cloud Functions to trigger such an email. In fact, that is precisely what the Firebase trigger email extension does. It's a click-to-install extension, so I recommend giving that a try and see if it fits your needs.

firebase firestore audit log in functions

I have a couple of http functions in my firebase project, because I prefer to hydrate, validate and update the data on the backend. I would like to use the automatic stackdriver logs, but I need to associate the log events with the authenticated user (the requests are authenticated). Is there any way to add some context to database updates? Or commit the changes in the name of the user (not the service account)?
Firestore triggers don't currently associate any information about the end user (not even if you're using Firebase Authentication and security rules), so you will have to write user information into each document in order to track who performed an action on it. It will not just appear in the environment or context.
If you go this route, I strongly suggest adding security rules that require the user to provide their Firebase Auth UID correctly in a document field, so you can be 100% sure it's correct.
Read this for more details: https://medium.com/firebase-developers/patterns-for-security-with-firebase-per-user-permissions-for-cloud-firestore-be67ee8edc4a

Firebase Mailchimp Extension - Update Subscriber Name

Is there a way to update the subscriber when the user updates their profile with their first/last name? Could that be done with a cloud function if not through the extension?
Thanks in advance
[can this be done] when the user updates their profile with their first/last name
There is currently no Cloud Functions trigger for Firebase Authentication profile updates. This means that there is no way to do this directly with Cloud Functions, and hence also not in the Auth Mailchimp sync Extension, which is based on Cloud Functions functionality.
If you build this functionality yourself (you can use the existing extension source code for inspiration), you can trigger an update of the Mailchimp audience directly from your client-side code when you also call the Firebase Authentication API to update the profile.

https.onRequest() vs onWrite() firebase using nodejs for Stripe

Currently the open sourced cloud function provided by Firebase/Stripe uses onwrite to Firebase database to trigger the charge to Stripe:
https://github.com/firebase/functions-samples/tree/master/stripe
It seems that it would be more direct and faster to just call Stripe using https trigger instead of writing to Firebase local database which syncs/writes to Firebase server database, which then triggers the cloud function call to Stripe.
Will there be any problems using Https call to trigger Stripe call? What advantages are there for using onwrite trigger instead of https?
Beginner to beginner, this is my understanding:
Let's say you have an app where a you need to
(1) sign users up for a subscription using Stripe, and then
(2) when users use the app, check to make sure their subscription is still valid.
For (1),
you'd only do this once(ish) per user, and you need to tell Stripe "make a new subscription for this user," so it makes sense to use an https.onRequest or https.onCall function.
For (2),
you'd be checking to see whether the user is subscribed many times, and you're not telling Stripe something, you're asking it about stored information: "is this user's subscription still valid?"
If you're asking about stored information, it's a lot faster to check your own database rather than to wait for a response from Stripe. You just need to make sure the information in your database (e.g. Firestore) is up to date with Stripe.
So what you can do is create a Stripe webhook that will trigger an https.onRequest function whenever there is a change to a user's subscription status. Then, your function writes the change to your database.
So, rather than ask Stripe over and over, "is this user subscribed," and wait for a slow response, you just check your own database, knowing that it's kept up to date by the Stripe webhook.

Resources