@@ -27,13 +27,16 @@ export async function POST(req: NextRequest) {
2727 return NextResponse . json ( { error : 'No signature' } , { status : 400 } ) ;
2828 }
2929
30- // Verify webhook signature
30+ // Verify webhook signature (constant-time)
3131 const expectedSignature = crypto
3232 . createHmac ( 'sha256' , WEBHOOK_SECRET )
3333 . update ( body )
3434 . digest ( 'hex' ) ;
3535
36- if ( signature !== expectedSignature ) {
36+ const providedBuf = Buffer . from ( signature , 'hex' ) ;
37+ const expectedBuf = Buffer . from ( expectedSignature , 'hex' ) ;
38+
39+ if ( providedBuf . length !== expectedBuf . length || ! crypto . timingSafeEqual ( providedBuf , expectedBuf ) ) {
3740 console . log ( '❌ Invalid webhook signature' ) ;
3841 return NextResponse . json ( { error : 'Invalid signature' } , { status : 400 } ) ;
3942 }
@@ -140,14 +143,19 @@ async function handlePaymentFailed(payment: any) {
140143 console . log ( '❌ Payment failed:' , payment . id ) ;
141144
142145 try {
143- // Find and update payment record
146+ // Find and update payment record (fallback by order id)
144147 const paymentRecord = await prisma . payment . findFirst ( {
145- where : { razorpayPaymentId : payment . id }
148+ where : {
149+ OR : [
150+ { razorpayPaymentId : payment . id } ,
151+ { razorpayOrderId : payment . order_id }
152+ ]
153+ }
146154 } ) ;
147155
148156 if ( paymentRecord ) {
149- await prisma . payment . update ( {
150- where : { id : paymentRecord . id } ,
157+ await prisma . payment . updateMany ( {
158+ where : { id : paymentRecord . id , status : { not : 'COMPLETED' } } ,
151159 data : {
152160 status : 'FAILED' ,
153161 failureReason : payment . error_description || 'Payment failed' ,
0 commit comments