Processing Stripe Dance Payments
This commit is contained in:
@@ -5,6 +5,7 @@ import QuillDB from "../_/quilldb.js"
|
||||
import Titles from "./model/Titles.js"
|
||||
import Members from './model/Members.js'
|
||||
import Tokens from './model/Tokens.js'
|
||||
import Payments from "./model/Payments.js"
|
||||
import Posts from "./model/Forum/Posts.js"
|
||||
import Conversations from "./model/Messages/Conversations.js"
|
||||
import Messages from "./model/Messages/Messages.js"
|
||||
@@ -13,6 +14,7 @@ export default class Database {
|
||||
titles = new Titles()
|
||||
members = new Members()
|
||||
tokens = new Tokens()
|
||||
payments = new Payments()
|
||||
posts = new Posts()
|
||||
conversations = new Conversations()
|
||||
messages = new Messages()
|
||||
@@ -21,6 +23,7 @@ export default class Database {
|
||||
"HY": this.titles,
|
||||
"MEMBER": this.members,
|
||||
"TOKEN": this.tokens,
|
||||
"PAYMENT": this.payments,
|
||||
"POST": this.posts,
|
||||
"CONVERSATION": this.conversations,
|
||||
"DM": this.messages
|
||||
@@ -79,8 +82,8 @@ export default class Database {
|
||||
this.tokens.entries,
|
||||
this.posts.entries,
|
||||
this.conversations.entries,
|
||||
this.messages.entries
|
||||
|
||||
this.messages.entries,
|
||||
this.payments.entries,
|
||||
]
|
||||
let ids = [
|
||||
Object.entries(this.titles.ids),
|
||||
@@ -89,7 +92,7 @@ export default class Database {
|
||||
Object.entries(this.posts.ids),
|
||||
Object.entries(this.conversations.ids),
|
||||
Object.entries(this.messages.ids),
|
||||
|
||||
Object.entries(this.payments.ids),
|
||||
]
|
||||
for(let i=0; i<arrs.length; i++) {
|
||||
let arr = arrs[i]
|
||||
|
||||
42
server/db/model/Payments.js
Normal file
42
server/db/model/Payments.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
const { z } = require("zod")
|
||||
|
||||
export default class Payments extends OrderedObject {
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
email: z.string(),
|
||||
time: z.string(),
|
||||
amount: z.number(),
|
||||
product: z.string(),
|
||||
})
|
||||
|
||||
save(payment) {
|
||||
let id = `PAYMENT-${payment.id}`
|
||||
let result = this.schema.safeParse(payment)
|
||||
if(result.success) {
|
||||
try {
|
||||
super.add(id, payment)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
console.error(result.error)
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
add(paymentObj) {
|
||||
let toSave = {
|
||||
id: this.entries.length+1,
|
||||
...paymentObj
|
||||
}
|
||||
this.save(toSave)
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.entries[this.ids[`PAYMENT-${id}`]]
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import "./util.js"
|
||||
import Socket from './ws/ws.js'
|
||||
import Database from "./db/db.js"
|
||||
import AuthHandler from './auth.js';
|
||||
import PaymentsHandler from "./payments.js"
|
||||
|
||||
class Server {
|
||||
db;
|
||||
@@ -20,10 +21,16 @@ class Server {
|
||||
DBPath = path.join(__dirname, './db')
|
||||
|
||||
registerRoutes(router) {
|
||||
// router.post('/api/location', handlers.updateLocation)
|
||||
/* Stripe */
|
||||
router.post("/create-checkout-session", PaymentsHandler.danceTicket)
|
||||
router.post("/webhook", express.raw({ type: "application/json" }), PaymentsHandler.webhook)
|
||||
|
||||
/* Auth */
|
||||
router.post('/login', this.auth.login)
|
||||
router.get('/profile', this.auth.getProfile)
|
||||
router.get('/signout', this.auth.logout)
|
||||
|
||||
/* Site */
|
||||
router.get('/signup', this.verifyToken, this.get)
|
||||
router.post('/signup', this.verifyToken, this.newUserSubmission)
|
||||
router.get('/db/images/*', this.getUserImage)
|
||||
@@ -162,6 +169,7 @@ class Server {
|
||||
global.db = this.db
|
||||
this.auth = new AuthHandler()
|
||||
const app = express();
|
||||
app.post("/webhook", express.raw({ type: "application/json" }), PaymentsHandler.webhook)
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
68
server/payments.js
Normal file
68
server/payments.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const Stripe = require("stripe")
|
||||
const dotenv = require("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