Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 1x 1x 36x 36x 36x 12x 12x 10x 10x 10x 2x 2x 2x 2x 2x 12x 12x 36x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 12x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 3x 3x 9x 9x 1x 1x 9x 9x 9x 9x 12x 12x 11x 11x 12x 36x 36x 36x 12x 12x 12x 24x 24x 24x 24x 36x 36x | 'use client';
import { useState } from 'react';
import { ArrowRight, Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { useToast } from '@/hooks/use-toast';
declare global {
interface Window {
Razorpay: new (options: RazorpayOptions) => RazorpayInstance;
}
}
interface RazorpayOptions {
key: string;
amount: number;
currency: string;
name: string;
description: string;
order_id: string;
prefill: {
email?: string;
name?: string;
};
theme: {
color: string;
};
handler: (response: RazorpayResponse) => void;
modal: {
ondismiss: () => void;
};
}
interface RazorpayInstance {
open: () => void;
}
interface RazorpayResponse {
razorpay_order_id: string;
razorpay_payment_id: string;
razorpay_signature: string;
}
interface PricingCheckoutProps {
tier: 'basic' | 'pro' | 'premium';
planName: string;
}
export function PricingCheckout({ tier, planName }: PricingCheckoutProps) {
const [isLoading, setIsLoading] = useState(false);
const { toast } = useToast();
const loadRazorpayScript = (): Promise<boolean> => {
return new Promise((resolve) => {
if (window.Razorpay) {
resolve(true);
return;
}
const script = document.createElement('script');
script.src = 'https://checkout.razorpay.com/v1/checkout.js';
script.onload = () => resolve(true);
script.onerror = () => resolve(false);
document.body.appendChild(script);
});
};
const handleCheckout = async () => {
setIsLoading(true);
try {
// Load Razorpay script
const scriptLoaded = await loadRazorpayScript();
if (!scriptLoaded) {
toast({
title: 'Error',
description: 'Failed to load payment gateway. Please try again.',
variant: 'destructive',
});
return;
}
// Create order
const orderResponse = await fetch('/api/payments/create-order', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tier }),
});
const orderData = await orderResponse.json();
if (!orderResponse.ok) {
toast({
title: 'Error',
description: orderData.error || 'Failed to create order',
variant: 'destructive',
});
return;
}
// Show discount notification if applicable
if (orderData.discount?.applied) {
toast({
title: '50% Trust Score Discount Applied!',
description: `Original price: \u20B9${(orderData.discount.originalAmount / 100).toFixed(0)} \u2192 You pay: \u20B9${(orderData.amount / 100).toFixed(0)}`,
});
}
// Open Razorpay checkout
const options: RazorpayOptions = {
key: orderData.keyId,
amount: orderData.amount,
currency: orderData.currency,
name: orderData.name,
description: orderData.description,
order_id: orderData.orderId,
prefill: orderData.prefill,
theme: {
color: '#7c3aed',
},
handler: async (response: RazorpayResponse) => {
// Verify payment
try {
const verifyResponse = await fetch('/api/payments/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(response),
});
const verifyData = await verifyResponse.json();
if (verifyResponse.ok && verifyData.success) {
toast({
title: 'Payment Successful!',
description: `You're now on the ${planName} plan.`,
});
// Use hard navigation to subscription page so users see their updated billing
window.location.href = '/subscription';
} else {
toast({
title: 'Verification Failed',
description: verifyData.error || 'Payment verification failed',
variant: 'destructive',
});
}
} catch {
toast({
title: 'Error',
description: 'Failed to verify payment. Please contact support.',
variant: 'destructive',
});
}
},
modal: {
ondismiss: () => {
setIsLoading(false);
},
},
};
const razorpay = new window.Razorpay(options);
razorpay.open();
} catch {
toast({
title: 'Error',
description: 'Something went wrong. Please try again.',
variant: 'destructive',
});
} finally {
setIsLoading(false);
}
};
return (
<Button onClick={handleCheckout} disabled={isLoading} className="w-full">
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Processing...
</>
) : (
<>
Upgrade to {planName}
<ArrowRight className="ml-2 h-4 w-4" />
</>
)}
</Button>
);
}
|