Skip to content

Commit 096e5b9

Browse files
committed
resolved suggestions
1 parent dc6716b commit 096e5b9

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/app/api/razorpay/webhook/route.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { NextRequest, NextResponse } from 'next/server';
22
import crypto from 'crypto';
33
import { prisma } from '@/lib/database';
44

5+
// Ensure this route is always handled at runtime and not during build
6+
export const dynamic = 'force-dynamic';
7+
export const runtime = 'nodejs';
8+
59
// Webhook secret from Razorpay dashboard
6-
const WEBHOOK_SECRET = process.env.RAZORPAY_WEBHOOK_SECRET!;
10+
// Note: Do NOT throw at module init; check inside the handler to avoid build-time failures
711

812
export async function POST(req: NextRequest) {
913
console.log('🔔 Razorpay webhook received');
1014

1115
try {
16+
const WEBHOOK_SECRET = process.env.RAZORPAY_WEBHOOK_SECRET;
17+
if (!WEBHOOK_SECRET) {
18+
console.error('❌ Missing RAZORPAY_WEBHOOK_SECRET');
19+
return NextResponse.json({ error: 'Missing webhook secret' }, { status: 500 });
20+
}
21+
1222
const body = await req.text();
1323
const signature = req.headers.get('x-razorpay-signature');
1424

@@ -74,6 +84,10 @@ async function handlePaymentCaptured(payment: any) {
7484
}
7585

7686
// Update payment status
87+
if (paymentRecord.status === 'COMPLETED') {
88+
console.log('⚠️ Payment already processed:', payment.id);
89+
return;
90+
}
7791
await prisma.payment.update({
7892
where: { id: paymentRecord.id },
7993
data: {
@@ -93,12 +107,8 @@ async function handlePaymentCaptured(payment: any) {
93107
const now = new Date();
94108
const subscriptionStartedAt = new Date(now);
95109
const subscriptionEndsAt = new Date(now);
96-
97-
if (order.billingCycle === 'monthly') {
98-
subscriptionEndsAt.setMonth(subscriptionEndsAt.getMonth() + 1);
99-
} else if (order.billingCycle === 'yearly') {
100-
subscriptionEndsAt.setFullYear(subscriptionEndsAt.getFullYear() + 1);
101-
}
110+
const daysToAdd = order.billingCycle === 'monthly' ? 30 : 365;
111+
subscriptionEndsAt.setDate(subscriptionEndsAt.getDate() + daysToAdd);
102112

103113
// Update user subscription
104114
await prisma.user.update({

src/components/Footer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ export default function Footer() {
9999
{ href: "/features", label: "Features" },
100100
{ href: "/pricing", label: "Pricing" },
101101
{ href: "/demo", label: "Demo" },
102-
{ href: isSignedIn ? (canAccessPro ? "/generate-pro" : "/generate") : "/generate", label: "Get Started" }
102+
{ href: isSignedIn ? (canAccessPro ? "/generate-pro" : "/generate") : "/sign-up", label: "Get Started" }
103+
103104
].map((link, index) => (
104105
<li key={index}>
105106
<Link

src/components/core/generate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export default function Generate() {
6565
title: "Flashcards generated successfully!",
6666
description: `${generated.length} flashcards created.`,
6767
})
68-
} catch (error: Error | any) {
68+
} catch (error: Error | unknown) {
6969
console.error('Error generating flashcards:', error)
7070
toast({
7171
title: "Error",
72-
description: error.message,
72+
description: error instanceof Error ? error.message : 'An unexpected error occurred',
7373
variant: "destructive",
7474
});
7575
} finally {

src/hooks/useSubscription.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useUser } from '@clerk/nextjs';
66
interface SubscriptionStatus {
77
isActive: boolean;
88
plan: string;
9-
expiresAt: string | null;
9+
expiresAt: Date | null;
1010
isExpired: boolean;
1111
canAccessPro: boolean;
1212
loading: boolean;
@@ -39,12 +39,13 @@ export function useSubscription() {
3939
const data = await response.json();
4040
setSubscription({
4141
...data,
42+
expiresAt: data.expiresAt ? new Date(data.expiresAt) : null,
4243
loading: false
4344
});
4445
} else {
4546
setSubscription(prev => ({ ...prev, loading: false }));
4647
}
47-
} catch (error) {
48+
} catch (error: unknown) {
4849
console.error('Error fetching subscription status:', error);
4950
setSubscription(prev => ({ ...prev, loading: false }));
5051
}

0 commit comments

Comments
 (0)