add pricing

This commit is contained in:
Emily
2024-06-05 15:40:51 +02:00
parent f7891a94cd
commit 854d6eb528
22 changed files with 435 additions and 294 deletions

View File

@@ -1,9 +1,9 @@
import { TProjectCount } from "@schema/ProjectsCounts";
import { ProjectModel } from "@schema/ProjectSchema"; import { ProjectModel } from "@schema/ProjectSchema";
import { UserModel } from "@schema/UserSchema"; import { UserModel } from "@schema/UserSchema";
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema"; import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
import EmailService from '@services/EmailService'; import EmailService from '@services/EmailService';
import { requireEnv } from "../../shared/utilts/requireEnv"; import { requireEnv } from "../../shared/utilts/requireEnv";
import { TProjectLimit } from "@schema/ProjectsLimits";
EmailService.createTransport( EmailService.createTransport(
@@ -13,7 +13,7 @@ EmailService.createTransport(
requireEnv('EMAIL_PASS'), requireEnv('EMAIL_PASS'),
); );
export async function checkLimitsForEmail(projectCounts: TProjectCount) { export async function checkLimitsForEmail(projectCounts: TProjectLimit) {
if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit / 2)) { if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit / 2)) {
const notify = await LimitNotifyModel.findOne({ project_id: projectCounts._id }); const notify = await LimitNotifyModel.findOne({ project_id: projectCounts._id });

View File

@@ -1,7 +1,6 @@
import { Router, json } from "express"; import { Router, json } from "express";
import { createSessionHash, getIPFromRequest } from "../../utils/Utils"; import { createSessionHash, getIPFromRequest } from "../../utils/Utils";
import { checkProjectCount } from "@functions/UtilsProjectCounts";
import { SessionModel } from "@schema/metrics/SessionSchema"; import { SessionModel } from "@schema/metrics/SessionSchema";
import { EVENT_LOG_LIMIT_PERCENT } from '@data/broker/Limits'; import { EVENT_LOG_LIMIT_PERCENT } from '@data/broker/Limits';
@@ -13,6 +12,8 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
import { EventModel } from "@schema/metrics/EventSchema"; import { EventModel } from "@schema/metrics/EventSchema";
import { ProjectCountModel } from "@schema/ProjectsCounts"; import { ProjectCountModel } from "@schema/ProjectsCounts";
import { checkLimitsForEmail } from "../../Controller"; import { checkLimitsForEmail } from "../../Controller";
import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { ProjectModel } from "@schema/ProjectSchema";
const router = Router(); const router = Router();
@@ -55,13 +56,21 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
const { pid } = req.body; const { pid } = req.body;
const projectCounts = await checkProjectCount(pid); const projectExist = await ProjectModel.exists({ _id: pid });
if (!projectExist) return res.status(400).json({ error: 'Project not exist' });
const projectLimits = await ProjectLimitModel.findOne({ project_id: pid });
if (!projectLimits) return res.status(400).json({ error: 'No limits found' });
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
const COUNT_LIMIT = projectLimits.limit;
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > COUNT_LIMIT) {
return res.status(200).json({ error: 'Limit reached' });
};
await checkLimitsForEmail(projectLimits);
const TOTAL_COUNT = projectCounts.events + projectCounts.visits;
const LIMIT = projectCounts.limit;
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > LIMIT) return;
await checkLimitsForEmail(projectCounts);
const ip = getIPFromRequest(req); const ip = getIPFromRequest(req);
@@ -113,7 +122,11 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
const fieldToInc = type === EventType.VISIT ? 'visits' : 'events'; const fieldToInc = type === EventType.VISIT ? 'visits' : 'events';
await ProjectCountModel.updateOne({ _id: projectCounts._id }, { $inc: { [fieldToInc]: 1 } }); await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } }, { upsert: true });
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } });
return res.sendStatus(200); return res.sendStatus(200);

View File

