mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
fix dashboard + payments
This commit is contained in:
@@ -80,7 +80,10 @@ export async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
await addSubscriptionToUser(premiumData.user_id.toString(), plan, subscription_id, event.data.object.period_start, event.data.object.period_end);
|
||||
await addSubscriptionToUser(premiumData.user_id.toString(), plan, subscription_id,
|
||||
event.data.object.lines.data[0].period.start,
|
||||
event.data.object.lines.data[0].period.end
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
if (plan.ID == 0) return;
|
||||
|
||||
@@ -17,6 +17,12 @@ console.log('Stripe started in', STRIPE_TESTMODE ? 'TESTMODE' : 'LIVEMODE');
|
||||
|
||||
const app = express();
|
||||
|
||||
|
||||
app.use((req, res, next) => {
|
||||
console.log(req.path);
|
||||
next();
|
||||
})
|
||||
|
||||
app.use('/webhook', webhookRouter);
|
||||
app.use('/payment', paymentRouter);
|
||||
|
||||
|
||||
@@ -5,14 +5,41 @@ import StripeService from '../services/StripeService';
|
||||
import { sendJson } from '../Utils';
|
||||
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
||||
import { Types } from 'mongoose';
|
||||
import { UserModel } from '../shared/schema/UserSchema';
|
||||
|
||||
export const paymentRouter = Router();
|
||||
|
||||
export const ZBodyCreateCustomer = z.object({
|
||||
user_id: z.string()
|
||||
});
|
||||
|
||||
paymentRouter.post('/create_customer', json(), async (req, res) => {
|
||||
try {
|
||||
const createCustomerData = ZBodyCreateCustomer.parse(req.body);
|
||||
const user = await UserModel.findOne({ _id: createCustomerData.user_id });
|
||||
if (!user) return sendJson(res, 400, { error: 'user not found' });
|
||||
|
||||
const customer = await StripeService.createCustomer(user.email);
|
||||
const freesub = await StripeService.createFreeSubscription(customer.id);
|
||||
|
||||
await PremiumModel.create({
|
||||
user_id: user.id,
|
||||
customer_id: customer.id,
|
||||
premium_type: 0,
|
||||
subscription_id: freesub.id
|
||||
})
|
||||
|
||||
return sendJson(res, 200, { ok: true });
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const ZBodyCreatePayment = z.object({
|
||||
user_id: z.string(),
|
||||
plan_id: z.number()
|
||||
})
|
||||
});
|
||||
|
||||
paymentRouter.post('/create', json(), async (req, res) => {
|
||||
try {
|
||||
@@ -38,6 +65,84 @@ paymentRouter.post('/create', json(), async (req, res) => {
|
||||
|
||||
return sendJson(res, 200, { url: checkout.url });
|
||||
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const ZBodyInvoicesList = z.object({
|
||||
user_id: z.string()
|
||||
});
|
||||
|
||||
paymentRouter.post('/invoices_list', json(), async (req, res) => {
|
||||
try {
|
||||
const invoicesListData = ZBodyInvoicesList.parse(req.body);
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id: invoicesListData.user_id });
|
||||
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
||||
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user have no customer_id' });
|
||||
|
||||
const invoices = await StripeService.getInvoices(premiumData.customer_id);
|
||||
return sendJson(res, 200, { invoices: invoices.data });
|
||||
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const ZBodyCustomerInfo = z.object({
|
||||
user_id: z.string()
|
||||
});
|
||||
|
||||
paymentRouter.post('/customer_info', json(), async (req, res) => {
|
||||
try {
|
||||
const customerInfoData = ZBodyCustomerInfo.parse(req.body);
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id: customerInfoData.user_id });
|
||||
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
||||
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user have no customer_id' });
|
||||
|
||||
const customer = await StripeService.getCustomer(premiumData.customer_id);
|
||||
if (!customer) return sendJson(res, 200, {});
|
||||
if (customer.deleted === true) return sendJson(res, 200, {});
|
||||
return sendJson(res, 200, customer.address);
|
||||
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
export const ZBodyUpdateCustomerInfo = z.object({
|
||||
user_id: z.string(),
|
||||
address: z.object({
|
||||
line1: z.string(),
|
||||
line2: z.string(),
|
||||
city: z.string(),
|
||||
country: z.string(),
|
||||
postal_code: z.string(),
|
||||
state: z.string()
|
||||
})
|
||||
});
|
||||
|
||||
paymentRouter.post('/update_customer_info', json(), async (req, res) => {
|
||||
try {
|
||||
const updateCustomerInfoData = ZBodyUpdateCustomerInfo.parse(req.body);
|
||||
|
||||
const premiumData = await PremiumModel.findOne({ user_id: updateCustomerInfoData.user_id });
|
||||
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
||||
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user have no customer_id' });
|
||||
await StripeService.setCustomerInfo(
|
||||
premiumData.customer_id,
|
||||
updateCustomerInfoData.address as any
|
||||
);
|
||||
return sendJson(res, 200, { ok: true });
|
||||
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user