I have installed the "Run Subscription Payments with Stripe" extension in Firebase.
When I try the to subscribe a user to a plan I get an error saying "snap.data is not a function". I use the following code:
db
.collection('customers')
.doc(cred.user.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
}).then(() => {
// Wait for the CheckoutSession to get attached by the extension
db
.collection('customers')
.doc(cred.user.uid)
.collection('checkout_sessions').onSnapshot((snap) => {
console.log(snap)
const { sessionId } = snap.data();
if ({sessionId}) {
console.log(sessionId)
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = Stripe('pk_test_ZEgiqIsOgob2wWIceTh0kCV4001CPznHi4');
stripe.redirectToCheckout({ sessionId });
};
});
});
This is a .then variation on the code example provided by the Firebase team:
const docRef = await db
.collection('customers')
.doc(currentUser.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
});
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
const { sessionId } = snap.data();
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = Stripe('pk_test_1234');
stripe.redirectToCheckout({ sessionId });
}
});
I got an error with the await function, saying "Uncaught SyntaxError: await is only valid in async function". So I refactered the code to a .then variant.
When I run this code from a button.addEventListener("click) I get an error saying snap.data is not a function.
I checked snap and it is populated
The customer gets created in Firestore and in Stripe dashboard, the customer session gets created.
What goes wrong here? There is a similair question on Stackoveflow (Run Subscription Payments with Stripe Firebase Extension Webhook Not Firing), but there is no answer, so I'm trying again.