@@ -5,16 +5,22 @@ export type PricingCardProp = {
cost: string, cost: string,
features: string[], features: string[],
desc: string, desc: string,
active: boolean active: boolean,
planId: number
} }
const props = defineProps<{ data: PricingCardProp }>(); const props = defineProps<{ data: PricingCardProp }>();
const activeProject = useActiveProject();
const router = useRouter(); async function onUpgradeClick() {
const res = await $fetch<string>(`/api/pay/${activeProject.value?._id.toString()}/create`, {
function onUpgradeClick() { ...signHeaders({ 'content-type': 'application/json' }),
router.push('/book_demo') method: 'POST',
body: JSON.stringify({ planId: props.data.planId })
})
if (!res) alert('Something went wrong');
window.open(res);
} }
</script> </script>
@@ -37,7 +43,8 @@ function onUpgradeClick() {
<div v-if="data.active" class="text-[1rem] bg-[#1f1f22] rounded-md py-2 text-center"> <div v-if="data.active" class="text-[1rem] bg-[#1f1f22] rounded-md py-2 text-center">
Current active plan Current active plan
</div> </div>
<div @click="onUpgradeClick()" v-if="!data.active" class="cursor-pointer text-[1rem] font-semibold bg-[#3a3af5] rounded-md py-2 text-center"> <div @click="onUpgradeClick()" v-if="!data.active"
class="cursor-pointer text-[1rem] font-semibold bg-[#3a3af5] rounded-md py-2 text-center">
Upgrade Upgrade
</div> </div>
</div> </div>

View File

@@ -22,7 +22,8 @@ const starterTierCardData = ref<PricingCardProp>({
can experience some data loss.To have a can experience some data loss.To have a
dedicated server we suggest to upgrade the dedicated server we suggest to upgrade the
plan to an higher one!`, plan to an higher one!`,
active: activeProject.value?.premium === false active: activeProject.value?.premium === false,
planId: 0
}); });
const accelerationTierCardData = ref<PricingCardProp>({ const accelerationTierCardData = ref<PricingCardProp>({
@@ -39,7 +40,8 @@ const accelerationTierCardData = ref<PricingCardProp>({
"Low priority email support" "Low priority email support"
], ],
desc: `Your project is entering a growth phase. We simplify data analysis for you. For more support, try our Expansion plan—it's worth it!`, desc: `Your project is entering a growth phase. We simplify data analysis for you. For more support, try our Expansion plan—it's worth it!`,
active: activeProject.value?.premium_type === 1 active: activeProject.value?.premium_type === 1,
planId: 1
}); });
const expansionTierCardData = ref<PricingCardProp>({ const expansionTierCardData = ref<PricingCardProp>({
@@ -56,7 +58,8 @@ const expansionTierCardData = ref<PricingCardProp>({
"high priority email support" "high priority email support"
], ],
desc: `We will support you with everything we can offer and give you the full power of our service. If you need more space and are growing, contact us for a custom offer!`, desc: `We will support you with everything we can offer and give you the full power of our service. If you need more space and are growing, contact us for a custom offer!`,
active: activeProject.value?.premium_type === 2 active: activeProject.value?.premium_type === 2,
planId: 2
}); });
</script> </script>

View File

