@@ -2,13 +2,23 @@ import { NextRequest, NextResponse } from 'next/server';
22import crypto from 'crypto' ;
33import { 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
812export 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 ( {
0 commit comments