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 [error, setError] = useState(null);
const handleCheckout = async () => {
setLoading(true);
setError(null);
try {
const merchantId = 'your-merchant-id';
const customerEmail = 'customer@example.com';
const planId = 'premium-plan';
const session = await createSession(merchantId, customerEmail, planId);
if (session) {
setSessionId(session);
} else {
setError('Failed to create payment session');
}
} catch (error) {
console.error('Error creating session:', error);
setError('An error occurred while creating the payment session');
} finally {
setLoading(false);
}
};
const handlePaymentComplete = () => {
// Redirect to success page or update UI
window.location.href = '/thank-you';
};
return (
<div className="checkout-container">
<h1>Complete Your Purchase</h1>
<p>Premium Plan - $49.99/month</p>
{error && (
<div className="error-message">{error}</div>
)}
{!sessionId ? (
<button
onClick={handleCheckout}
disabled={loading}
className="checkout-button"
>
{loading ? 'Creating Session...' : 'Pay with Crypto'}
</button>
) : (
<PaymentModal
sessionId={sessionId}
RPC_URL="https://api.mainnet-beta.solana.com"
onRedirect={handlePaymentComplete}
onClose={() => setSessionId(null)}
defaultToken="USDC"
/>
)}
</div>
);
}
export default CheckoutPage;