@@ -75,17 +75,6 @@ function onHideClicked() {
const activeProject = useActiveProject(); const activeProject = useActiveProject();
async function payment() {
// const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
// ...signHeaders({ 'content-type': 'application/json' }),
// method: 'POST',
// body: JSON.stringify({ planId: 1 })
// })
// console.log(res);
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, signHeaders())
console.log(res);
}
</script> </script>
@@ -93,7 +82,7 @@ async function payment() {
<div class="bg-bg overflow-y-auto w-full h-dvh p-6 gap-6 flex flex-col"> <div class="bg-bg overflow-y-auto w-full h-dvh p-6 gap-6 flex flex-col">
<div @click="payment()" v-if="!isAdminHidden" <div @click="onHideClicked()" v-if="!isAdminHidden"
class="bg-menu hover:bg-menu/70 cursor-pointer flex gap-2 rounded-lg w-fit px-6 py-4 text-text-sub"> class="bg-menu hover:bg-menu/70 cursor-pointer flex gap-2 rounded-lg w-fit px-6 py-4 text-text-sub">
<div class="text-text-sub/90"> <i class="far fa-eye"></i> </div> <div class="text-text-sub/90"> <i class="far fa-eye"></i> </div>
<div> Nascondi dalla barra </div> <div> Nascondi dalla barra </div>

View File

@@ -107,6 +107,9 @@ function openInvoice(link: string) {
</div> </div>
<div class="poppins"> {{ daysLeft }} days left </div> <div class="poppins"> {{ daysLeft }} days left </div>
</div> </div>
<div class="flex justify-center">
Subscription: {{ planData.subscription_status }}
</div>
</div> </div>
</div> </div>
<div class="my-4 w-full bg-gray-400/30 h-[1px]"> <div class="my-4 w-full bg-gray-400/30 h-[1px]">

View File

@@ -1,10 +1,10 @@
import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA"; import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import { checkProjectCount } from '@functions/UtilsProjectCounts';
export async function getAiChatRemainings(project_id: string) { export async function getAiChatRemainings(project_id: string) {
const counts = await checkProjectCount(project_id) const limits = await ProjectLimitModel.findOne({ _id: project_id })
if (!counts) return 0; if (!limits) return 0;
const chatsRemaining = counts.ai_limit - counts.ai_messages; const chatsRemaining = limits.ai_limit - limits.ai_messages;
if (isNaN(chatsRemaining)) return 0; if (isNaN(chatsRemaining)) return 0;
return chatsRemaining; return chatsRemaining;
} }

View File

@@ -3,6 +3,8 @@ import { OAuth2Client } from 'google-auth-library';
import { createUserJwt } from '~/server/AuthManager'; import { createUserJwt } from '~/server/AuthManager';
import { UserModel } from '@schema/UserSchema'; import { UserModel } from '@schema/UserSchema';
import EmailService from '@services/EmailService'; 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() const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
@@ -11,6 +13,8 @@ const client = new OAuth2Client({
clientSecret: GOOGLE_AUTH_CLIENT_SECRET clientSecret: GOOGLE_AUTH_CLIENT_SECRET
}); });
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const body = await readBody(event) const body = await readBody(event)
@@ -33,8 +37,10 @@ export default defineEventHandler(async event => {
const user = await UserModel.findOne({ email: payload.email }); const user = await UserModel.findOne({ email: payload.email });
if (user) return { error: false, access_token: createUserJwt({ email: user.email, name: user.name }) } if (user) return { error: false, access_token: createUserJwt({ email: user.email, name: user.name }) }
const newUser = new UserModel({ const newUser = new UserModel({
email: payload.email, email: payload.email,
given_name: payload.given_name, given_name: payload.given_name,

View File

@@ -1,5 +1,4 @@
import { PREMIUM_PLANS, STRIPE_PLANS } from "@data/PREMIUM_LIMITS"; import { getPlanFromId } from "@data/PREMIUM";
import { ProjectModel } from "@schema/ProjectSchema";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA"; import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import StripeService from '~/server/services/StripeService'; import StripeService from '~/server/services/StripeService';
@@ -17,24 +16,22 @@ export default defineEventHandler(async event => {
const { planId } = body; 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'); console.error('PLAN', planId, 'NOT EXIST');
return setResponseStatus(event, 400, 'Plan not exist'); return setResponseStatus(event, 400, 'Plan not exist');
} }
const { price } = STRIPE_PLANS[plan.tag];
const checkout = await StripeService.cretePayment( const checkout = await StripeService.cretePayment(
price, PLAN.PRICE,
'https://dashboard.litlyx.com/payment_ok', 'https://dashboard.litlyx.com/payment_ok',
project_id, project_id,
project.customer_id project.customer_id
); );
if (!checkout) { if (!checkout) {
console.error('Cannot create payment', { plan, price }); console.error('Cannot create payment', { plan: PLAN });
return setResponseStatus(event, 400, 'Cannot create payment'); return setResponseStatus(event, 400, 'Cannot create payment');
} }

View File

@@ -1,4 +1,5 @@
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA"; import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import { Redis } from "~/server/services/CacheService";
import StripeService from '~/server/services/StripeService'; import StripeService from '~/server/services/StripeService';
@@ -21,17 +22,21 @@ export default defineEventHandler(async event => {
if (!project.customer_id) return []; 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 invoices = await StripeService.getInvoices(project.customer_id);
const result: InvoiceData = {
link: e.invoice_pdf || '', return invoices?.data.map(e => {
id: e.number || '', const result: InvoiceData = {
date: e.created * 1000, link: e.invoice_pdf || '',
status: e.status || 'NO_STATUS', id: e.number || '',
cost: e.amount_due date: e.created * 1000,
} status: e.status || 'NO_STATUS',
return result; cost: e.amount_due
}) }
return result;
});
});
}); });

