mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
fix payments
This commit is contained in:
@@ -41,6 +41,8 @@ export default defineNuxtConfig({
|
|||||||
GOOGLE_AUTH_CLIENT_SECRET: process.env.GOOGLE_AUTH_CLIENT_SECRET,
|
GOOGLE_AUTH_CLIENT_SECRET: process.env.GOOGLE_AUTH_CLIENT_SECRET,
|
||||||
STRIPE_SECRET: process.env.STRIPE_SECRET,
|
STRIPE_SECRET: process.env.STRIPE_SECRET,
|
||||||
STRIPE_WH_SECRET: process.env.STRIPE_WH_SECRET,
|
STRIPE_WH_SECRET: process.env.STRIPE_WH_SECRET,
|
||||||
|
STRIPE_SECRET_TEST: process.env.STRIPE_SECRET_TEST,
|
||||||
|
STRIPE_WH_SECRET_TEST: process.env.STRIPE_WH_SECRET_TEST,
|
||||||
public: {
|
public: {
|
||||||
PAYPAL_CLIENT_ID: ''
|
PAYPAL_CLIENT_ID: ''
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export default defineEventHandler(async event => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const checkout = await StripeService.cretePayment(
|
const checkout = await StripeService.cretePayment(
|
||||||
PLAN.PRICE,
|
StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||||
'https://dashboard.litlyx.com/payment_ok',
|
'https://dashboard.litlyx.com/payment_ok',
|
||||||
project_id,
|
project_id,
|
||||||
project.customer_id
|
project.customer_id
|
||||||
|
|||||||
@@ -2,150 +2,129 @@
|
|||||||
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_PLAN, getPlanFromPrice } from '@data/PREMIUM';
|
import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromPrice } from '@data/PREMIUM';
|
||||||
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||||
import { ProjectLimitModel } from '@schema/ProjectsLimits';
|
import { ProjectLimitModel } from '@schema/ProjectsLimits';
|
||||||
import { UserModel } from '@schema/UserSchema';
|
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 data = event.data.object;
|
const customer_id = event.data.object.customer as string;
|
||||||
|
const subscription_id = event.data.object.subscription as string;
|
||||||
|
|
||||||
// const pid = data.subscription_details?.metadata?.pid;
|
const project = await ProjectModel.findOne({ customer_id });
|
||||||
// if (!pid) return { error: 'ProjectId not found' }
|
if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
// const project = await ProjectModel.findById(pid);
|
const subscriptionInfo = await StripeService.getSubscription(subscription_id);
|
||||||
// if (!project) return { error: 'Project not found' }
|
|
||||||
|
|
||||||
// const price = data.lines.data[0].plan?.id;
|
if (subscriptionInfo.status === 'active') {
|
||||||
// if (!price) return { error: 'Price not found' }
|
|
||||||
|
|
||||||
// const PLAN = getPlanFromPrice(price);
|
const price = subscriptionInfo.items.data[0].price.id;
|
||||||
// if (!PLAN) return { error: 'Plan not found' }
|
if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
// await ProjectModel.updateOne({ _id: pid }, {
|
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||||
// premium: true,
|
if (!PLAN) return { error: 'Plan not found' }
|
||||||
// customer_id: data.customer,
|
|
||||||
// premium_type: PLAN.ID,
|
|
||||||
// premium_expire_at: data.lines.data[0].period.end * 1000
|
|
||||||
// });
|
|
||||||
|
|
||||||
// await ProjectCountModel.create({
|
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, subscriptionInfo.current_period_start, subscriptionInfo.current_period_end)
|
||||||
// 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,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// return { ok: true }
|
return { ok: true };
|
||||||
// }
|
} else {
|
||||||
|
return { received: true, warn: 'subscription status not active' }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return { received: true, warn: 'payment status not paid' }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
||||||
|
|
||||||
|
await ProjectModel.updateOne({ _id: project_id }, {
|
||||||
|
premium: plan.ID != 0,
|
||||||
|
premium_type: plan.ID,
|
||||||
|
subscription_id,
|
||||||
|
premium_expire_at: current_period_end
|
||||||
|
});
|
||||||
|
|
||||||
|
await ProjectLimitModel.updateOne({ project_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 })
|
||||||
|
|
||||||
return { received: true }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
||||||
if (!project) return { error: 'Project not found' }
|
if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
const price = event.data.object.items.data[0].price.id;
|
const price = event.data.object.items.data[0].price.id;
|
||||||
if (!price) return { error: 'Price not found' }
|
if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
const PLAN = getPlanFromPrice(price);
|
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||||
if (!PLAN) return { error: 'Plan not found' }
|
if (!PLAN) return { error: 'Plan not found' }
|
||||||
|
|
||||||
|
|
||||||
if (project.subscription_id != event.data.object.id) {
|
if (project.subscription_id != event.data.object.id) {
|
||||||
await StripeService.deleteSubscription(project.subscription_id);
|
try {
|
||||||
|
await StripeService.deleteSubscription(project.subscription_id);
|
||||||
|
} catch (ex) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
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([
|
if (event.data.object.status === 'active') {
|
||||||
|
await addSubscriptionToProject(
|
||||||
|
project._id.toString(),
|
||||||
|
PLAN,
|
||||||
|
event.data.object.id,
|
||||||
|
event.data.object.current_period_start,
|
||||||
|
event.data.object.current_period_end
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
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 }
|
return { ok: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({
|
// const project = await ProjectModel.findOne({
|
||||||
customer_id: event.data.object.customer,
|
// customer_id: event.data.object.customer,
|
||||||
subscription_id: event.data.object.id
|
// subscription_id: event.data.object.id
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (!project) return { error: 'Project not found' }
|
// if (!project) return { error: 'PROJECT WITH SUBSCRIPTION NOT FOUND' }
|
||||||
|
|
||||||
const PLAN = PREMIUM_PLAN['FREE'];
|
// const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
||||||
|
|
||||||
const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
// let customer: Event.Customer;
|
||||||
|
|
||||||
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;
|
||||||
|
// }
|
||||||
|
|
||||||
if (!targetCustomer.deleted) {
|
// await StripeService.createFreeSubscription(customer.id);
|
||||||
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);
|
return { received: true }
|
||||||
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({
|
const project = await ProjectModel.findOne({
|
||||||
customer_id: event.data.object.customer,
|
customer_id: event.data.object.customer,
|
||||||
subscription_id: event.data.object.id
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!project) return { error: 'Project not found' }
|
if (!project) return { error: 'Project not found' }
|
||||||
@@ -153,29 +132,18 @@ async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEve
|
|||||||
const price = event.data.object.items.data[0].price.id;
|
const price = event.data.object.items.data[0].price.id;
|
||||||
if (!price) return { error: 'Price not found' }
|
if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
const PLAN = getPlanFromPrice(price);
|
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||||
if (!PLAN) return { error: 'Plan not found' }
|
if (!PLAN) return { error: 'Plan not found' }
|
||||||
|
|
||||||
project.premium = PLAN.ID != 0;
|
if (event.data.object.status === 'active') {
|
||||||
project.premium_type = PLAN.ID;
|
await addSubscriptionToProject(
|
||||||
project.subscription_id = event.data.object.id;
|
project._id.toString(),
|
||||||
project.premium_expire_at = new Date(event.data.object.current_period_end * 1000);
|
PLAN,
|
||||||
|
event.data.object.id,
|
||||||
await Promise.all([
|
event.data.object.current_period_start,
|
||||||
|
event.data.object.current_period_end
|
||||||
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 }
|
return { ok: true }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ export default defineEventHandler(async event => {
|
|||||||
|
|
||||||
if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
|
if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
|
||||||
|
|
||||||
await StripeService.deleteCustomer(project.customer_id);
|
if (project.customer_id) {
|
||||||
|
await StripeService.deleteCustomer(project.customer_id);
|
||||||
|
}
|
||||||
|
|
||||||
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
||||||
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ export default defineEventHandler(async event => {
|
|||||||
await StripeService.deleteCustomer(project.customer_id);
|
await StripeService.deleteCustomer(project.customer_id);
|
||||||
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
||||||
const userSettingsDeletation = await UserSettingsModel.deleteOne({ project_id });
|
const userSettingsDeletation = await UserSettingsModel.deleteOne({ project_id });
|
||||||
|
|
||||||
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
||||||
const limitdeletation = await ProjectLimitModel.deleteMany({ project_id });
|
const limitdeletation = await ProjectLimitModel.deleteMany({ project_id });
|
||||||
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
|
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ export default async () => {
|
|||||||
config.EMAIL_PASS,
|
config.EMAIL_PASS,
|
||||||
);
|
);
|
||||||
|
|
||||||
StripeService.init(config.STRIPE_SECRET, config.STRIPE_WH_SECRET);
|
StripeService.init(config.STRIPE_SECRET, config.STRIPE_WH_SECRET, false);
|
||||||
|
|
||||||
|
|
||||||
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
|
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
|
||||||
console.log('[DATABASE] Connecting');
|
console.log('[DATABASE] Connecting');
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ class StripeService {
|
|||||||
private stripe?: Stripe;
|
private stripe?: Stripe;
|
||||||
private privateKey?: string;
|
private privateKey?: string;
|
||||||
private webhookSecret?: string;
|
private webhookSecret?: string;
|
||||||
|
public testMode?: boolean;
|
||||||
|
|
||||||
init(privateKey: string, webhookSecret: string) {
|
init(privateKey: string, webhookSecret: string, testMode: boolean = false) {
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey;
|
||||||
this.webhookSecret = webhookSecret;
|
this.webhookSecret = webhookSecret;
|
||||||
this.stripe = new Stripe(this.privateKey);
|
this.stripe = new Stripe(this.privateKey);
|
||||||
|
this.testMode = testMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
parseWebhook(body: any, sig: string) {
|
parseWebhook(body: any, sig: string) {
|
||||||
@@ -87,7 +89,7 @@ class StripeService {
|
|||||||
const subscription = await this.stripe.subscriptions.create({
|
const subscription = await this.stripe.subscriptions.create({
|
||||||
customer: customer_id,
|
customer: customer_id,
|
||||||
items: [
|
items: [
|
||||||
{ price: FREE_PLAN.PRICE, quantity: 1 }
|
{ price: this.testMode ? FREE_PLAN.PRICE_TEST : FREE_PLAN.PRICE, quantity: 1 }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,14 @@ export function getPlanFromId(id: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPlanFromPrice(price: string) {
|
export function getPlanFromPrice(price: string, testMode: boolean) {
|
||||||
for (const tag of PREMIUM_TAGS) {
|
for (const tag of PREMIUM_TAGS) {
|
||||||
const plan = getPlanFromTag(tag);
|
const plan = getPlanFromTag(tag);
|
||||||
if (plan.PRICE === price) return plan;
|
if (testMode) {
|
||||||
|
if (plan.PRICE_TEST === price) return plan;
|
||||||
|
} else {
|
||||||
|
if (plan.PRICE === price) return plan;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user