MCPcopy Create free account
hub / github.com/CodeWithHarry/Sigma-Web-Dev-Course / POST

Function POST

Video 131/app/api/razorpay/route.js:8–36  ·  view source on GitHub ↗
(req)

Source from the content-addressed store, hash-verified

6import User from "@/models/User";
7
8export const POST = async (req) => {
9 await connectDb()
10 let body = await req.formData()
11 body = Object.fromEntries(body)
12
13 // Check if razorpayOrderId is present on the server
14 let p = await Payment.findOne({oid: body.razorpay_order_id})
15 if(!p){
16 return NextResponse.json({success: false, message:"Order Id not found"})
17 }
18
19 // fetch the secret of the user who is getting the payment
20 let user = await User.findOne({username: p.to_user})
21 const secret = user.razorpaysecret
22
23 // Verify the payment
24 let xx = validatePaymentVerification({"order_id": body.razorpay_order_id, "payment_id": body.razorpay_payment_id}, body.razorpay_signature, secret)
25
26 if(xx){
27 // Update the payment status
28 const updatedPayment = await Payment.findOneAndUpdate({oid: body.razorpay_order_id}, {done: "true"}, {new: true})
29 return NextResponse.redirect(`${process.env.NEXT_PUBLIC_URL}/${updatedPayment.to_user}?paymentdone=true`)
30 }
31
32 else{
33 return NextResponse.json({success: false, message:"Payment Verification Failed"})
34 }
35
36}

Callers

nothing calls this directly

Calls 1

connectDbFunction · 0.85

Tested by

no test coverage detected