View File

@@ -2,69 +2,187 @@
import StripeService from '~/server/services/StripeService'; import StripeService from '~/server/services/StripeService';
import type Event from 'stripe'; import type Event from 'stripe';
import { ProjectModel } from '@schema/ProjectSchema'; 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 { ProjectCountModel } from '@schema/ProjectsCounts';
import { ProjectLimitModel } from '@schema/ProjectsLimits';
import { UserModel } from '@schema/UserSchema';
async function onPaymentSuccess(event: Event.InvoicePaidEvent) { 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); // const pid = data.subscription_details?.metadata?.pid;
if (!project) return { error: 'Project not found' } // if (!pid) return { error: 'ProjectId not found' }
const subscriptionId = event.data.object.subscription; // const project = await ProjectModel.findById(pid);
if (!subscriptionId) return { error: 'SubscriptionId not found' } // if (!project) return { error: 'Project not found' }
const price = event.data.object.lines.data[0].plan?.id; // const price = data.lines.data[0].plan?.id;
if (!price) return { error: 'Price not found' } // if (!price) return { error: 'Price not found' }
const premiumTag = getPlanTagFromStripePrice(price); // const PLAN = getPlanFromPrice(price);
if (!premiumTag) return { error: 'Premium tag not found' } // if (!PLAN) return { error: 'Plan not found' }
const plan = getPlanFromPremiumTag(premiumTag); // await ProjectModel.updateOne({ _id: pid }, {
if (!plan) return { error: 'Plan not found' } // 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 }, { // await ProjectCountModel.create({
premium: true, // project_id: project._id,
customer_id: event.data.object.customer, // events: 0,
premium_type: plan.id, // visits: 0,
premium_expire_at: event.data.object.lines.data[0].period.end * 1000 // 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]; // return { ok: true }
// }
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 { received: true } return { received: true }
} }
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) { 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) { 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) { 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 => { export default defineEventHandler(async event => {
const body = await readRawBody(event); const body = await readRawBody(event);

View File

@@ -1,4 +1,5 @@
import { ProjectModel, TProject } from "@schema/ProjectSchema"; import { ProjectModel, TProject } from "@schema/ProjectSchema";
import StripeService from '~/server/services/StripeService';
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
@@ -15,9 +16,23 @@ export default defineEventHandler(async event => {
const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id }); const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id });
if (existingUserProjects == 3) return setResponseStatus(event, 400, 'Already have 3 projects'); if (existingUserProjects == 3) return setResponseStatus(event, 400, 'Already have 3 projects');
const newProject = new ProjectModel({ owner: userData.id, name: newProjectName }); const customer = await StripeService.createCustomer(userData.user.email);
const saved = await newProject.save(); 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;
}); });

View File

@@ -1,4 +1,7 @@
import { ProjectModel } from "@schema/ProjectSchema"; 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 => { export default defineEventHandler(async event => {
@@ -9,10 +12,29 @@ export default defineEventHandler(async event => {
const userData = getRequestUser(event); const userData = getRequestUser(event);
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged'); 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 }); const projects = await ProjectModel.countDocuments({ owner: userData.id });
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project'); if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
const deletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId }); if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
return { ok: deletation.acknowledged };
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
]
};
}); });

View File

