mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
implementing new payment system + rewrite deploy scripts
This commit is contained in:
66
payments/src/controllers/WebhookController.ts
Normal file
66
payments/src/controllers/WebhookController.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
import type Event from 'stripe';
|
||||
import StripeService from '../services/StripeService';
|
||||
import { getPlanFromPrice, PLAN_DATA } from '../shared/data/PLANS';
|
||||
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
||||
import { UserLimitModel } from '../shared/schema/UserLimitSchema';
|
||||
|
||||
import { EmailService } from '../shared/services/EmailService';
|
||||
|
||||
|
||||
|
||||
async function addSubscriptionToUser(user_id: string, plan: PLAN_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
||||
|
||||
await PremiumModel.updateOne({ _id: user_id }, {
|
||||
premium_type: plan.ID,
|
||||
subscription_id,
|
||||
expire_at: current_period_end * 1000
|
||||
}, { upsert: true });
|
||||
|
||||
await UserLimitModel.updateOne({ _id: user_id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: plan.COUNT_LIMIT,
|
||||
ai_limit: plan.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: current_period_start * 1000,
|
||||
billing_expire_at: current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
}
|
||||
|
||||
|
||||
export async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
|
||||
const customer_id = event.data.object.customer;
|
||||
const premiumData = await PremiumModel.findOne({ customer_id });
|
||||
if (!premiumData) return { error: 'customer not found' }
|
||||
|
||||
if (event.data.object.status !== 'paid') return { received: true, warn: 'payment status not paid' }
|
||||
|
||||
const subscription_id = event.data.object.subscription as string;
|
||||
|
||||
const price = event.data.object.lines.data[0].price.id;
|
||||
if (!price) return { error: 'price not found' }
|
||||
|
||||
const plan = getPlanFromPrice(price, StripeService.testMode);
|
||||
if (!plan) return { error: 'plan not found' }
|
||||
|
||||
const databaseSubscription = premiumData.subscription_id;
|
||||
|
||||
if (databaseSubscription != subscription_id) {
|
||||
await StripeService.deleteSubscription(subscription_id);
|
||||
}
|
||||
|
||||
await addSubscriptionToUser(premiumData.user_id.toString(), plan, subscription_id, event.data.object.period_start, event.data.object.period_end);
|
||||
|
||||
setTimeout(() => {
|
||||
if (plan.ID == 0) return;
|
||||
//TODO: Email service template
|
||||
// const emailData = EmailService.getEmailServerInfo('purchase', { target: user.email, projectName: project.name });
|
||||
// EmailServiceHelper.sendEmail(emailData);
|
||||
}, 1);
|
||||
|
||||
return { ok: true };
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import express from 'express';
|
||||
import StripeService from './services/StripeService'
|
||||
import { webhookRouter } from './routers/WebhookRouter';
|
||||
import { paymentRouter } from './routers/PaymentRouter';
|
||||
|
||||
|
||||
const STRIPE_PRIVATE_KEY = process.env.STRIPE_PRIVATE_KEY;
|
||||
const STRIPE_WEBHOOK_SECRET = process.env.STRIPE_WEBHOOK_SECRET;
|
||||
const STRIPE_TESTMODE = process.env.STRIPE_TESTMODE === 'true';
|
||||
|
||||
StripeService.init(STRIPE_PRIVATE_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_TESTMODE);
|
||||
|
||||
console.log('Stripe started in', STRIPE_TESTMODE ? 'TESTMODE' : 'LIVEMODE');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/webhook', webhookRouter);
|
||||
app.use('/payment', paymentRouter);
|
||||
|
||||
const port = parseInt(process.env.PORT);
|
||||
if (!port) {
|
||||
console.error('PORT is not set');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
app.listen(port, () => console.log(`Listening on port ${port}`));
|
||||
@@ -3,13 +3,13 @@ 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';
|
||||
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
||||
|
||||
export const paymentRouter = Router();
|
||||
|
||||
|
||||
export const ZBodyCreatePayment = z.object({
|
||||
pid: z.string(),
|
||||
user_id: z.string(),
|
||||
plan_id: z.number()
|
||||
})
|
||||
|
||||
@@ -20,17 +20,17 @@ paymentRouter.post('/create', json(), async (req, res) => {
|
||||
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 premiumData = await PremiumModel.findById(createPaymentData.user_id);
|
||||
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
||||
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user 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
|
||||
createPaymentData.user_id,
|
||||
premiumData.customer_id
|
||||
);
|
||||
|
||||
if (!checkout) return sendJson(res, 400, { error: 'cannot create payment' });
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
|
||||
import { json, Router } from 'express';
|
||||
import { sendJson } from '../Utils';
|
||||
import StripeService from '../services/StripeService';
|
||||
|
||||
import * as WebhookController from '../controllers/WebhookController'
|
||||
|
||||
export const webhookRouter = Router();
|
||||
|
||||
@@ -8,10 +12,21 @@ webhookRouter.get('/', json(), async (req, res) => {
|
||||
try {
|
||||
|
||||
const signature = req.header('stripe-signature');
|
||||
if (!signature) {
|
||||
console.error('No signature on the webhook')
|
||||
}
|
||||
if (!signature) return sendJson(res, 400, { error: 'No signature' });
|
||||
|
||||
const eventData = StripeService.parseWebhook(req.body, signature);
|
||||
if (!eventData) return sendJson(res, 400, { error: 'Error parsing event data' });
|
||||
|
||||
if (eventData.type === 'invoice.paid') {
|
||||
const response = await WebhookController.onPaymentSuccess(eventData);
|
||||
return sendJson(res, 200, response);
|
||||
}
|
||||
|
||||
// if (eventData.type === 'payment_intent.succeeded') return await onPaymentOnetimeSuccess(eventData);
|
||||
// if (eventData.type === 'invoice.payment_failed') return await onPaymentFailed(eventData);
|
||||
// if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
|
||||
// if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
|
||||
// if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
|
||||
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
|
||||
@@ -51,7 +51,7 @@ class StripeService {
|
||||
return checkout;
|
||||
}
|
||||
|
||||
async createPayment(price: string, success_url: string, pid: string, customer: string) {
|
||||
async createPayment(price: string, success_url: string, user_id: string, customer: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
|
||||
const checkout = await this.stripe.checkout.sessions.create({
|
||||
@@ -61,7 +61,7 @@ class StripeService {
|
||||
{ price, quantity: 1 }
|
||||
],
|
||||
subscription_data: {
|
||||
metadata: { pid },
|
||||
metadata: { user_id },
|
||||
},
|
||||
customer,
|
||||
success_url,
|
||||
|
||||
Reference in New Issue
Block a user