Billing is currently in Beta and its APIs are experimental and may undergo breaking changes. To mitigate potential disruptions, we recommend pinning your SDK and clerk-js package versions.
The usePaymentElement() hook is used to control the payment form rendered by the <PaymentElement /> component. It provides the necessary state and methods to submit payment details to a payment provider like Stripe.
This hook must be used within a component that is a descendant of the <PaymentElementProvider /> component. It is typically used in a checkout flow that prompts a user to add a new payment method, or for adding a new payment method outside of a checkout.
usePaymentElement() returns an object with the following properties:
Name
isFormReady
Type
boolean
Description
A boolean that indicates if the payment form UI has been rendered and is ready for user input. This is useful for disabling a submit button until the form is interactive.
Name
isProviderReady
Type
boolean
Description
A boolean that indicates if the underlying payment provider (e.g. Stripe) has been fully initialized.
Name
provider
Type
undefined | { name: "stripe"; }
Description
An object containing information about the initialized payment provider. It is undefined until isProviderReady is true.
Name
reset
Type
() => Promise<void>
Description
A function that resets the payment form to its initial, empty state.
A function that submits the payment form data to the payment provider. It returns a promise that resolves with either a data object containing a payment token on success, or an error object on failure.
The <PaymentElementProvider /> component sets up the context for the payment element. It fetches all the necessary data from the payment provider (e.g., Stripe) and makes it available to its children.
This component renders the actual payment form from the provider (e.g., the Stripe Payment Element). It should be rendered as a child of <PaymentElementProvider />.
The following example demonstrates how to create a billing page where a user can add a new payment method. It is split into two components:
<UserBillingPage />: Sets up the <PaymentElementProvider />, which specifies that the payment actions within its children are for the user.
<AddPaymentMethodForm />: Renders the payment form and handles the submission logic. It uses usePaymentElement() to get the submit function and useUser() to get the user object. When the form is submitted, it first creates a payment token and then attaches it to the user.