@@ -1,8 +1,7 @@
import { ProjectModel, TProject } from "@schema/ProjectSchema"; import { ProjectModel } from "@schema/ProjectSchema";
import { ProjectCountModel } from "@schema/ProjectsCounts"; import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { UserSettingsModel } from "@schema/UserSettings"; import { UserSettingsModel } from "@schema/UserSettings";
import StripeService from '~/server/services/StripeService';
const { BROKER_UPDATE_EXPIRE_TIME_PATH } = useRuntimeConfig();
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
@@ -17,25 +16,20 @@ export default defineEventHandler(async event => {
const project = await ProjectModel.findById(project_id); const project = await ProjectModel.findById(project_id);
if (!project) return setResponseStatus(event, 400, 'Project not found'); if (!project) return setResponseStatus(event, 400, 'Project not found');
const subscription = await StripeService.getSubscription(project.subscription_id);
let projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { const projectLimits = await ProjectLimitModel.findOne({ project_id });
sort: { billing_expire_at: -1 } 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 = { const result = {
premium: project.premium, premium: project.premium,
premium_type: project.premium_type, premium_type: project.premium_type,
billing_start_at: projectCounts.billing_start_at, billing_start_at: projectLimits.billing_start_at,
billing_expire_at: projectCounts.billing_expire_at, billing_expire_at: projectLimits.billing_expire_at,
limit: projectCounts.limit, limit: projectLimits.limit,
count: projectCounts.events + projectCounts.visits, count: projectLimits.events + projectLimits.visits,
subscription_status: subscription.status
} }
return result; return result;

View File

@@ -5,7 +5,7 @@ import OpenAI from "openai";
import { AiChatModel } from '@schema/ai/AiChatSchema'; import { AiChatModel } from '@schema/ai/AiChatSchema';
import { AI_EventsFunctions, AI_EventsTools } from '../api/ai/functions/AI_Events'; import { AI_EventsFunctions, AI_EventsTools } from '../api/ai/functions/AI_Events';
import { ProjectCountModel } from '@schema/ProjectsCounts'; import { ProjectCountModel } from '@schema/ProjectsCounts';
import { getCurrentProjectCountId } from '@functions/UtilsProjectCounts'; import { ProjectLimitModel } from '@schema/ProjectsLimits';
const { AI_ORG, AI_PROJECT, AI_KEY } = useRuntimeConfig(); const { AI_ORG, AI_PROJECT, AI_KEY } = useRuntimeConfig();
@@ -135,9 +135,7 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
} }
const currentCountId = await getCurrentProjectCountId(pid); await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } })
if (!currentCountId) console.error('Project not exist');
await ProjectCountModel.updateOne({ _id: currentCountId }, { $inc: { ai_messages: 1 } })
return responseMessage.content; return responseMessage.content;
} }

View File

