stripe payment endpoints created

This commit is contained in:
Sanidhya Kumar Verma
2024-09-04 10:17:45 +00:00
committed by GitHub
parent c5c50e75db
commit eb7e052d75
3 changed files with 179 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
import { Stripe } from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const body = await req.json();
const { name, email, amount } = body;
if (!name || !email || !amount)
return new Response(
JSON.stringify({ error: "Please enter a valid email address" }),
{ status: 404 },
);
try {
let customer;
const existingCustomer = await stripe.customers.list({ email });
if (existingCustomer.data.length > 0) customer = existingCustomer.data[0];
else {
customer = await stripe.customers.create({
name,
email,
});
}
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: "2024-06-20" },
);
const paymentIntent = await stripe.paymentIntents.create({
amount: parseInt(amount) * 100,
currency: "usd",
customer: customer.id,
automatic_payment_methods: {
enabled: true,
allow_redirects: "never",
},
});
return new Response(
JSON.stringify({
paymentIntent: paymentIntent,
ephemeralKey: ephemeralKey,
customer: customer.id,
}),
);
} catch (err) {
console.log("[PAYMENT_CREATE]: ", err);
return new Response(
JSON.stringify({
error: "Internal Server Error",
}),
{ status: 500 },
);
}
}