PaymentModal Component
The PaymentModal component provides a complete payment UI for your application. It handles token selection, balance checking, payment confirmation, and transaction processing.
Basic Usage
The easiest way to use PaymentModal is through the useVolrPay hook:
import { useVolrPay } from '@volr/react-ui';
function CheckoutButton() {
const { pay } = useVolrPay();
const handlePay = () => {
pay({
amount: '10.00',
currency: 'USDC',
onSuccess: () => {
console.log('Payment successful!');
},
onError: (error) => {
console.error('Payment failed:', error);
},
});
};
return <button onClick={handlePay}>Pay $10.00</button>;
}
useVolrPay Hook
The useVolrPay hook provides a simple API for opening the payment modal.
Payment Options
interface PaymentModalOptions {
amount: string; // Payment amount (e.g., "10.00")
currency: string; // Currency symbol (e.g., "USDC")
item?: { // Optional item details
name: string;
description?: string;
image?: string;
};
onSuccess?: () => void; // Called when payment succeeds
onError?: (error: Error) => void; // Called when payment fails
onCancel?: () => void; // Called when user cancels
}
Example with Item Details
import { useVolrPay } from '@volr/react-ui';
function ProductCard({ product }) {
const { pay } = useVolrPay();
const handlePurchase = () => {
pay({
amount: product.price,
currency: 'USDC',
item: {
name: product.name,
description: product.description,
image: product.imageUrl,
},
onSuccess: () => {
console.log('Purchase successful!');
// Redirect to success page, update cart, etc.
},
onError: (error) => {
console.error('Purchase failed:', error);
alert('Payment failed. Please try again.');
},
});
};
return (
<div>
<h3>{product.name}</h3>
<p>{product.description}</p>
<button onClick={handlePurchase}>
Buy for {product.price} USDC
</button>
</div>
);
}
Direct PaymentModal Usage
You can also use PaymentModal directly if you need more control:
import { PaymentModal } from '@volr/react-ui';
import { useState } from 'react';
import { useVolrModal } from '@volr/react-ui';
function CustomPaymentFlow() {
const [showPayment, setShowPayment] = useState(false);
const { openPayment } = useVolrModal();
const handlePay = () => {
openPayment({
amount: '10.00',
currency: 'USDC',
onSuccess: () => {
setShowPayment(false);
console.log('Payment successful!');
},
onError: (error) => {
console.error('Payment failed:', error);
},
});
};
return (
<>
<button onClick={handlePay}>Pay</button>
{/* PaymentModal is rendered automatically via portal */}
</>
);
}
Payment States
The PaymentModal handles several states automatically: