191 lines
7.6 KiB
JavaScript
191 lines
7.6 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.subscriptionWebhookRouter = exports.subscriptionPublicRouter = void 0;
|
|
const express_1 = require("express");
|
|
const requireCompanyAuth_1 = require("../../middleware/requireCompanyAuth");
|
|
const requireTenant_1 = require("../../middleware/requireTenant");
|
|
const requireRole_1 = require("../../middleware/requireRole");
|
|
const validate_1 = require("../../http/validate");
|
|
const respond_1 = require("../../http/respond");
|
|
const webhooks_1 = require("../../http/webhooks");
|
|
const amanpay = __importStar(require("../../services/amanpayService"));
|
|
const paypal = __importStar(require("../../services/paypalService"));
|
|
const service = __importStar(require("./subscription.service"));
|
|
const subscription_schemas_1 = require("./subscription.schemas");
|
|
const publicRouter = (0, express_1.Router)();
|
|
exports.subscriptionPublicRouter = publicRouter;
|
|
const webhookRouter = (0, express_1.Router)();
|
|
exports.subscriptionWebhookRouter = webhookRouter;
|
|
const router = (0, express_1.Router)();
|
|
// ─── Public ────────────────────────────────────────────────────
|
|
publicRouter.get('/plans', (_req, res, next) => {
|
|
service.getPlans().then((d) => (0, respond_1.ok)(res, d)).catch(next);
|
|
});
|
|
publicRouter.get('/providers', (_req, res) => {
|
|
(0, respond_1.ok)(res, service.getProviders());
|
|
});
|
|
publicRouter.get('/features', (_req, res, next) => {
|
|
service.getPlanFeatures().then((d) => (0, respond_1.ok)(res, d)).catch(next);
|
|
});
|
|
// ─── Webhooks (no auth) ────────────────────────────────────────
|
|
webhookRouter.post('/webhooks/amanpay', async (req, res, next) => {
|
|
try {
|
|
const rawBody = (0, webhooks_1.getRawBodyString)(req);
|
|
const payload = (0, webhooks_1.parseRawJsonBody)(req);
|
|
const signature = req.headers['x-amanpay-signature'] ?? '';
|
|
if (!amanpay.isConfigured() || !amanpay.verifyWebhookSignature(rawBody, signature)) {
|
|
return res.status(401).json({ error: 'invalid_signature' });
|
|
}
|
|
await service.handleAmanpayWebhook(payload, rawBody);
|
|
res.json({ received: true });
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
webhookRouter.post('/webhooks/paypal', async (req, res, next) => {
|
|
try {
|
|
const rawBody = (0, webhooks_1.getRawBodyString)(req);
|
|
const payload = (0, webhooks_1.parseRawJsonBody)(req);
|
|
const isValid = await paypal.verifyWebhookEvent(req.headers, rawBody);
|
|
if (!paypal.isConfigured() || !isValid)
|
|
return res.status(401).json({ error: 'invalid_signature' });
|
|
await service.handlePaypalWebhook(payload, rawBody);
|
|
res.json({ received: true });
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
// ─── PayPal capture (auth but no subscription check) ──────────
|
|
router.post('/capture-paypal', requireCompanyAuth_1.requireCompanyAuth, requireTenant_1.requireTenant, async (req, res, next) => {
|
|
try {
|
|
const { paypalOrderId } = (0, validate_1.parseBody)(subscription_schemas_1.capturePaypalSchema, req);
|
|
(0, respond_1.ok)(res, await service.capturePaypal(req.companyId, paypalOrderId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
// ─── Authenticated ─────────────────────────────────────────────
|
|
router.use(requireCompanyAuth_1.requireCompanyAuth, requireTenant_1.requireTenant);
|
|
router.get('/me', async (req, res, next) => {
|
|
try {
|
|
(0, respond_1.ok)(res, await service.getSubscription(req.companyId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.get('/invoices', async (req, res, next) => {
|
|
try {
|
|
(0, respond_1.ok)(res, await service.getInvoices(req.companyId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.get('/events', async (req, res, next) => {
|
|
try {
|
|
(0, respond_1.ok)(res, await service.getEvents(req.companyId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.get('/entitlement', async (req, res, next) => {
|
|
try {
|
|
(0, respond_1.ok)(res, await service.getEntitlement(req.companyId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/trial', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
const body = (0, validate_1.parseBody)(subscription_schemas_1.startTrialSchema, req);
|
|
(0, respond_1.ok)(res, await service.startTrial(req.companyId, body.plan, body.billingPeriod, body.currency));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/checkout', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
const body = (0, validate_1.parseBody)(subscription_schemas_1.checkoutSchema, req);
|
|
(0, respond_1.ok)(res, await service.checkout(req.companyId, body));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/reactivate', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
const body = (0, validate_1.parseBody)(subscription_schemas_1.reactivateSchema, req);
|
|
(0, respond_1.ok)(res, await service.reactivate(req.companyId, body));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/change-plan', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
const body = (0, validate_1.parseBody)(subscription_schemas_1.changePlanSchema, req);
|
|
(0, respond_1.ok)(res, await service.changePlan(req.companyId, body));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/cancel', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
const { mode, reason } = (0, validate_1.parseBody)(subscription_schemas_1.cancelSchema, req);
|
|
(0, respond_1.ok)(res, await service.cancel(req.companyId, mode, reason));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
router.post('/resume', (0, requireRole_1.requireRole)('OWNER'), async (req, res, next) => {
|
|
try {
|
|
(0, respond_1.ok)(res, await service.resume(req.companyId));
|
|
}
|
|
catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
exports.default = router;
|
|
//# sourceMappingURL=subscription.routes.js.map
|