mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add pricing
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { checkProjectCount } from '@functions/UtilsProjectCounts';
|
||||
|
||||
export async function getAiChatRemainings(project_id: string) {
|
||||
const counts = await checkProjectCount(project_id)
|
||||
if (!counts) return 0;
|
||||
const chatsRemaining = counts.ai_limit - counts.ai_messages;
|
||||
const limits = await ProjectLimitModel.findOne({ _id: project_id })
|
||||
if (!limits) return 0;
|
||||
const chatsRemaining = limits.ai_limit - limits.ai_messages;
|
||||
if (isNaN(chatsRemaining)) return 0;
|
||||
return chatsRemaining;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ import { OAuth2Client } from 'google-auth-library';
|
||||
import { createUserJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import EmailService from '@services/EmailService';
|
||||
import { ProjectModel } from '@schema/ProjectSchema';
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
|
||||
|
||||
@@ -11,6 +13,8 @@ const client = new OAuth2Client({
|
||||
clientSecret: GOOGLE_AUTH_CLIENT_SECRET
|
||||
});
|
||||
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const body = await readBody(event)
|
||||
|
||||
@@ -33,8 +37,10 @@ export default defineEventHandler(async event => {
|
||||
|
||||
|
||||
const user = await UserModel.findOne({ email: payload.email });
|
||||
|
||||
if (user) return { error: false, access_token: createUserJwt({ email: user.email, name: user.name }) }
|
||||
|
||||
|
||||
const newUser = new UserModel({
|
||||
email: payload.email,
|
||||
given_name: payload.given_name,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PREMIUM_PLANS, STRIPE_PLANS } from "@data/PREMIUM_LIMITS";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
@@ -17,24 +16,22 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const { planId } = body;
|
||||
|
||||
const plan = PREMIUM_PLANS.find(e => e.id == planId);
|
||||
const PLAN = getPlanFromId(planId);
|
||||
|
||||
if (!plan) {
|
||||
if (!PLAN) {
|
||||
console.error('PLAN', planId, 'NOT EXIST');
|
||||
return setResponseStatus(event, 400, 'Plan not exist');
|
||||
}
|
||||
|
||||
const { price } = STRIPE_PLANS[plan.tag];
|
||||
|
||||
const checkout = await StripeService.cretePayment(
|
||||
price,
|
||||
PLAN.PRICE,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
project_id,
|
||||
project.customer_id
|
||||
);
|
||||
|
||||
if (!checkout) {
|
||||
console.error('Cannot create payment', { plan, price });
|
||||
console.error('Cannot create payment', { plan: PLAN });
|
||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
@@ -21,17 +22,21 @@ export default defineEventHandler(async event => {
|
||||
|
||||
if (!project.customer_id) return [];
|
||||
|
||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
||||
return await Redis.useCache({ key: `invoices:${project_id}`, exp: 10 }, async () => {
|
||||
|
||||
return invoices?.data.map(e => {
|
||||
const result: InvoiceData = {
|
||||
link: e.invoice_pdf || '',
|
||||
id: e.number || '',
|
||||
date: e.created * 1000,
|
||||
status: e.status || 'NO_STATUS',
|
||||
cost: e.amount_due
|
||||
}
|
||||
return result;
|
||||
})
|
||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
||||
|
||||
return invoices?.data.map(e => {
|
||||
const result: InvoiceData = {
|
||||
link: e.invoice_pdf || '',
|
||||
id: e.number || '',
|
||||
date: e.created * 1000,
|
||||
status: e.status || 'NO_STATUS',
|
||||
cost: e.amount_due
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -2,69 +2,187 @@
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import type Event from 'stripe';
|
||||
import { ProjectModel } from '@schema/ProjectSchema';
|
||||
import { PREMIUM_LIMITS, getPlanFromPremiumTag, getPlanTagFromStripePrice } from '@data/PREMIUM_LIMITS';
|
||||
import { PREMIUM_PLAN, getPlanFromPrice } from '@data/PREMIUM';
|
||||
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||
import { ProjectLimitModel } from '@schema/ProjectsLimits';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
|
||||
async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
|
||||
if (event.data.object.status === 'paid') {
|
||||
// if (event.data.object.status === 'paid') {
|
||||
|
||||
const pid = event.data.object.subscription_details?.metadata?.pid;
|
||||
// const data = event.data.object;
|
||||
|
||||
const project = await ProjectModel.findById(pid);
|
||||
if (!project) return { error: 'Project not found' }
|
||||
// const pid = data.subscription_details?.metadata?.pid;
|
||||
// if (!pid) return { error: 'ProjectId not found' }
|
||||
|
||||
const subscriptionId = event.data.object.subscription;
|
||||
if (!subscriptionId) return { error: 'SubscriptionId not found' }
|
||||
// const project = await ProjectModel.findById(pid);
|
||||
// if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.lines.data[0].plan?.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
// const price = data.lines.data[0].plan?.id;
|
||||
// if (!price) return { error: 'Price not found' }
|
||||
|
||||
const premiumTag = getPlanTagFromStripePrice(price);
|
||||
if (!premiumTag) return { error: 'Premium tag not found' }
|
||||
// const PLAN = getPlanFromPrice(price);
|
||||
// if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
const plan = getPlanFromPremiumTag(premiumTag);
|
||||
if (!plan) return { error: 'Plan not found' }
|
||||
// await ProjectModel.updateOne({ _id: pid }, {
|
||||
// premium: true,
|
||||
// customer_id: data.customer,
|
||||
// premium_type: PLAN.ID,
|
||||
// premium_expire_at: data.lines.data[0].period.end * 1000
|
||||
// });
|
||||
|
||||
await ProjectModel.updateOne({ _id: pid }, {
|
||||
premium: true,
|
||||
customer_id: event.data.object.customer,
|
||||
premium_type: plan.id,
|
||||
premium_expire_at: event.data.object.lines.data[0].period.end * 1000
|
||||
});
|
||||
// await ProjectCountModel.create({
|
||||
// project_id: project._id,
|
||||
// events: 0,
|
||||
// visits: 0,
|
||||
// ai_messages: 0,
|
||||
// limit: PLAN.COUNT_LIMIT,
|
||||
// ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
// billing_start_at: event.data.object.lines.data[0].period.start * 1000,
|
||||
// billing_expire_at: event.data.object.lines.data[0].period.end * 1000,
|
||||
// });
|
||||
|
||||
const limits = PREMIUM_LIMITS[premiumTag];
|
||||
|
||||
await ProjectCountModel.create({
|
||||
project_id: project._id,
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: limits.COUNT_LIMIT,
|
||||
ai_limit: limits.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.lines.data[0].period.start * 1000,
|
||||
billing_expire_at: event.data.object.lines.data[0].period.end * 1000,
|
||||
});
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
// return { ok: true }
|
||||
// }
|
||||
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.items.data[0].price.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
|
||||
const PLAN = getPlanFromPrice(price);
|
||||
if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
|
||||
if (project.subscription_id != event.data.object.id) {
|
||||
await StripeService.deleteSubscription(project.subscription_id);
|
||||
}
|
||||
|
||||
project.premium = PLAN.ID != 0;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = event.data.object.id;
|
||||
project.premium_expire_at = new Date(event.data.object.current_period_end * 1000);
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({
|
||||
customer_id: event.data.object.customer,
|
||||
subscription_id: event.data.object.id
|
||||
});
|
||||
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const PLAN = PREMIUM_PLAN['FREE'];
|
||||
|
||||
const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
||||
|
||||
let customer: Event.Customer;
|
||||
|
||||
if (!targetCustomer.deleted) {
|
||||
customer = targetCustomer;
|
||||
} else {
|
||||
const user = await UserModel.findById(project._id, { email: 1 });
|
||||
if (!user) return { error: 'User not found' }
|
||||
const newCustomer = await StripeService.createCustomer(user.email);
|
||||
customer = newCustomer;
|
||||
}
|
||||
|
||||
const freeSubscription = await StripeService.createFreeSubscription(customer.id);
|
||||
|
||||
|
||||
project.premium = false;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = freeSubscription.id;
|
||||
project.premium_expire_at = new Date(freeSubscription.current_period_end * 1000);
|
||||
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({
|
||||
customer_id: event.data.object.customer,
|
||||
subscription_id: event.data.object.id
|
||||
});
|
||||
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.items.data[0].price.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
|
||||
const PLAN = getPlanFromPrice(price);
|
||||
if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
project.premium = PLAN.ID != 0;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = event.data.object.id;
|
||||
project.premium_expire_at = new Date(event.data.object.current_period_end * 1000);
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const body = await readRawBody(event);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -15,9 +16,23 @@ export default defineEventHandler(async event => {
|
||||
const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (existingUserProjects == 3) return setResponseStatus(event, 400, 'Already have 3 projects');
|
||||
|
||||
const newProject = new ProjectModel({ owner: userData.id, name: newProjectName });
|
||||
const saved = await newProject.save();
|
||||
const customer = await StripeService.createCustomer(userData.user.email);
|
||||
if (!customer) return setResponseStatus(event, 400, 'Error creating customer');
|
||||
|
||||
return saved.toJSON() as TProject;
|
||||
const subscription = await StripeService.createFreeSubscription(customer.id);
|
||||
if (!subscription) return setResponseStatus(event, 400, 'Error creating subscription');
|
||||
|
||||
const project = await ProjectModel.create({
|
||||
owner: userData.id,
|
||||
name: newProjectName,
|
||||
premium: false,
|
||||
premium_type: 0,
|
||||
customer_id: customer.id,
|
||||
subscription_id: subscription.id,
|
||||
premium_expire_at: subscription.current_period_end * 1000
|
||||
});
|
||||
|
||||
|
||||
return project.toJSON() as TProject;
|
||||
|
||||
});
|
||||
@@ -1,4 +1,7 @@
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -9,10 +12,29 @@ export default defineEventHandler(async event => {
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const project = await ProjectModel.findById(projectId);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not exist');
|
||||
|
||||
const projects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
|
||||
|
||||
const deletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
return { ok: deletation.acknowledged };
|
||||
if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
|
||||
|
||||
await StripeService.deleteCustomer(project.customer_id);
|
||||
|
||||
const countDeletation = await ProjectCountModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
const limitdeletation = await ProjectLimitModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
const projectDeletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
|
||||
const ok = countDeletation.acknowledged && limitdeletation.acknowledged && projectDeletation.acknowledged
|
||||
|
||||
return {
|
||||
ok,
|
||||
data: [
|
||||
countDeletation.acknowledged,
|
||||
limitdeletation.acknowledged,
|
||||
projectDeletation.acknowledged
|
||||
]
|
||||
};
|
||||
|
||||
});
|
||||
@@ -1,8 +1,7 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
|
||||
const { BROKER_UPDATE_EXPIRE_TIME_PATH } = useRuntimeConfig();
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -17,25 +16,20 @@ export default defineEventHandler(async event => {
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
const subscription = await StripeService.getSubscription(project.subscription_id);
|
||||
|
||||
let projectCounts = await ProjectCountModel.findOne({ project_id }, {}, {
|
||||
sort: { billing_expire_at: -1 }
|
||||
});
|
||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
||||
if (!projectLimits) return setResponseStatus(event, 400, 'Project limits not found');
|
||||
|
||||
if (!projectCounts || Date.now() > new Date(projectCounts.billing_expire_at).getTime()) {
|
||||
await fetch(BROKER_UPDATE_EXPIRE_TIME_PATH + project._id.toString());
|
||||
projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { sort: { billing_expire_at: -1 } });
|
||||
}
|
||||
|
||||
if (!projectCounts) return setResponseStatus(event, 400, 'Project counts not found');
|
||||
|
||||
const result = {
|
||||
premium: project.premium,
|
||||
premium_type: project.premium_type,
|
||||
billing_start_at: projectCounts.billing_start_at,
|
||||
billing_expire_at: projectCounts.billing_expire_at,
|
||||
limit: projectCounts.limit,
|
||||
count: projectCounts.events + projectCounts.visits,
|
||||
billing_start_at: projectLimits.billing_start_at,
|
||||
billing_expire_at: projectLimits.billing_expire_at,
|
||||
limit: projectLimits.limit,
|
||||
count: projectLimits.events + projectLimits.visits,
|
||||
subscription_status: subscription.status
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user