mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
add code redeem
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { PREMIUM_PLAN } from "../../../../shared/data/PREMIUM";
|
||||
import { checkAppsumoCode, useAppsumoCode } from "~/server/services/AppsumoService";
|
||||
|
||||
|
||||
import { canTryAppsumoCode, checkAppsumoCode, useAppsumoCode, useTryAppsumoCode } from "~/server/services/AppsumoService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
function getPlanToActivate(current_plan_id: number) {
|
||||
if (current_plan_id === PREMIUM_PLAN.FREE.ID) {
|
||||
@@ -28,24 +26,26 @@ export default defineEventHandler(async event => {
|
||||
const data = await getRequestData(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
||||
if (!data) return;
|
||||
|
||||
const { project, pid } = data;
|
||||
const { project, pid, user } = data;
|
||||
|
||||
const body = await readBody(event);
|
||||
|
||||
const { code } = body;
|
||||
|
||||
const valid = await checkAppsumoCode(code);
|
||||
const canTry = await canTryAppsumoCode(pid);
|
||||
if (!canTry) return setResponseStatus(event, 400, 'You tried too much codes. Please contact support.');
|
||||
await useTryAppsumoCode(pid, code);
|
||||
|
||||
if (!valid) return setResponseStatus(event, 400, 'Current plan not found');
|
||||
const valid = await checkAppsumoCode(code);
|
||||
if (!valid) return setResponseStatus(event, 400, 'Code not valid');
|
||||
|
||||
const currentPlan = getPlanFromId(project.premium_type);
|
||||
if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
|
||||
const planToActivate = getPlanToActivate(currentPlan.ID);
|
||||
if (!planToActivate) return setResponseStatus(event, 400, 'Cannot use code on current plan');
|
||||
|
||||
await StripeService.deleteSubscription(project.subscription_id);
|
||||
await StripeService.createSubscription(project.customer_id, planToActivate.ID);
|
||||
|
||||
await useAppsumoCode(code);
|
||||
await useAppsumoCode(pid, code);
|
||||
|
||||
});
|
||||
14
dashboard/server/api/pay/valid_codes.ts
Normal file
14
dashboard/server/api/pay/valid_codes.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { AppsumoCodeTryModel } from "@schema/AppsumoCodeTrySchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
||||
if (!data) return;
|
||||
|
||||
const { pid } = data;
|
||||
|
||||
const tryRes = await AppsumoCodeTryModel.findOne({ project_id: pid }, { valid_codes: 1 });
|
||||
if (!tryRes) return { count: 0 }
|
||||
return { count: tryRes.valid_codes.length }
|
||||
|
||||
});
|
||||
@@ -133,7 +133,7 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
if (!price) return { error: 'Price not found' }
|
||||
|
||||
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||
if (!PLAN) return { error: 'Plan not found' }
|
||||
if (!PLAN) return { error: `Plan not found. Price: ${price}. TestMode: ${StripeService.testMode}` }
|
||||
|
||||
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user