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

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

View File

@@ -22,7 +22,8 @@ const starterTierCardData = ref<PricingCardProp>({
can experience some data loss.To have a
dedicated server we suggest to upgrade the
plan to an higher one!`,
active: activeProject.value?.premium === false
active: activeProject.value?.premium === false,
planId: 0
});
const accelerationTierCardData = ref<PricingCardProp>({
@@ -39,7 +40,8 @@ const accelerationTierCardData = ref<PricingCardProp>({
"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!`,
active: activeProject.value?.premium_type === 1
active: activeProject.value?.premium_type === 1,
planId: 1
});
const expansionTierCardData = ref<PricingCardProp>({
@@ -56,7 +58,8 @@ const expansionTierCardData = ref<PricingCardProp>({
"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!`,
active: activeProject.value?.premium_type === 2
active: activeProject.value?.premium_type === 2,
planId: 2
});
</script>

View File

@@ -75,17 +75,6 @@ function onHideClicked() {
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>
@@ -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 @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">
<div class="text-text-sub/90"> <i class="far fa-eye"></i> </div>
<div> Nascondi dalla barra </div>

View File

@@ -107,6 +107,9 @@ function openInvoice(link: string) {
</div>
<div class="poppins"> {{ daysLeft }} days left </div>
</div>
<div class="flex justify-center">
Subscription: {{ planData.subscription_status }}
</div>
</div>
</div>
<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 { 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;
}

View File

@@ -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,

View File

@@ -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');
}

View File

@@ -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;
});
});
});

View File

@@ -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);

View File

@@ -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;
});

View File

@@ -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
]
};
});

View File

@@ -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;

View File

@@ -5,7 +5,7 @@ import OpenAI from "openai";
import { AiChatModel } from '@schema/ai/AiChatSchema';
import { AI_EventsFunctions, AI_EventsTools } from '../api/ai/functions/AI_Events';
import { ProjectCountModel } from '@schema/ProjectsCounts';
import { getCurrentProjectCountId } from '@functions/UtilsProjectCounts';
import { ProjectLimitModel } from '@schema/ProjectsLimits';
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);
if (!currentCountId) console.error('Project not exist');
await ProjectCountModel.updateOne({ _id: currentCountId }, { $inc: { ai_messages: 1 } })
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } })
return responseMessage.content;
}

View File

@@ -1,3 +1,4 @@
import { getPlanFromTag } from '@data/PREMIUM';
import Stripe from 'stripe';
class StripeService {
@@ -10,11 +11,9 @@ class StripeService {
this.webhookSecret = webhookSecret;
this.stripe = new Stripe(this.privateKey);
}
parseWebhook(body: any, sig: string) {
if (!this.stripe) {
console.error('Stripe not initialized')
return;
}
if (!this.stripe) throw Error('Stripe not initialized');
if (!this.webhookSecret) {
console.error('Stripe not initialized')
return;
@@ -23,10 +22,7 @@ class StripeService {
}
async cretePayment(price: string, success_url: string, pid: string, customer?: string) {
if (!this.stripe) {
console.error('Stripe not initialized')
return;
}
if (!this.stripe) throw Error('Stripe not initialized');
const checkout = await this.stripe.checkout.sessions.create({
payment_method_types: ['card'],
@@ -44,11 +40,14 @@ class StripeService {
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) {
if (!this.stripe) {
console.error('Stripe not initialized')
return;
}
if (!this.stripe) throw Error('Stripe not initialized');
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
return subscription;
}
@@ -57,6 +56,45 @@ class StripeService {
const invoices = await this.stripe?.invoices.list({ customer: customer_id });
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();