"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.StripeController = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const stripe_service_1 = require("../services/stripe.service"); const stripe_webhook_dto_1 = require("../dto/stripe-webhook.dto"); const jwt_auth_guard_1 = require("../../auth/guards/jwt-auth.guard"); const permissions_guard_1 = require("../../rbac/guards/permissions.guard"); const current_user_decorator_1 = require("../../auth/decorators/current-user.decorator"); let StripeController = class StripeController { constructor(stripeService) { this.stripeService = stripeService; } async createCheckoutSession(dto, user) { dto.tenant_id = user.tenant_id; const session = await this.stripeService.createCheckoutSession(dto); return { session_id: session.id, url: session.url, }; } async createBillingPortalSession(dto, user) { dto.tenant_id = user.tenant_id; const session = await this.stripeService.createBillingPortalSession(dto); return { url: session.url, }; } async createSetupIntent(user) { const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id); if (!customer) { return { error: 'Stripe customer not found', client_secret: null, }; } const setupIntent = await this.stripeService.createSetupIntent(customer.id); return { client_secret: setupIntent.client_secret, }; } async listPrices(productId) { const prices = await this.stripeService.listPrices(productId); return prices.map((price) => ({ id: price.id, product: typeof price.product === 'string' ? price.product : price.product?.name, currency: price.currency, unit_amount: price.unit_amount, interval: price.recurring?.interval, interval_count: price.recurring?.interval_count, })); } async getCustomer(user) { const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id); if (!customer) { return { exists: false, customer: null, }; } return { exists: true, customer: { id: customer.id, email: customer.email, name: customer.name, created: customer.created, }, }; } async createCustomer(user, body) { const existingCustomer = await this.stripeService.findCustomerByTenantId(user.tenant_id); if (existingCustomer) { return { created: false, customer: { id: existingCustomer.id, email: existingCustomer.email, name: existingCustomer.name, }, message: 'Customer already exists', }; } const customer = await this.stripeService.createCustomer({ tenant_id: user.tenant_id, email: body.email, name: body.name, }); return { created: true, customer: { id: customer.id, email: customer.email, name: customer.name, }, }; } async listPaymentMethods(user) { const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id); if (!customer) { return { payment_methods: [] }; } const paymentMethods = await this.stripeService.listPaymentMethods(customer.id); return { payment_methods: paymentMethods.map((pm) => ({ id: pm.id, type: pm.type, card: pm.card ? { brand: pm.card.brand, last4: pm.card.last4, exp_month: pm.card.exp_month, exp_year: pm.card.exp_year, } : null, created: pm.created, })), }; } }; exports.StripeController = StripeController; __decorate([ (0, common_1.Post)('checkout-session'), (0, common_1.UseGuards)(permissions_guard_1.PermissionsGuard), (0, permissions_guard_1.RequirePermissions)('billing:manage'), (0, swagger_1.ApiOperation)({ summary: 'Create Stripe checkout session' }), __param(0, (0, common_1.Body)()), __param(1, (0, current_user_decorator_1.CurrentUser)()), __metadata("design:type", Function), __metadata("design:paramtypes", [stripe_webhook_dto_1.CreateCheckoutSessionDto, Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "createCheckoutSession", null); __decorate([ (0, common_1.Post)('billing-portal'), (0, common_1.UseGuards)(permissions_guard_1.PermissionsGuard), (0, permissions_guard_1.RequirePermissions)('billing:manage'), (0, swagger_1.ApiOperation)({ summary: 'Create Stripe billing portal session' }), __param(0, (0, common_1.Body)()), __param(1, (0, current_user_decorator_1.CurrentUser)()), __metadata("design:type", Function), __metadata("design:paramtypes", [stripe_webhook_dto_1.CreateBillingPortalSessionDto, Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "createBillingPortalSession", null); __decorate([ (0, common_1.Post)('setup-intent'), (0, common_1.UseGuards)(permissions_guard_1.PermissionsGuard), (0, permissions_guard_1.RequirePermissions)('billing:manage'), (0, swagger_1.ApiOperation)({ summary: 'Create setup intent for adding payment method' }), __param(0, (0, current_user_decorator_1.CurrentUser)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "createSetupIntent", null); __decorate([ (0, common_1.Get)('prices'), (0, swagger_1.ApiOperation)({ summary: 'List available Stripe prices/plans' }), (0, swagger_1.ApiQuery)({ name: 'product_id', required: false }), __param(0, (0, common_1.Query)('product_id')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], StripeController.prototype, "listPrices", null); __decorate([ (0, common_1.Get)('customer'), (0, swagger_1.ApiOperation)({ summary: 'Get Stripe customer for current tenant' }), __param(0, (0, current_user_decorator_1.CurrentUser)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "getCustomer", null); __decorate([ (0, common_1.Post)('customer'), (0, common_1.UseGuards)(permissions_guard_1.PermissionsGuard), (0, permissions_guard_1.RequirePermissions)('billing:manage'), (0, swagger_1.ApiOperation)({ summary: 'Create Stripe customer for tenant' }), __param(0, (0, current_user_decorator_1.CurrentUser)()), __param(1, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "createCustomer", null); __decorate([ (0, common_1.Get)('payment-methods'), (0, swagger_1.ApiOperation)({ summary: 'List Stripe payment methods' }), __param(0, (0, current_user_decorator_1.CurrentUser)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], StripeController.prototype, "listPaymentMethods", null); exports.StripeController = StripeController = __decorate([ (0, swagger_1.ApiTags)('stripe'), (0, common_1.Controller)('stripe'), (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard), (0, swagger_1.ApiBearerAuth)(), __metadata("design:paramtypes", [stripe_service_1.StripeService]) ], StripeController); //# sourceMappingURL=stripe.controller.js.map