File size: 8,973 Bytes
303c85f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const better_auth_guard_1 = require("../auth/better-auth.guard");
const billing_service_1 = require("./billing.service");
const stripe_webhook_guard_1 = require("./stripe-webhook.guard");
const mpesa_webhook_guard_1 = require("./mpesa-webhook.guard");
const payhero_webhook_guard_1 = require("./payhero-webhook.guard");
const paystack_webhook_guard_1 = require("./paystack-webhook.guard");
const billing_dto_1 = require("./billing.dto");
const raw_body_middleware_1 = require("../common/middleware/raw-body.middleware");
let BillingController = class BillingController {
    billingService;
    constructor(billingService) {
        this.billingService = billingService;
    }
    listPlans() {
        return this.billingService.listPlans();
    }
    getWorkspaceSubscription(req, workspaceId) {
        const userId = req.userId || req.headers["x-user-id"];
        return this.billingService.getWorkspaceSubscription(userId, workspaceId);
    }
    createStripeCheckout(req, body) {
        const userId = req.userId || req.headers["x-user-id"];
        return this.billingService.createStripeCheckout(userId, body);
    }
    initiateMpesaCheckout(req, body) {
        const userId = req.userId || req.headers["x-user-id"];
        return this.billingService.initiateMpesaCheckout(userId, body);
    }
    initiatePaystackCheckout(req, body) {
        const userId = req.userId || req.headers["x-user-id"];
        return this.billingService.initiatePaystackCheckout(userId, {
            ...body,
            email: body.email || `${userId}@placeholder.local`,
        });
    }
    verifyPaystackCheckout(req, body) {
        const userId = req.userId || req.headers["x-user-id"];
        return this.billingService.verifyPaystackCheckout(userId, body.workspaceId, body.reference);
    }
    handleStripeWebhook(req) {
        const event = req.stripeEvent;
        return this.billingService.handleStripeWebhook(event);
    }
    handleMpesaWebhook(req) {
        return this.billingService.handleMpesaWebhook(req.body);
    }
    handlePayheroWebhook(req) {
        return this.billingService.handlePayheroWebhook(req.body, req.query);
    }
    handlePaystackWebhook(req) {
        return this.billingService.handlePaystackWebhook(req.body);
    }
};
exports.BillingController = BillingController;
__decorate([
    (0, common_1.Get)("plans"),
    (0, swagger_1.ApiOperation)({ summary: "List all subscription plans" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "listPlans", null);
__decorate([
    (0, common_1.Get)("workspaces/:workspaceId/subscription"),
    (0, common_1.UseGuards)(better_auth_guard_1.BetterAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiParam)({ name: "workspaceId" }),
    (0, swagger_1.ApiOperation)({ summary: "Get current workspace subscription" }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Param)("workspaceId")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "getWorkspaceSubscription", null);
__decorate([
    (0, common_1.Post)("checkout/stripe"),
    (0, common_1.UseGuards)(better_auth_guard_1.BetterAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: "Initialize Stripe checkout" }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, billing_dto_1.CreateStripeCheckoutDto]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "createStripeCheckout", null);
__decorate([
    (0, common_1.Post)("checkout/mpesa"),
    (0, common_1.UseGuards)(better_auth_guard_1.BetterAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: "Initialize M-Pesa STK push checkout" }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, billing_dto_1.InitiateMpesaCheckoutDto]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "initiateMpesaCheckout", null);
__decorate([
    (0, common_1.Post)("checkout/paystack"),
    (0, common_1.UseGuards)(better_auth_guard_1.BetterAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({
        summary: "Initialize Paystack checkout (card, M-Pesa, Airtel Money)",
    }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "initiatePaystackCheckout", null);
__decorate([
    (0, common_1.Post)("checkout/paystack/verify"),
    (0, common_1.UseGuards)(better_auth_guard_1.BetterAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: "Manually verify a Paystack transaction reference" }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "verifyPaystackCheckout", null);
__decorate([
    (0, common_1.Post)("webhooks/stripe"),
    (0, common_1.UseInterceptors)(raw_body_middleware_1.RawBodyMiddleware),
    (0, common_1.UseGuards)(stripe_webhook_guard_1.StripeWebhookGuard),
    (0, swagger_1.ApiOperation)({ summary: "Stripe webhook callback - signature verified" }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "handleStripeWebhook", null);
__decorate([
    (0, common_1.Post)("webhooks/mpesa"),
    (0, common_1.UseInterceptors)(raw_body_middleware_1.RawBodyMiddleware),
    (0, common_1.UseGuards)(mpesa_webhook_guard_1.MpesaWebhookGuard),
    (0, swagger_1.ApiOperation)({ summary: "M-Pesa callback - signature verified in production" }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "handleMpesaWebhook", null);
__decorate([
    (0, common_1.Post)("webhooks/payhero"),
    (0, common_1.UseInterceptors)(raw_body_middleware_1.RawBodyMiddleware),
    (0, common_1.UseGuards)(payhero_webhook_guard_1.PayheroWebhookGuard),
    (0, swagger_1.ApiOperation)({ summary: "PayHero webhook callback - query signature verified" }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "handlePayheroWebhook", null);
__decorate([
    (0, common_1.Post)("webhooks/paystack"),
    (0, common_1.UseInterceptors)(raw_body_middleware_1.RawBodyMiddleware),
    (0, common_1.UseGuards)(paystack_webhook_guard_1.PaystackWebhookGuard),
    (0, swagger_1.ApiOperation)({
        summary: "Paystack webhook callback - HMAC-SHA512 signature verified",
    }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], BillingController.prototype, "handlePaystackWebhook", null);
exports.BillingController = BillingController = __decorate([
    (0, swagger_1.ApiTags)("Billing"),
    (0, common_1.Controller)("billing"),
    __metadata("design:paramtypes", [billing_service_1.BillingService])
], BillingController);
//# sourceMappingURL=billing.controller.js.map