add payment service

This commit is contained in:
Emily
2025-03-26 15:30:22 +01:00
parent 94a28b31d3
commit 1f9ef5d18c
13 changed files with 1542 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import { json, Router } from 'express';
import z from 'zod';
import { getPlanFromId } from '../shared/data/PLANS';
import StripeService from '../services/StripeService';
import { sendJson } from '../Utils';
import { ProjectModel } from '../shared/schema/project/ProjectSchema';
export const paymentRouter = Router();
export const ZBodyCreatePayment = z.object({
pid: z.string(),
plan_id: z.number()
})
paymentRouter.post('/create', json(), async (req, res) => {
try {
const createPaymentData = ZBodyCreatePayment.parse(req.body);
const plan = getPlanFromId(createPaymentData.plan_id);
if (!plan) return sendJson(res, 400, { error: 'plan not found' });
const project = await ProjectModel.findById(createPaymentData.pid);
if (!project) return sendJson(res, 400, { error: 'project not found' });
if (!project.customer_id) return sendJson(res, 400, { error: 'project have no customer_id' });
const price = StripeService.testMode ? plan.PRICE_TEST : plan.PRICE;
const checkout = await StripeService.createPayment(
price,
'https://dashboard.litlyx.com/payment_ok',
createPaymentData.pid,
project.customer_id
);
if (!checkout) return sendJson(res, 400, { error: 'cannot create payment' });
return sendJson(res, 200, { url: checkout.url });
} catch (ex) {
res.status(500).json({ error: ex.message });
}
});