update subscription algorithm
This commit is contained in:
@@ -7,16 +7,23 @@ import { ok } from '../../http/respond'
|
||||
import * as amanpay from '../../services/amanpayService'
|
||||
import * as paypal from '../../services/paypalService'
|
||||
import * as service from './subscription.service'
|
||||
import { checkoutSchema, changePlanSchema, capturePaypalSchema } from './subscription.schemas'
|
||||
import {
|
||||
checkoutSchema,
|
||||
changePlanSchema,
|
||||
capturePaypalSchema,
|
||||
startTrialSchema,
|
||||
cancelSchema,
|
||||
reactivateSchema,
|
||||
} from './subscription.schemas'
|
||||
|
||||
const publicRouter = Router()
|
||||
const publicRouter = Router()
|
||||
const webhookRouter = Router()
|
||||
const router = Router()
|
||||
const router = Router()
|
||||
|
||||
// ─── Public ────────────────────────────────────────────────────
|
||||
|
||||
publicRouter.get('/plans', (_req, res) => {
|
||||
ok(res, service.getPlans())
|
||||
publicRouter.get('/plans', (_req, res, next) => {
|
||||
service.getPlans().then((d) => ok(res, d)).catch(next)
|
||||
})
|
||||
|
||||
publicRouter.get('/providers', (_req, res) => {
|
||||
@@ -27,7 +34,7 @@ publicRouter.get('/providers', (_req, res) => {
|
||||
|
||||
webhookRouter.post('/webhooks/amanpay', async (req, res, next) => {
|
||||
try {
|
||||
const rawBody = JSON.stringify(req.body)
|
||||
const rawBody = JSON.stringify(req.body)
|
||||
const signature = (req.headers['x-amanpay-signature'] as string) ?? ''
|
||||
if (!amanpay.isConfigured() || !amanpay.verifyWebhookSignature(rawBody, signature)) {
|
||||
return res.status(401).json({ error: 'invalid_signature' })
|
||||
@@ -56,19 +63,30 @@ router.post('/capture-paypal', requireCompanyAuth, requireTenant, async (req, re
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
// ─── Authenticated ────────────────────────────────────────────
|
||||
// ─── Authenticated ─────────────────────────────────────────────
|
||||
|
||||
router.use(requireCompanyAuth, requireTenant)
|
||||
|
||||
router.get('/me', async (req, res, next) => {
|
||||
try {
|
||||
ok(res, await service.getSubscription(req.companyId))
|
||||
} catch (err) { next(err) }
|
||||
try { ok(res, await service.getSubscription(req.companyId)) } catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.get('/invoices', async (req, res, next) => {
|
||||
try { ok(res, await service.getInvoices(req.companyId)) } catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.get('/events', async (req, res, next) => {
|
||||
try { ok(res, await service.getEvents(req.companyId)) } catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.get('/entitlement', async (req, res, next) => {
|
||||
try { ok(res, await service.getEntitlement(req.companyId)) } catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/trial', requireRole('OWNER'), async (req, res, next) => {
|
||||
try {
|
||||
ok(res, await service.getInvoices(req.companyId))
|
||||
const body = parseBody(startTrialSchema, req)
|
||||
ok(res, await service.startTrial(req.companyId, body.plan, body.billingPeriod, body.currency))
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
@@ -79,6 +97,13 @@ router.post('/checkout', requireRole('OWNER'), async (req, res, next) => {
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/reactivate', requireRole('OWNER'), async (req, res, next) => {
|
||||
try {
|
||||
const body = parseBody(reactivateSchema, req)
|
||||
ok(res, await service.reactivate(req.companyId, body))
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/change-plan', requireRole('OWNER'), async (req, res, next) => {
|
||||
try {
|
||||
const body = parseBody(changePlanSchema, req)
|
||||
@@ -88,14 +113,13 @@ router.post('/change-plan', requireRole('OWNER'), async (req, res, next) => {
|
||||
|
||||
router.post('/cancel', requireRole('OWNER'), async (req, res, next) => {
|
||||
try {
|
||||
ok(res, await service.cancel(req.companyId))
|
||||
const { mode, reason } = parseBody(cancelSchema, req)
|
||||
ok(res, await service.cancel(req.companyId, mode, reason))
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
router.post('/resume', requireRole('OWNER'), async (req, res, next) => {
|
||||
try {
|
||||
ok(res, await service.resume(req.companyId))
|
||||
} catch (err) { next(err) }
|
||||
try { ok(res, await service.resume(req.companyId)) } catch (err) { next(err) }
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user