Skip to content

Commit

Permalink
success url
Browse files Browse the repository at this point in the history
  • Loading branch information
makon1234 committed Mar 12, 2024
1 parent decb45b commit 1af481b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/pages/api/success.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// pages/api/success.ts
import { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';

const apiKey: string | undefined = process.env.STRIPE_SECRET_KEY;

if (!apiKey) {
throw new Error("Stripe API key is not defined.");
}

const stripe = new Stripe(apiKey);

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
// Extract the session_id from the query parameters
const sessionId = req.query.session_id as string;

try {
// Fetch the session details from Stripe
const session = await stripe.checkout.sessions.retrieve(sessionId);

// Check if the payment was successful
if (session.payment_status === 'paid') {
// Display a success message
res.redirect('/donations');
res.status(200).send("Payment was successful.");

// Redirect back to the donations page after a delay
} else {
// Handle unsuccessful payment
res.status(400).send("Payment was not successful.");
}
} catch (error) {
console.error("Error fetching session details:", error);
res.status(500).send("Error fetching session details.");
}
}




0 comments on commit 1af481b

Please sign in to comment.