@@ -1,3 +1,4 @@
import { getPlanFromTag } from '@data/PREMIUM';
import Stripe from 'stripe'; import Stripe from 'stripe';
class StripeService { class StripeService {
@@ -10,11 +11,9 @@ class StripeService {
this.webhookSecret = webhookSecret; this.webhookSecret = webhookSecret;
this.stripe = new Stripe(this.privateKey); this.stripe = new Stripe(this.privateKey);
} }
parseWebhook(body: any, sig: string) { parseWebhook(body: any, sig: string) {
if (!this.stripe) { if (!this.stripe) throw Error('Stripe not initialized');
console.error('Stripe not initialized')
return;
}
if (!this.webhookSecret) { if (!this.webhookSecret) {
console.error('Stripe not initialized') console.error('Stripe not initialized')
return; return;
@@ -23,10 +22,7 @@ class StripeService {
} }
async cretePayment(price: string, success_url: string, pid: string, customer?: string) { async cretePayment(price: string, success_url: string, pid: string, customer?: string) {
if (!this.stripe) { if (!this.stripe) throw Error('Stripe not initialized');
console.error('Stripe not initialized')
return;
}
const checkout = await this.stripe.checkout.sessions.create({ const checkout = await this.stripe.checkout.sessions.create({
payment_method_types: ['card'], payment_method_types: ['card'],
@@ -44,11 +40,14 @@ class StripeService {
return checkout; return checkout;
} }
async deleteSubscription(subscriptionId: string) {
if (!this.stripe) throw Error('Stripe not initialized');
const subscription = await this.stripe.subscriptions.cancel(subscriptionId);
return subscription;
}
async getSubscription(subscriptionId: string) { async getSubscription(subscriptionId: string) {
if (!this.stripe) { if (!this.stripe) throw Error('Stripe not initialized');
console.error('Stripe not initialized')
return;
}
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId); const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
return subscription; return subscription;
} }
@@ -57,6 +56,45 @@ class StripeService {
const invoices = await this.stripe?.invoices.list({ customer: customer_id }); const invoices = await this.stripe?.invoices.list({ customer: customer_id });
return invoices; return invoices;
} }
async getCustomer(customer_id: string) {
if (!this.stripe) throw Error('Stripe not initialized');
const customer = await this.stripe.customers.retrieve(customer_id, { expand: [] })
return customer;
}
async createCustomer(email: string) {
if (!this.stripe) throw Error('Stripe not initialized');
const customer = await this.stripe.customers.create({ email });
return customer;
}
async deleteCustomer(customer_id: string) {
if (!this.stripe) throw Error('Stripe not initialized');
const { deleted } = await this.stripe.customers.del(customer_id);
return deleted;
}
async createFreeSubscription(customer_id: string) {
if (!this.stripe) throw Error('Stripe not initialized');
const FREE_PLAN = getPlanFromTag('FREE');
const subscription = await this.stripe.subscriptions.create({
customer: customer_id,
items: [
{ price: FREE_PLAN.PRICE, quantity: 1 }
]
});
return subscription;
}
} }
const instance = new StripeService(); const instance = new StripeService();

66
shared/data/PREMIUM.ts Normal file
View File

@@ -0,0 +1,66 @@
export type PREMIUM_TAG = typeof PREMIUM_TAGS[number];
export const PREMIUM_TAGS = [
'FREE', 'PLAN_1', 'PLAN_2', 'PLAN_3', 'PLAN_99'
] as const;
export type PREMIUM_DATA = {
COUNT_LIMIT: number,
AI_MESSAGE_LIMIT: number,
PRICE: string,
ID: number
}
export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
FREE: {
ID: 0,
COUNT_LIMIT: 3_000,
AI_MESSAGE_LIMIT: 10,
PRICE: 'price_1PNbHYB2lPUiVs9VZP32xglF'
},
PLAN_1: {
ID: 1,
COUNT_LIMIT: 150_000,
AI_MESSAGE_LIMIT: 100,
PRICE: 'price_1PNZjVB2lPUiVs9VrsTbJL04'
},
PLAN_2: {
ID: 2,
COUNT_LIMIT: 500_000,
AI_MESSAGE_LIMIT: 5_000,
PRICE: ''
},
PLAN_3: {
ID: 3,
COUNT_LIMIT: 2_000_000,
AI_MESSAGE_LIMIT: 10_000,
PRICE: ''
},
PLAN_99: {
ID: 99,
COUNT_LIMIT: 10_000_000,
AI_MESSAGE_LIMIT: 100_000,
PRICE: ''
}
}
export function getPlanFromTag(tag: PREMIUM_TAG) {
return PREMIUM_PLAN[tag];
}
export function getPlanFromId(id: number) {
for (const tag of PREMIUM_TAGS) {
const plan = getPlanFromTag(tag);
if (plan.ID === id) return plan;
}
}
export function getPlanFromPrice(price: string) {
for (const tag of PREMIUM_TAGS) {
const plan = getPlanFromTag(tag);
if (plan.PRICE === price) return plan;
}
}

View File

@@ -1,81 +0,0 @@
export const PREMIUM_PLANS = [
{ id: 0, tag: 'FREE', name: 'Free' },
{ id: 1, tag: 'PLAN_1', name: 'Premium 1' },
{ id: 2, tag: 'PLAN_2', name: 'Premium 2' },
{ id: 3, tag: 'PLAN_3', name: 'Premium 3' },
{ id: 99, tag: 'PLAN_99', name: 'Premium 99' },
] as const;
export function getPlanFromPremiumType(premium_type?: number) {
if (!premium_type) return PREMIUM_PLANS[0];
const plan = PREMIUM_PLANS.find(e => e.id === premium_type);
if (!plan) return PREMIUM_PLANS[0];
return plan;
}
export function getPlanFromPremiumTag(tag: PREMIUM_PLAN_TAG) {
const plan = PREMIUM_PLANS.find(e => e.tag === tag);
return plan;
}
export type PREMIUM_PLAN_TAG = typeof PREMIUM_PLANS[number]['tag'];
export type PROJECT_LIMIT = {
COUNT_LIMIT: number,
AI_MESSAGE_LIMIT: number,
}
export const PREMIUM_LIMITS: Record<PREMIUM_PLAN_TAG, PROJECT_LIMIT> = {
FREE: {
COUNT_LIMIT: 3_000,
AI_MESSAGE_LIMIT: 10
},
PLAN_1: {
COUNT_LIMIT: 150_000,
AI_MESSAGE_LIMIT: 100
},
PLAN_2: {
COUNT_LIMIT: 500_000,
AI_MESSAGE_LIMIT: 5_000
},
PLAN_3: {
COUNT_LIMIT: 2_000_000,
AI_MESSAGE_LIMIT: 10_000
},
PLAN_99: {
COUNT_LIMIT: 10_000_000,
AI_MESSAGE_LIMIT: 100_000
}
}
export type STRIPE_PLAN = {
price: string
}
export const STRIPE_PLANS: Record<PREMIUM_PLAN_TAG, STRIPE_PLAN> = {
FREE: {
price: 'price_1PNbHYB2lPUiVs9VZP32xglF'
},
PLAN_1: {
price: 'price_1PNZjVB2lPUiVs9VrsTbJL04'
},
PLAN_2: {
price: ''
},
PLAN_3: {
price: ''
},
PLAN_99: {
price: ''
}
}
export function getPlanTagFromStripePrice(price: string): PREMIUM_PLAN_TAG | undefined {
for (const plan of PREMIUM_PLANS.map(e => e.tag)) {
const stripePrice = STRIPE_PLANS[plan].price;
if (stripePrice === price) return plan;
}
}

View File

@@ -1,70 +0,0 @@
import { ProjectCountModel } from '../schema/ProjectsCounts';
import { ProjectModel } from '../schema/ProjectSchema';
import { LimitNotifyModel } from '../schema/broker/LimitNotifySchema';
import { PREMIUM_LIMITS, getPlanFromPremiumType } from '../data/PREMIUM_LIMITS';
import { MONTH } from '../utilts/TIME';
export async function getCurrentProjectCountId(project_id: string) {
const projectCount = await ProjectCountModel.findOne({ project_id }, { _id: 1 }, { sort: { billing_expire_at: -1 } });
return projectCount?._id.toString();
}
export async function getAllLimitsFromProjectId(project_id: string) {
const targetProject = await ProjectModel.findById(project_id, {
premium: 1, premium_type: 1, premium_expire_at: 1
});
if (!targetProject) return PREMIUM_LIMITS.FREE;
if (!targetProject.premium) return PREMIUM_LIMITS.FREE;
const plan = getPlanFromPremiumType(targetProject.premium_type);
return PREMIUM_LIMITS[plan.tag];
}
export async function checkProjectCount(project_id: string) {
const targetProject = await ProjectModel.findById(project_id, {
premium: 1, premium_type: 1, premium_expire_at: 1
});
if (!targetProject) return;
if (new Date(targetProject.premium_expire_at).getTime() < Date.now()) {
await ProjectModel.updateOne({ _id: project_id }, {
premium: false,
$unset: {
premium_type: 1,
premium_expire_at: 1
},
});
}
const limits = await getAllLimitsFromProjectId(project_id);
const projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { sort: { billing_expire_at: -1 } });
const billingExpireAt = projectCounts ? new Date(projectCounts.billing_expire_at).getTime() : -1;
if (projectCounts && Date.now() < billingExpireAt) {
if (projectCounts.ai_limit) return projectCounts.toJSON();
projectCounts.ai_limit = limits.AI_MESSAGE_LIMIT;
const saved = await projectCounts.save();
return saved.toJSON();
}
const newProjectCounts = await ProjectCountModel.create({
project_id,
events: 0,
visits: 0,
limit: limits.COUNT_LIMIT,
ai_messages: 0,
ai_limit: limits.AI_MESSAGE_LIMIT,
billing_start_at: projectCounts ? billingExpireAt : Date.now(),
billing_expire_at: (projectCounts ? billingExpireAt : Date.now()) + MONTH
});
await LimitNotifyModel.updateOne({ project_id }, { limit1: false, limit2: false, limit3: false });
return newProjectCounts.toJSON();
}

View File

@@ -5,8 +5,9 @@ export type TProject = {
owner: Schema.Types.ObjectId, owner: Schema.Types.ObjectId,
name: string, name: string,
premium: boolean, premium: boolean,
premium_type?: number, premium_type: number,
customer_id?: string, customer_id: string,
subscription_id: string,
premium_expire_at: Date, premium_expire_at: Date,
created_at: Date created_at: Date
} }
@@ -15,9 +16,10 @@ const ProjectSchema = new Schema<TProject>({
owner: { type: Types.ObjectId, index: 1 }, owner: { type: Types.ObjectId, index: 1 },
name: { type: String, required: true }, name: { type: String, required: true },
premium: { type: Boolean, default: false }, premium: { type: Boolean, default: false },
premium_type: { type: Number }, premium_type: { type: Number, default: 0 },
customer_id: { type: String }, customer_id: { type: String, required: true },
premium_expire_at: { type: Date }, subscription_id: { type: String, required: true },
premium_expire_at: { type: Date, required: true },
created_at: { type: Date, default: () => Date.now() }, created_at: { type: Date, default: () => Date.now() },
}) })

