subscription.controller.ts117 lines, 2.9 KB
| 1 | import { NextFunction, Request, Response } from "express"; |
| 2 | import { StatusCodes } from "http-status-codes"; |
| 3 | import { SubscriptionService } from "../services/subscription.service"; |
| 4 | import { successResponse } from "../utils/response"; |
| 5 | import Stripe from "stripe"; |
| 6 | import { AppError } from "../utils/errors"; |
| 7 | |
| 8 | export class SubscriptionController { |
| 9 | static async getPlans(req: Request, res: Response, next: NextFunction) { |
| 10 | try { |
| 11 | const plans = await SubscriptionService.getPlans(); |
| 12 | res.json(successResponse(plans)); |
| 13 | } catch (error) { |
| 14 | next(error); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | static async getUserSubscription( |
| 19 | req: Request, |
| 20 | res: Response, |
| 21 | next: NextFunction |
| 22 | ) { |
| 23 | try { |
| 24 | const userId = req.user?.userId; |
| 25 | |
| 26 | const subscription = await SubscriptionService.getUserSubscription( |
| 27 | userId! |
| 28 | ); |
| 29 | |
| 30 | const response = subscription |
| 31 | ? { isSubscribed: true, subscription } |
| 32 | : { isSubscribed: false }; |
| 33 | |
| 34 | res.json(successResponse(response)); |
| 35 | } catch (error) { |
| 36 | next(error); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static async createCheckoutSession( |
| 41 | req: Request, |
| 42 | res: Response, |
| 43 | next: NextFunction |
| 44 | ) { |
| 45 | try { |
| 46 | const userId = req.user?.userId; |
| 47 | const { planId } = req.body; |
| 48 | |
| 49 | if (!planId) { |
| 50 | throw new AppError(StatusCodes.BAD_REQUEST, "Plan ID is required"); |
| 51 | } |
| 52 | |
| 53 | const session = await SubscriptionService.createCheckoutSession( |
| 54 | userId!, |
| 55 | planId |
| 56 | ); |
| 57 | res.json(successResponse(session)); |
| 58 | } catch (error) { |
| 59 | next(error); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static async cancelSubscription( |
| 64 | req: Request, |
| 65 | res: Response, |
| 66 | next: NextFunction |
| 67 | ) { |
| 68 | try { |
| 69 | const userId = req.user?.userId; |
| 70 | const result = await SubscriptionService.cancelSubscription(userId!); |
| 71 | res.json(successResponse(result)); |
| 72 | } catch (error) { |
| 73 | next(error); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static async handleWebhook(req: Request, res: Response, next: NextFunction) { |
| 78 | const sig = req.headers["stripe-signature"] as string; |
| 79 | const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET; |
| 80 | |
| 81 | if (!endpointSecret) { |
| 82 | return next( |
| 83 | new AppError( |
| 84 | StatusCodes.INTERNAL_SERVER_ERROR, |
| 85 | "Webhook secret is not configured" |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | let event: Stripe.Event; |
| 91 | |
| 92 | try { |
| 93 | // Important: req.body must be a raw buffer for Stripe signature verification |
| 94 | // This is handled by the express.raw middleware applied in index.ts |
| 95 | const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); |
| 96 | event = stripe.webhooks.constructEvent( |
| 97 | req.body, // This is the raw request body Buffer |
| 98 | sig, |
| 99 | endpointSecret |
| 100 | ); |
| 101 | } catch (err) { |
| 102 | return next( |
| 103 | new AppError( |
| 104 | StatusCodes.BAD_REQUEST, |
| 105 | `Webhook Error: ${(err as Error).message}` |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | try { |
| 111 | const result = await SubscriptionService.handleWebhook(event); |
| 112 | res.json(successResponse(result)); |
| 113 | } catch (error) { |
| 114 | next(error); |
| 115 | } |
| 116 | } |
| 117 | } |