fix payments

This commit is contained in:
Emily
2024-06-12 15:43:23 +02:00
parent b7c3ef19ba
commit 6a7143c8d4
8 changed files with 173 additions and 89 deletions

View File

@@ -2,42 +2,11 @@
import StripeService from '~/server/services/StripeService';
import type Event from 'stripe';
import { ProjectModel } from '@schema/ProjectSchema';
import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromPrice } from '@data/PREMIUM';
import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromId, getPlanFromPrice, getPlanFromTag } 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') {
const customer_id = event.data.object.customer as string;
const subscription_id = event.data.object.subscription as string;
const project = await ProjectModel.findOne({ customer_id });
if (!project) return { error: 'CUSTOMER NOT EXIST' }
const subscriptionInfo = await StripeService.getSubscription(subscription_id);
if (subscriptionInfo.status === 'active') {
const price = subscriptionInfo.items.data[0].price.id;
if (!price) return { error: 'Price not found' }
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
if (!PLAN) return { error: 'Plan not found' }
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, subscriptionInfo.current_period_start, subscriptionInfo.current_period_end)
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) {
@@ -46,7 +15,7 @@ async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA,
premium: plan.ID != 0,
premium_type: plan.ID,
subscription_id,
premium_expire_at: current_period_end
premium_expire_at: current_period_end * 1000
});
await ProjectLimitModel.updateOne({ project_id }, {
@@ -61,35 +30,108 @@ async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA,
}
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
if (!project) return { error: 'CUSTOMER NOT EXIST' }
const price = event.data.object.items.data[0].price.id;
if (!price) return { error: 'Price not found' }
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
if (!PLAN) return { error: 'Plan not found' }
if (project.subscription_id != event.data.object.id) {
try {
await StripeService.deleteSubscription(project.subscription_id);
} catch (ex) { }
}
if (event.data.object.status === 'active') {
async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) {
//TODO: Send emails
if (event.data.object.attempt_count > 1) {
const customer_id = event.data.object.customer as string;
const project = await ProjectModel.findOne({ customer_id });
if (!project) return { error: 'CUSTOMER NOT EXIST' }
const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
for (const subscription of allSubscriptions.data) {
await StripeService.deleteSubscription(subscription.id);
}
const freeSub = await StripeService.createFreeSubscription(customer_id);
await addSubscriptionToProject(
project._id.toString(),
PLAN,
event.data.object.id,
event.data.object.current_period_start,
event.data.object.current_period_end
);
getPlanFromTag('FREE'),
freeSub.id,
freeSub.current_period_start,
freeSub.current_period_end
)
return { ok: true };
}
}
async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
const customer_id = event.data.object.customer as string;
const project = await ProjectModel.findOne({ customer_id });
if (!project) return { error: 'CUSTOMER NOT EXIST' }
if (event.data.object.status === 'paid') {
const subscription_id = event.data.object.subscription as string;
const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
const currentSubscription = allSubscriptions.data.find(e => e.id === subscription_id);
if (!currentSubscription) return { error: 'SUBSCRIPTION NOT EXIST' }
if (currentSubscription.status !== 'active') return { error: 'SUBSCRIPTION NOT ACTIVE' }
for (const subscription of allSubscriptions.data) {
if (subscription.id === subscription_id) continue;
await StripeService.deleteSubscription(subscription.id);
}
const price = currentSubscription.items.data[0].price.id;
if (!price) return { error: 'Price not found' }
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
if (!PLAN) return { error: 'Plan not found' }
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)
return { ok: true };
}
return { received: true, warn: 'payment status not paid' }
}
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
// const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
// if (!project) return { error: 'CUSTOMER NOT EXIST' }
// const price = event.data.object.items.data[0].price.id;
// if (!price) return { error: 'Price not found' }
// const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
// if (!PLAN) return { error: 'Plan not found' }
// if (project.subscription_id != event.data.object.id) {
// try {
// await StripeService.deleteSubscription(project.subscription_id);
// } catch (ex) { }
// }
// 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
// );
// }
return { ok: true }
}
@@ -123,27 +165,27 @@ async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEve
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
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: 'Project not found' }
const price = event.data.object.items.data[0].price.id;
if (!price) return { error: 'Price not found' }
// const price = event.data.object.items.data[0].price.id;
// if (!price) return { error: 'Price not found' }
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
if (!PLAN) return { error: 'Plan not found' }
// const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
// if (!PLAN) return { error: 'Plan not found' }
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
);
}
// 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
// );
// }
return { ok: true }
}
@@ -159,6 +201,7 @@ export default defineEventHandler(async event => {
const eventData = StripeService.parseWebhook(body, signature);
if (!eventData) return;
if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
if (eventData.type === 'invoice.payment_failed') return await onPaymentFailed(eventData);
if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);