Skip to Content
Docs

Release a checkout

Free the native checkout behind a PayCheckout once you're done with it.

checkout.release() frees the native checkout that PayCheckout.create() started. Call it when you're finished with the checkout, such as after the customer completes or cancels payment, when the screen unmounts, or before starting a checkout for a new session. Card fields are separate. usePayCardFields() releases them when its component unmounts.

It's safe to call more than once. Once a checkout is released, checkout.pay() and checkout.presentDropIn() reject with released. checkout.session, checkout.paymentMethods(), and checkout.savedPaymentMethods() keep returning what the checkout loaded.

checkout.release()

Example

This hook starts a checkout for a screen, and releases it when the screen unmounts or the session changes, even if that happens before PayCheckout.create() resolves:

import { useEffect, useState } from 'react'
import { PayCheckout } from '@pay-com/react-native'

export function useCheckout(clientSecret: string): PayCheckout | null {
  const [checkout, setCheckout] = useState<PayCheckout | null>(null)

  useEffect(() => {
    let current: PayCheckout | null = null
    let done = false
    PayCheckout.create(clientSecret, { returnUrl: 'myshop://pay' })
      .then((created) => {
        if (done) {
          created.release() // the screen unmounted while the session loaded
        } else {
          current = created
          setCheckout(created)
        }
      })
      .catch((error) => console.warn('Could not start the checkout', error))

    return () => {
      done = true
      current?.release()
      setCheckout(null)
    }
  }, [clientSecret])

  return checkout
}

Next steps

  • Start a checkout: Start a new checkout for another session.
  • Card fields: Release card fields you created yourself with new PayCardFields().
Was this page helpful?