Overview
The Chainrails React package provides two hooks for payment modal integration:usePaymentSession: Automatic session management with server-side URLusePaymentModal: Manual session token control for advanced use cases
For both cases, the
<PaymentModal /> component requires a server-generated session token to function. We’ve provided a Github
repository with a curated Github template to help you get started quickly:
Chainrails Demo ServerOption 1: usePaymentSession Hook (Recommended)
TheusePaymentSession hook is ideal for simple implementations where your session URL is known upfront. The hook handles session creation automatically.
When to Use
- You have a static session endpoint
- You want minimal setup and automatic session management
- You don’t need to fetch additional data before opening the modal
Parameters
session_url(string, required): Your server endpoint for creating payment sessionsonSuccess(function, optional): Callback fired when payment completes successfullyonCancel(function, optional): Callback fired when user cancels the paymentstyles(object, optional): Available styling (e.g.,{ accentColor: "#FF6B6B", theme: "dark" })
Example Implementation
Hook Return Value
TheusePaymentSession hook returns an object with:
open(): Function to open the payment modalclose(): Function to close the payment modalisOpen: Boolean indicating if modal is currently opensessionToken: The current session token (automatically managed)
Option 2: usePaymentModal Hook (Advanced)
TheusePaymentModal hook provides manual control over session token management. Use this when you need to fetch session data dynamically, set custom amounts, or coordinate the modal with other operations.
When to Use
- You need to fetch session data from your server dynamically
- You want to display custom amounts or user-specific information
- You need to perform additional operations before opening the modal
- You want explicit control over the session lifecycle
Parameters
sessionToken(string | null, required): Session token obtained from your serveronSuccess(function, optional): Callback fired when payment completes successfullyonCancel(function, optional): Callback fired when user cancels the paymentstyles(object, optional): Available styling (e.g.,{ accentColor: "#FF6B6B", theme: "dark" })
Example Implementation
Hook Return Value
TheusePaymentModal hook returns an object with:
open(): Function to open the payment modalclose(): Function to close the payment modalisOpen: Boolean indicating if modal is currently opensessionToken: The current session token (pass this to the hook)
PaymentModal Component Props
The<PaymentModal /> component accepts the following props:
| Prop | Type | Description |
|---|---|---|
open | function | Opens the modal |
close | function | Closes the modal |
isOpen | boolean | Current modal visibility state |
sessionToken | string | Active session token for the payment |
styles | object | Custom styling object with options like accentColor, theme |
amount | number | (Optional) Display amount in the modal |
excludeChains | array | (Optional) Array of chains to exclude from the modal (e.g., [chains.BASE]) |
Styling
Both hooks accept astyles object to customize the modal appearance:
accentColor: Primary color for buttons and highlightstheme: “light”, “dark”, or “system” for automatic theme switching
Callbacks
onSuccess
Fired when the payment completes successfully. Use this to:- Update user balance
- Show success message
- Redirect to confirmation page
onCancel
Fired when the user closes or cancels the payment modal. Use this to:- Log analytics
- Show informational message
- Clean up UI state
Quick Comparison
| Feature | usePaymentSession | usePaymentModal |
|---|---|---|
| Setup Complexity | Simple | Moderate |
| Session Management | Automatic | Manual |
| Dynamic Data Fetching | Limited | Full Control |
| Best For | Standard flows | Complex workflows |

