Created
January 13, 2025 10:08
-
-
Save sagaratalatti/f286a293661c51b5896ed5755283bddb to your computer and use it in GitHub Desktop.
Promptys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Razorpay = require("razorpay"); | |
export default async function handler(req, res) { | |
// Allow CORS | |
res.setHeader("Access-Control-Allow-Origin", "https://supaprompts.dcms.site"); | |
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); | |
res.setHeader("Access-Control-Allow-Headers", "Content-Type"); | |
// Handle preflight requests (OPTIONS) | |
if (req.method === "OPTIONS") { | |
return res.status(200).end(); | |
} | |
const { payment_id } = req.query; | |
// Initialize Razorpay client | |
const razorpay = new Razorpay({ | |
key_id: process.env.RAZORPAY_KEY_ID, // Set in Vercel environment variables | |
key_secret: process.env.RAZORPAY_KEY_SECRET, // Set in Vercel environment variables | |
}); | |
if (req.method === "GET" && payment_id) { | |
try { | |
// Step 1: Fetch payment details | |
const payment = await razorpay.payments.fetch(payment_id); | |
console.log("Payment details:", payment); | |
// Step 2: Check payment status | |
if (payment.status === "authorized") { | |
// Step 3: Capture the payment | |
const capturedPayment = await razorpay.payments.capture(payment_id, payment.amount); | |
console.log("Payment captured:", capturedPayment); | |
} | |
// Step 4: Validate the captured payment | |
const capturedPaymentDetails = await razorpay.payments.fetch(payment_id); | |
if (capturedPaymentDetails.status === "captured") { | |
console.log("Payment captured successfully!"); | |
// Google Drive Direct Download Link | |
const downloadLink = "https://drive.google.com/uc?export=download&id=1qcwrwth8MVCkT0A17EXw-p5d98X75nMq"; | |
res.status(200).json({ status: "success", download_link: downloadLink }); | |
} else { | |
res.status(400).json({ error: "Payment not captured. Please try again." }); | |
} | |
} catch (error) { | |
console.error("Error during payment processing:", error); | |
res.status(500).json({ error: "Internal Server Error. Payment validation failed." }); | |
} | |
} else { | |
res.status(400).json({ error: "Invalid request. Payment ID is required." }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment