getStripeCheckoutSession

This function is a helper for creating a Stripe Checkout Session. It pings a corresponding backend function to create a short-term session that can be used to allow a customer to pay for one-time purchases or subscriptions through Stripe Checkout.

Import

import { getStripeCheckoutSession } from '@staqjs/client'

API

type: function

signature: (uid: string, customerId: string, priceId: string) => Promise<Session>

This function takes a user ID, a Stripe Customer ID, and a Stripe Price ID.

When successful, the function returns a Promise which resolves to the Session object. Use the session object to call redirectToCheckout.

When unsuccessful, the Promise resolves to an error.

Example Usage

import { getStripeCheckoutSession, withAuth, withStripe } from '@staqjs/client'

function Dashboard(props) {
  const { auth, stripe } = props
  const [isLoading, setLoading] = useState(false)
  
  const onClickCheckoutBtn = () => {
    setLoading(true)
    getStripeCheckoutSession(
      auth.currentUser.uid,
      auth.currentUser.stripeCustomerId,
      'price_1I2nbcA8NzJgebq9qZajEq0E'
    )
      .then((result) => {
        setLoading(false)
        return stripe.redirectToCheckout({ sessionId: result.data.id })
      })
      .catch((error) => {
        console.error(error)
        // error handling
      })
  }

  // ... code ...
}

export default withAuth(withStripe(Dashboard))