0

I'm building a React App where you users can make one-time purchases of in-game tokens using Stripe. I'm having trouble getting onCurrentUserPayment() to work from the @stripe/firestore-stripe-payments sdk.

CODE

I have an onClick button event with the following code.

  const buyTokens1 = async () => {
      const session = await createCheckoutSession(payments, {
        mode: "payment",
        price: [PRICE_ID],
        success_url: window.location.origin,
        cancel_url: window.location.origin,
      });
      window.location.assign(session.url);

      onCurrentUserPaymentUpdate(payments, async (snapshot) => {
        for (const change in snapshot.changes) {
          if (change.type === "added") {
            const docRef = doc(db, "users", user.uid);
            await updateDoc(docRef, {
              tokens: authUser.tokens + 100,
            });
          }
        }
      });

  };

PROBLEM

When I click the button, the stripe checkout works perfectly fine, but the onCurrentUserPaymentUpdate() does not work.

It's supposed to execute the firebase updateDoc function when the Stripe purchase finishes, but it is not doing it.

The documentation isn't super clear. I've tried changing the "change-type" if statement and switching up API keys. None of this has worked.

Help! Thank you in advance.

filmsaremyalc
  • 17
  • 1
  • 6
  • I think you meant to listen for Stripe subscription update events through [**onCurrentUserSubscriptionUpdate**](https://github.com/stripe/stripe-firebase-extensions/tree/master/firestore-stripe-web-sdk#listen-for-subscription-updates) I don't find any `@stripe/stripe-firebase-extensions` package. – Rohit Kharche Jun 13 '23 at 05:35
  • Ahh yes, but I'm looking to listen for Stripe payment events, not subscription events. I tried using `OnCurrentUserSubscriptionUpdate` and that didn't work either. Also my bad and edited. It was `@stripe/firestore-stripe-payments` like you had linked. – filmsaremyalc Jun 13 '23 at 15:27

1 Answers1

0

I was never able to get onCurrentUserPaymentUpdate to work, so instead I leaned on Firebase onSnapshot event listeners in order to track for the payment collection to update.

Once I did, I updated the document.

See Code Below

 useEffect(() => {
    const paymentQ = query(
      collection(db, "users", user.uid, "payments"),
      where("status", "==", "succeeded"),
    );

    const unsubscribe = onSnapshot(paymentQ, (snapshot) => {
      snapshot.docChanges().forEach(async (change) => {
     if (change.type === "added") {
        if (authUser) {
           const docRef = doc(db, "users", user.uid);

           await updateDoc(docRef, {
              kTokens: authUser?.kTokens + 100,
             });
           }
         }
       }
      });
    });

    return () => {
      unsubscribe();
    };
  }, [authUser]);
filmsaremyalc
  • 17
  • 1
  • 6