import React, { useState } from 'react';
import { createSession, PaymentModal } from '@whisky-peak/whisky-pay-sdk';
function CheckoutPage() {
const [sessionId, setSessionId] = useState(null);
const [loading, setLoading] = useState(false);
const handleCheckout = async () => {
setLoading(true);
try {
// Replace with your actual merchant ID from the dashboard
const merchantId = 'your-saas-id-from-dashboard';
// Create a payment session
const session = await createSession(
merchantId,
'customer@example.com', // Customer's email
'premium-plan' // Plan identifier
);
if (session) {
setSessionId(session);
} else {
alert('Failed to create payment session');
}
} catch (error) {
console.error('Error creating session:', error);
alert('An error occurred');
} finally {
setLoading(false);
}
};
// When payment is complete, redirect to a thank you page
const handlePaymentComplete = () => {
window.location.href = '/thank-you';
};
return (
<div>
{!sessionId ? (
<button
onClick={handleCheckout}
disabled={loading}
>
{loading ? 'Creating session...' : 'Pay with Crypto'}
</button>
) : (
<PaymentModal
sessionId={sessionId}
RPC_URL="https://api.mainnet-beta.solana.com"
onRedirect={handlePaymentComplete}
/>
)}
</div>
);
}
export default CheckoutPage;