Overview
TheuseCreatePaymentIntent hook is a custom React hook designed to create a payment intent using an API endpoint. It handles the creation process, manages loading and error states, and provides the client secret, payment intent, and customer options.
Usage
Parameters
payload(object, required): An object containing the payment details.amount(number): The amount for the payment.currency(string): The currency code for the payment.planCode(string): The code of the plan associated with the payment.
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:clientSecret(string | null): The client secret for the payment intent.paymentIntent(object): The payment intent object returned by the API.customerOptions(object): Customer options related to the payment intent.loading(boolean): Indicates whether the payment intent is currently being created.error(string | null): Contains an error message if an error occurred during the process, otherwise null.
Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/payments - Method: POST
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
- Body: JSON string containing
amount,currency, andplanCode
Error Handling
If an error occurs during the API call, theerror state will be set to the error message. You should handle this in your component to display an appropriate error message to the user.
Performance Considerations
- The hook uses
useEffectto create the payment intent when the component mounts or when theamountchanges. - The API call is made only once per mount or when the
amountchanges, preventing unnecessary requests.
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined