mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
add stripe
This commit is contained in:
45
dashboard/server/api/pay/[project_id]/create.post.ts
Normal file
45
dashboard/server/api/pay/[project_id]/create.post.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { PREMIUM_PLANS, STRIPE_PLANS } from "@data/PREMIUM_LIMITS";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const project_id = getRequestProjectId(event);
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const body = await readBody(event);
|
||||
|
||||
const { planId } = body;
|
||||
|
||||
const plan = PREMIUM_PLANS.find(e => e.id == planId);
|
||||
|
||||
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,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
project.customer_id
|
||||
);
|
||||
|
||||
if (!checkout) {
|
||||
console.error('Cannot create payment', { plan, price });
|
||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
}
|
||||
|
||||
const customer = checkout.customer;
|
||||
await ProjectModel.updateOne({ _id: project_id }, { customer_id: customer });
|
||||
|
||||
return checkout.url;
|
||||
|
||||
});
|
||||
77
dashboard/server/api/pay/webhook.post.ts
Normal file
77
dashboard/server/api/pay/webhook.post.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
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 { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||
|
||||
async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
|
||||
if (event.data.object.status === 'paid') {
|
||||
const customer = event.data.object.customer;
|
||||
|
||||
const project = await ProjectModel.findOne({ customer_id: customer });
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const subscriptionId = event.data.object.subscription;
|
||||
if (!subscriptionId) return { error: 'SubscriptionId not found' }
|
||||
|
||||
const subscription = await StripeService.getSubscription(subscriptionId as string);
|
||||
if (!subscription) return { error: 'Subscription not found' }
|
||||
|
||||
const price = subscription.items.data[0].plan.id;
|
||||
|
||||
|
||||
const premiumTag = getPlanTagFromStripePrice(price);
|
||||
if (!premiumTag) return { error: 'Premium tag not found' }
|
||||
|
||||
const plan = getPlanFromPremiumTag(premiumTag);
|
||||
if (!plan) return { error: 'Plan not found' }
|
||||
|
||||
await ProjectModel.updateOne({ customer_id: customer }, {
|
||||
premium: true,
|
||||
premium_type: plan.id,
|
||||
premium_expire_at: subscription.current_period_end
|
||||
});
|
||||
|
||||
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: subscription.current_period_start,
|
||||
billing_expire_at: subscription.current_period_end,
|
||||
});
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const body = await readRawBody(event);
|
||||
const signature = getHeader(event, 'stripe-signature') || '';
|
||||
|
||||
const eventData = StripeService.parseWebhook(body, signature);
|
||||
if (!eventData) return;
|
||||
if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
|
||||
if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
|
||||
if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
|
||||
|
||||
return { received: true }
|
||||
});
|
||||
Reference in New Issue
Block a user