All files / src/platform/billing billing.controller.ts

0% Statements 0/41
0% Branches 0/27
0% Functions 0/13
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                               
import {
  Controller,
  Get,
  Post,
  Param,
  Body,
  Query,
  Req,
  Headers,
} from '@nestjs/common';
import { BillingService } from './billing.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/billing')
@ApiTags('Billing')
export class BillingController {
  constructor(private readonly billingService: BillingService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Post('checkout')
  @ApiOperation({ summary: 'Create checkout operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async createCheckout(
    @Body()
    body: { invoiceId: string; providerName: string; callbackUrl: string },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.billingService.createCheckout({
      ...body,
      tenantId: ctx.tenantId,
    });
  }
 
  @Get('invoices')
  @ApiOperation({ summary: 'Get invoices operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getInvoices(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.billingService.getInvoices(ctx.tenantId);
  }
 
  @Get('invoices/:id')
  @ApiOperation({ summary: 'Get invoice operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getInvoice(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.billingService.getInvoice(id, ctx.tenantId);
  }
}
 
@Controller('api/v1/webhooks')
@ApiTags('PaymentWebhook')
export class PaymentWebhookController {
  constructor(private readonly billingService: BillingService) {}
 
  @Post('stripe')
  @ApiOperation({ summary: 'Handle stripe operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async handleStripe(
    @Body() body: any,
    @Headers('stripe-signature') signature: string,
  ) {
    return this.billingService.processWebhook('Stripe', body, signature || '');
  }
 
  @Post('razorpay')
  @ApiOperation({ summary: 'Handle razorpay operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async handleRazorpay(
    @Body() body: any,
    @Headers('x-razorpay-signature') signature: string,
  ) {
    return this.billingService.processWebhook(
      'Razorpay',
      body,
      signature || '',
    );
  }
 
  @Post('easebuzz')
  @ApiOperation({ summary: 'Handle easebuzz operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async handleEasebuzz(
    @Body() body: any,
    @Headers('easebuzz-signature') signature: string,
  ) {
    return this.billingService.processWebhook(
      'Easebuzz',
      body,
      signature || '',
    );
  }
}
 
@Controller('api/v1/coupons')
@ApiTags('Coupons')
export class CouponsController {
  constructor(private readonly billingService: BillingService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    return { tenantId };
  }
 
  @Post('validate')
  @ApiOperation({ summary: 'Validate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async validate(@Body() body: { code: string }, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.billingService.validateCoupon(body.code, ctx.tenantId);
  }
 
  @Post('apply')
  @ApiOperation({ summary: 'Apply operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async apply(
    @Body() body: { code: string; invoiceId: string },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.billingService.applyCoupon(
      body.code,
      ctx.tenantId,
      body.invoiceId,
    );
  }
}