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,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();