View File

@@ -5,22 +5,12 @@ export type TProjectCount = {
project_id: Schema.Types.ObjectId, project_id: Schema.Types.ObjectId,
events: number, events: number,
visits: number, visits: number,
ai_messages: number,
limit: number,
ai_limit: number,
billing_expire_at: Date,
billing_start_at: Date,
} }
const ProjectCountSchema = new Schema<TProjectCount>({ const ProjectCountSchema = new Schema<TProjectCount>({
project_id: { type: Types.ObjectId, index: 1 }, project_id: { type: Types.ObjectId, index: 1 },
events: { type: Number, required: true, default: 0 }, events: { type: Number, required: true, default: 0 },
visits: { type: Number, required: true, default: 0 }, visits: { type: Number, required: true, default: 0 },
ai_messages: { type: Number, required: true, default: 0 },
limit: { type: Number, required: true },
ai_limit: { type: Number, required: true },
billing_start_at: { type: Date, required: true },
billing_expire_at: { type: Date, required: true },
}); });
export const ProjectCountModel = model<TProjectCount>('project_counts', ProjectCountSchema); export const ProjectCountModel = model<TProjectCount>('project_counts', ProjectCountSchema);

View File

@@ -0,0 +1,26 @@
import { model, Schema, Types } from 'mongoose';
export type TProjectLimit = {
_id: Schema.Types.ObjectId,
project_id: Schema.Types.ObjectId,
events: number,
visits: number,
ai_messages: number,
limit: number,
ai_limit: number,
billing_expire_at: Date,
billing_start_at: Date,
}
const ProjectLimitSchema = new Schema<TProjectLimit>({
project_id: { type: Types.ObjectId, index: true, unique: true },
events: { type: Number, required: true, default: 0 },
visits: { type: Number, required: true, default: 0 },
ai_messages: { type: Number, required: true, default: 0 },
limit: { type: Number, required: true },
ai_limit: { type: Number, required: true },
billing_start_at: { type: Date, required: true },
billing_expire_at: { type: Date, required: true },
});
export const ProjectLimitModel = model<TProjectLimit>('project_limits', ProjectLimitSchema);