feat: integrate LemonSqueezy to subscription payment

This commit is contained in:
Ahmed Bouhuolia
2024-04-14 10:33:29 +02:00
parent 9807ac04b0
commit 693ae61141
10 changed files with 363 additions and 25 deletions

View File

@@ -1,16 +1,21 @@
import { Router, Request, Response, NextFunction } from 'express';
import { Container, Service, Inject } from 'typedi';
import { Service, Inject } from 'typedi';
import { body } from 'express-validator';
import JWTAuth from '@/api/middleware/jwtAuth';
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
import PaymentViaLicenseController from '@/api/controllers/Subscription/PaymentViaLicense';
import SubscriptionService from '@/services/Subscription/SubscriptionService';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import BaseController from '../BaseController';
import { LemonSqueezyService } from '@/services/Subscription/LemonSqueezyService';
@Service()
export default class SubscriptionController {
export default class SubscriptionController extends BaseController {
@Inject()
subscriptionService: SubscriptionService;
private subscriptionService: SubscriptionService;
@Inject()
private lemonSqueezyService: LemonSqueezyService;
/**
* Router constructor.
@@ -22,7 +27,12 @@ export default class SubscriptionController {
router.use(AttachCurrentTenantUser);
router.use(TenancyMiddleware);
router.use('/license', Container.get(PaymentViaLicenseController).router());
router.post(
'/lemon/checkout_url',
[body('variantId').exists().trim()],
this.validationResult,
this.getCheckoutUrl.bind(this)
);
router.get('/', asyncMiddleware(this.getSubscriptions.bind(this)));
return router;
@@ -34,7 +44,11 @@ export default class SubscriptionController {
* @param {Response} res
* @param {NextFunction} next
*/
async getSubscriptions(req: Request, res: Response, next: NextFunction) {
private async getSubscriptions(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
try {
@@ -46,4 +60,29 @@ export default class SubscriptionController {
next(error);
}
}
/**
* Retrieves the LemonSqueezy checkout url.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async getCheckoutUrl(
req: Request,
res: Response,
next: NextFunction
) {
const { variantId } = this.matchedBodyData(req);
const { user } = req;
try {
const checkout = await this.lemonSqueezyService.getCheckout(
variantId,
user
);
return res.status(200).send(checkout);
} catch (error) {
next(error);
}
}
}