init
This commit is contained in:
68
server/payments.js
Normal file
68
server/payments.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import Stripe from 'stripe';
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET);
|
||||
|
||||
export default class PaymentsHandler {
|
||||
|
||||
static async danceTicket(req, res) {
|
||||
try {
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
mode: "payment",
|
||||
payment_method_types: ["card"],
|
||||
metadata: {
|
||||
productId: "austin_winter_ball_2025_ticket"
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: "usd",
|
||||
product_data: {
|
||||
name: "Hyperia Winter Ball"
|
||||
},
|
||||
unit_amount: 3500
|
||||
},
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
success_url: `${process.env.PROTOCOL + process.env.BASE_URL}/success`,
|
||||
cancel_url: `${process.env.PROTOCOL + process.env.BASE_URL}/events`
|
||||
});
|
||||
|
||||
res.json({ url: session.url });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Something went wrong." });
|
||||
}
|
||||
}
|
||||
|
||||
static webhook = (req, res) => {
|
||||
const sig = req.headers["stripe-signature"];
|
||||
|
||||
try {
|
||||
const event = stripe.webhooks.constructEvent(
|
||||
req.body,
|
||||
sig,
|
||||
process.env.WEBHOOK_SECRET
|
||||
);
|
||||
|
||||
if (event.type === "checkout.session.completed") {
|
||||
const session = event.data.object;
|
||||
let toStore = {
|
||||
"product": session.metadata.productId,
|
||||
"email": session.customer_details.email,
|
||||
"name": session.customer_details.name,
|
||||
"time": global.currentTime(),
|
||||
"amount": session.amount_total,
|
||||
}
|
||||
global.db.payments.add(toStore)
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.sendStatus(400);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user