mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
new selfhosted version
This commit is contained in:
18
dashboard/server/api/payments/cancel_plan.ts
Normal file
18
dashboard/server/api/payments/cancel_plan.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
//TODO: SELFHOST
|
||||
|
||||
const ctx = await getRequestContext(event, 'flag:allowAnonRegistered');
|
||||
const { user_id } = ctx;
|
||||
|
||||
const tRpc = useTRPC();
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id });
|
||||
if (!premiumData) throw createError({ status: 400, message: 'Error getting premium data. Please contact support.' });
|
||||
|
||||
const result = await tRpc.payments.subscription.cancelPlan.mutate({ user_id, customer_id: premiumData.customer_id });
|
||||
return result;
|
||||
});
|
||||
45
dashboard/server/api/payments/change_plan.ts
Normal file
45
dashboard/server/api/payments/change_plan.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { getPlanFromTag } from "~/shared/data/PLANS";
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
//TODO: SELFHOST
|
||||
|
||||
const ctx = await getRequestContext(event, 'flag:allowAnonRegistered');
|
||||
const { user_id } = ctx;
|
||||
|
||||
const { plan } = getQuery(event);
|
||||
|
||||
const newPlan = getPlanFromTag(plan as any);
|
||||
if (!newPlan) throw createError({ status: 400, message: 'Plan not valid. Please contact support.' });
|
||||
|
||||
const tRpc = useTRPC();
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id });
|
||||
if (!premiumData) throw createError({ status: 400, message: 'Premium data not found. Please contact support.' });
|
||||
|
||||
const paymentMethods = await tRpc.payments.customer.listMethods.query({ customer_id: premiumData.customer_id });
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
const redirect_url = config.BASE_URL + '/payment_ok';
|
||||
|
||||
if (paymentMethods.data.length > 0) {
|
||||
|
||||
if (premiumData.payment_failed) {
|
||||
const checkout_url = await tRpc.payments.subscription.checkout.mutate({ customer_id: premiumData.customer_id, user_id: premiumData.user_id.toString(), plan_tag: newPlan.TAG, redirect_url });
|
||||
return { ok: true, soft: false, url: checkout_url }
|
||||
} else {
|
||||
return ({ ok: true, soft: true, url: `/checkout?plan_tag=${newPlan.TAG}` });
|
||||
// await tRpc.payments.subscription.activate.mutate({ customer_id: premiumData.customer_id, user_id: premiumData.user_id.toString(), plan_tag: newPlan.TAG });
|
||||
// return { ok: true }
|
||||
}
|
||||
|
||||
} else {
|
||||
const checkout_url = await tRpc.payments.subscription.checkout.mutate({ customer_id: premiumData.customer_id, user_id: premiumData.user_id.toString(), plan_tag: newPlan.TAG, redirect_url });
|
||||
return { ok: true, soft: false, url: checkout_url }
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
13
dashboard/server/api/payments/check_info.ts
Normal file
13
dashboard/server/api/payments/check_info.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const ctx = await getRequestContext(event, 'flag:allowAnonRegistered');
|
||||
throw createError({ status: 400, message: 'NOT IMPLEMENTED' });
|
||||
// const { user_id } = ctx;
|
||||
// const tRpc = useTRPC();
|
||||
// const premiumData = await PremiumModel.findOne({ user_id });
|
||||
// if (!premiumData) throw createError({ status: 400, message: 'Premium data not found. Please contact support.' });
|
||||
// const paymentMethods = await tRpc.payments.customer.listMethods.query({ customer_id: premiumData.customer_id });
|
||||
// return { paymentMethodsCount: paymentMethods.data.length }
|
||||
});
|
||||
27
dashboard/server/api/payments/preview_upgrade.ts
Normal file
27
dashboard/server/api/payments/preview_upgrade.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const ctx = await getRequestContext(event, 'flag:allowAnonRegistered');
|
||||
|
||||
|
||||
//TODO: SELFHOST
|
||||
|
||||
const { user_id } = ctx;
|
||||
|
||||
const { plan_tag } = getQuery(event);
|
||||
|
||||
const tRpc = useTRPC();
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id })
|
||||
if (!premiumData) throw createError({ status: 400, message: 'Cannot get premium data. Please contact support.' });
|
||||
|
||||
|
||||
const preview = await tRpc.payments.subscription.createPreviewUpgrade.query({
|
||||
user_id,
|
||||
customer_id: premiumData.customer_id,
|
||||
plan_tag: plan_tag as string
|
||||
});
|
||||
|
||||
return preview;
|
||||
|
||||
});
|
||||
27
dashboard/server/api/payments/upgrade.ts
Normal file
27
dashboard/server/api/payments/upgrade.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const ctx = await getRequestContext(event, 'flag:allowAnonRegistered');
|
||||
|
||||
|
||||
//TODO: SELFHOST
|
||||
|
||||
const { user_id } = ctx;
|
||||
|
||||
const { plan_tag } = getQuery(event);
|
||||
|
||||
const tRpc = useTRPC();
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id })
|
||||
if (!premiumData) throw createError({ status: 400, message: 'Cannot get premium data. Please contact support.' });
|
||||
|
||||
|
||||
const preview = await tRpc.payments.subscription.createUpgrade.mutate({
|
||||
user_id,
|
||||
customer_id: premiumData.customer_id,
|
||||
plan_tag: plan_tag as string
|
||||
});
|
||||
|
||||
return preview;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user