update_stripe_customer_id
A firebase functionfirebase functionCloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests.
https://firebase.google.com/docs/functions that is triggered whenever a new stripe customer is added from the firebase stripe extension. The function then updates the supabase table with the correct stripe_customer_id
.
Steps:
- Firebase function is triggered
- The
supabase_user_id
is queried using thefirebase_user_id
- If a
supabase_user_id
exists, then upserts thesupabase_user_id
along with thestripe_customer_id
into a table.
exports.update_stripe_customer_id = functions.firestore.document('customers/{uid}').onWrite(async (change, context) => {
const stripeId = change.after.data()?.stripeId;
const firebaseUid = context.params.uid
if (stripeId) {
const { data: { id: supabaseUid } } = await supabase.from('user_data')
.select('id')
.eq("firebase_uid", firebaseUid)
.limit(1).single();
if (supabaseUid) {
await supabase.from('stripe')
.upsert({
id: supabaseUid,
stripe_customer_id: stripeId
})
}
}
});