mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
stripe
This commit is contained in:
@@ -76,11 +76,13 @@ function onHideClicked() {
|
|||||||
const activeProject = useActiveProject();
|
const activeProject = useActiveProject();
|
||||||
|
|
||||||
async function payment() {
|
async function payment() {
|
||||||
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
// const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
||||||
...signHeaders({ 'content-type': 'application/json' }),
|
// ...signHeaders({ 'content-type': 'application/json' }),
|
||||||
method: 'POST',
|
// method: 'POST',
|
||||||
body: JSON.stringify({ planId: 1 })
|
// body: JSON.stringify({ planId: 1 })
|
||||||
})
|
// })
|
||||||
|
// console.log(res);
|
||||||
|
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, signHeaders())
|
||||||
console.log(res);
|
console.log(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,12 +37,17 @@ const prettyExpireDate = computed(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const router = useRouter();
|
const { data: invoices } = await useFetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, signHeaders())
|
||||||
|
|
||||||
const showPricingDrawer = ref<boolean>(false);
|
const showPricingDrawer = ref<boolean>(false);
|
||||||
function onPlanUpgradeClick() {
|
function onPlanUpgradeClick() {
|
||||||
showPricingDrawer.value = true;
|
showPricingDrawer.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function openInvoice(link: string) {
|
||||||
|
window.open(link, '_blank');
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -158,7 +163,30 @@ function onPlanUpgradeClick() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<CardTitled title="Invoices" sub="No invoices yet" class="p-4 mt-8 max-w-[72rem]">
|
<CardTitled title="Invoices" :sub="(invoices && invoices.length == 0) ? 'No invoices yet' : ''"
|
||||||
|
class="p-4 mt-8 max-w-[72rem]">
|
||||||
|
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
|
||||||
|
<div class="flex gap-10 bg-black p-4 rounded-lg" v-for="invoice of invoices">
|
||||||
|
|
||||||
|
<div> <i class="far fa-file-invoice"></i> </div>
|
||||||
|
|
||||||
|
<div> {{ new Date(invoice.date).toLocaleString() }} </div>
|
||||||
|
<div> € {{ invoice.cost / 100 }} </div>
|
||||||
|
<div> {{ invoice.id }} </div>
|
||||||
|
<div
|
||||||
|
class="flex items-center lato text-[.8rem] bg-accent/25 border-accent/40 border-[1px] px-[.6rem] rounded-lg">
|
||||||
|
{{ invoice.status }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<i @click="openInvoice(invoice.link)"
|
||||||
|
class="far fa-download cursor-pointer hover:text-white/80"></i>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
</CardTitled>
|
</CardTitled>
|
||||||
|
|
||||||
|
|
||||||
@@ -181,6 +209,4 @@ function onPlanUpgradeClick() {
|
|||||||
.pdrawer-leave-from {
|
.pdrawer-leave-from {
|
||||||
transform: translateX(0)
|
transform: translateX(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export default defineEventHandler(async event => {
|
|||||||
const checkout = await StripeService.cretePayment(
|
const checkout = await StripeService.cretePayment(
|
||||||
price,
|
price,
|
||||||
'https://dashboard.litlyx.com/payment_ok',
|
'https://dashboard.litlyx.com/payment_ok',
|
||||||
|
project_id,
|
||||||
project.customer_id
|
project.customer_id
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -37,9 +38,6 @@ export default defineEventHandler(async event => {
|
|||||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||||
}
|
}
|
||||||
|
|
||||||
const customer = checkout.customer;
|
|
||||||
await ProjectModel.updateOne({ _id: project_id }, { customer_id: customer });
|
|
||||||
|
|
||||||
return checkout.url;
|
return checkout.url;
|
||||||
|
|
||||||
});
|
});
|
||||||
37
dashboard/server/api/pay/[project_id]/invoices.ts
Normal file
37
dashboard/server/api/pay/[project_id]/invoices.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||||
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
|
||||||
|
|
||||||
|
export type InvoiceData = {
|
||||||
|
date: number,
|
||||||
|
cost: number,
|
||||||
|
id: string,
|
||||||
|
status: string,
|
||||||
|
link: string
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (!project.customer_id) return [];
|
||||||
|
|
||||||
|
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;
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
||||||
@@ -8,19 +8,17 @@ import { ProjectCountModel } from '@schema/ProjectsCounts';
|
|||||||
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 customer = event.data.object.customer;
|
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({ customer_id: customer });
|
const pid = event.data.object.subscription_details?.metadata?.pid;
|
||||||
|
|
||||||
|
const project = await ProjectModel.findById(pid);
|
||||||
if (!project) return { error: 'Project not found' }
|
if (!project) return { error: 'Project not found' }
|
||||||
|
|
||||||
const subscriptionId = event.data.object.subscription;
|
const subscriptionId = event.data.object.subscription;
|
||||||
if (!subscriptionId) return { error: 'SubscriptionId not found' }
|
if (!subscriptionId) return { error: 'SubscriptionId not found' }
|
||||||
|
|
||||||
const subscription = await StripeService.getSubscription(subscriptionId as string);
|
const price = event.data.object.lines.data[0].plan?.id;
|
||||||
if (!subscription) return { error: 'Subscription not found' }
|
if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
const price = subscription.items.data[0].plan.id;
|
|
||||||
|
|
||||||
|
|
||||||
const premiumTag = getPlanTagFromStripePrice(price);
|
const premiumTag = getPlanTagFromStripePrice(price);
|
||||||
if (!premiumTag) return { error: 'Premium tag not found' }
|
if (!premiumTag) return { error: 'Premium tag not found' }
|
||||||
@@ -28,10 +26,11 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
|||||||
const plan = getPlanFromPremiumTag(premiumTag);
|
const plan = getPlanFromPremiumTag(premiumTag);
|
||||||
if (!plan) return { error: 'Plan not found' }
|
if (!plan) return { error: 'Plan not found' }
|
||||||
|
|
||||||
await ProjectModel.updateOne({ customer_id: customer }, {
|
await ProjectModel.updateOne({ _id: pid }, {
|
||||||
premium: true,
|
premium: true,
|
||||||
|
customer_id: event.data.object.customer,
|
||||||
premium_type: plan.id,
|
premium_type: plan.id,
|
||||||
premium_expire_at: subscription.current_period_end
|
premium_expire_at: event.data.object.lines.data[0].period.end * 1000
|
||||||
});
|
});
|
||||||
|
|
||||||
const limits = PREMIUM_LIMITS[premiumTag];
|
const limits = PREMIUM_LIMITS[premiumTag];
|
||||||
@@ -43,8 +42,8 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
|||||||
ai_messages: 0,
|
ai_messages: 0,
|
||||||
limit: limits.COUNT_LIMIT,
|
limit: limits.COUNT_LIMIT,
|
||||||
ai_limit: limits.AI_MESSAGE_LIMIT,
|
ai_limit: limits.AI_MESSAGE_LIMIT,
|
||||||
billing_start_at: subscription.current_period_start,
|
billing_start_at: event.data.object.lines.data[0].period.start * 1000,
|
||||||
billing_expire_at: subscription.current_period_end,
|
billing_expire_at: event.data.object.lines.data[0].period.end * 1000,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { ok: true }
|
return { ok: true }
|
||||||
@@ -61,6 +60,10 @@ async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEve
|
|||||||
return { received: true }
|
return { received: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
||||||
|
return { received: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
@@ -72,6 +75,7 @@ export default defineEventHandler(async event => {
|
|||||||
if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
|
if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
|
||||||
if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(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.created') return await onSubscriptionCreated(eventData);
|
||||||
|
if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
|
||||||
|
|
||||||
return { received: true }
|
return { received: true }
|
||||||
});
|
});
|
||||||
@@ -22,16 +22,20 @@ class StripeService {
|
|||||||
return this.stripe.webhooks.constructEvent(body, sig, this.webhookSecret);
|
return this.stripe.webhooks.constructEvent(body, sig, this.webhookSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
async cretePayment(price: string, success_url: string, customer?: string) {
|
async cretePayment(price: string, success_url: string, pid: string, customer?: string) {
|
||||||
if (!this.stripe) {
|
if (!this.stripe) {
|
||||||
console.error('Stripe not initialized')
|
console.error('Stripe not initialized')
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkout = await this.stripe.checkout.sessions.create({
|
const checkout = await this.stripe.checkout.sessions.create({
|
||||||
payment_method_types: ['card'],
|
payment_method_types: ['card'],
|
||||||
line_items: [
|
line_items: [
|
||||||
{ price, quantity: 1 }
|
{ price, quantity: 1 }
|
||||||
],
|
],
|
||||||
|
subscription_data: {
|
||||||
|
metadata: { pid },
|
||||||
|
},
|
||||||
customer,
|
customer,
|
||||||
success_url,
|
success_url,
|
||||||
mode: 'subscription'
|
mode: 'subscription'
|
||||||
@@ -48,6 +52,11 @@ class StripeService {
|
|||||||
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
|
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
|
||||||
return subscription;
|
return subscription;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getInvoices(customer_id: string) {
|
||||||
|
const invoices = await this.stripe?.invoices.list({ customer: customer_id });
|
||||||
|
return invoices;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = new StripeService();
|
const instance = new StripeService();
|
||||||
|
|||||||
Reference in New Issue
Block a user