#itemas - Billing Address
1 messages ยท Page 1 of 1 (latest)
Hi, I have react hook for this:
export const useCreateSession = () => {
// Create a new checkout session in the subollection inside this users document
const [loading, setLoading] = useState(false);
const createSession = async (uid: string, priceId: string) => {
setLoading(true);
const checkoutSessionRef = await addDoc(
collection(firestore, "users", uid, "checkout_sessions"),
{
price: priceId,
success_url: window.location.origin,
cancel_url: window.location.origin,
}
);
onSnapshot(checkoutSessionRef, async (doc) => {
const { sessionId } = doc.data() as CheckoutSession;
console.log(doc.data());
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = await initializeStripe();
const result = await stripe?.redirectToCheckout({ sessionId });
setLoading(false);
if (result?.error) {
console.log(result.error.message);
}
}
});
};
return { createSession, loading };
};
Ah, this is using Firestore... ๐ค