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();
|
||||
|
||||
async function payment() {
|
||||
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
||||
...signHeaders({ 'content-type': 'application/json' }),
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ planId: 1 })
|
||||
})
|
||||
// const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
||||
// ...signHeaders({ 'content-type': 'application/json' }),
|
||||
// method: 'POST',
|
||||
// body: JSON.stringify({ planId: 1 })
|
||||
// })
|
||||
// console.log(res);
|
||||
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, signHeaders())
|
||||
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);
|
||||
function onPlanUpgradeClick() {
|
||||
showPricingDrawer.value = true;
|
||||
}
|
||||
|
||||
function openInvoice(link: string) {
|
||||
window.open(link, '_blank');
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -158,7 +163,30 @@ function onPlanUpgradeClick() {
|
||||
</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>
|
||||
|
||||
|
||||
@@ -181,6 +209,4 @@ function onPlanUpgradeClick() {
|
||||
.pdrawer-leave-from {
|
||||
transform: translateX(0)
|
||||
}
|
||||
|
||||
|
||||
</style>
|
||||
|
||||
@@ -29,6 +29,7 @@ export default defineEventHandler(async event => {
|
||||
const checkout = await StripeService.cretePayment(
|
||||
price,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
project_id,
|
||||
project.customer_id
|
||||
);
|
||||
|
||||
@@ -37,9 +38,6 @@ export default defineEventHandler(async event => {
|
||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
}
|
||||
|
||||
const customer = checkout.customer;
|
||||
await ProjectModel.updateOne({ _id: project_id }, { customer_id: customer });
|
||||
|
||||
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,30 +8,29 @@ 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 });
|
||||
const pid = event.data.object.subscription_details?.metadata?.pid;
|
||||
|
||||
const project = await ProjectModel.findById(pid);
|
||||
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 = event.data.object.lines.data[0].plan?.id;
|
||||
if (!price) return { error: 'Price 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 }, {
|
||||
await ProjectModel.updateOne({ _id: pid }, {
|
||||
premium: true,
|
||||
customer_id: event.data.object.customer,
|
||||
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];
|
||||
@@ -43,8 +42,8 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
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,
|
||||
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 }
|
||||
@@ -61,9 +60,13 @@ async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEve
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
|
||||
const body = await readRawBody(event);
|
||||
const signature = getHeader(event, 'stripe-signature') || '';
|
||||
|
||||
@@ -72,6 +75,7 @@ export default defineEventHandler(async event => {
|
||||
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);
|
||||
if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
|
||||
|
||||
return { received: true }
|
||||
});
|
||||
@@ -22,16 +22,20 @@ class StripeService {
|
||||
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) {
|
||||
console.error('Stripe not initialized')
|
||||
return;
|
||||
}
|
||||
|
||||
const checkout = await this.stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
line_items: [
|
||||
{ price, quantity: 1 }
|
||||
],
|
||||
subscription_data: {
|
||||
metadata: { pid },
|
||||
},
|
||||
customer,
|
||||
success_url,
|
||||
mode: 'subscription'
|
||||
@@ -48,6 +52,11 @@ class StripeService {
|
||||
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
|
||||
return subscription;
|
||||
}
|
||||
|
||||
async getInvoices(customer_id: string) {
|
||||
const invoices = await this.stripe?.invoices.list({ customer: customer_id });
|
||||
return invoices;
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new StripeService();
|
||||
|
||||
Reference in New Issue
Block a user