diff --git a/src/app/api/admin/StripeController.ts b/src/app/api/admin/StripeController.ts index 84550974..cfbf0ad1 100644 --- a/src/app/api/admin/StripeController.ts +++ b/src/app/api/admin/StripeController.ts @@ -19,19 +19,19 @@ export default class StripeController extends ApiController { * @description Create a new checkout session */ @Post("/api/v1/admin/stripe", [authHandler]) - protected async post(req: Request, response: Response) { - try { + protected async createStripeSubscriptionCheckout(req: Request, response: Response) { + try { const officeId: string = req.body.user.office_Id; //add office id to request body - req.body.office = {uid: officeId}; + req.body.office = { uid: officeId }; - //init Subscription resource with request body values + //init Subscription resource with request body values const subscriptionEntity = Subscription.hydrate(req.body, { strategy: "excludeAll" }); - await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); + await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); - const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); + const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); this.httpCreated(response, stripeSession); } catch (error) { @@ -41,7 +41,7 @@ export default class StripeController extends ApiController { } @Get("/api/v1/admin/stripe/:uid", [authHandler]) - protected async get(req: Request, response: Response) { + protected async getClientPortalSession(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { @@ -57,4 +57,21 @@ export default class StripeController extends ApiController { return; } } -} \ No newline at end of file + + @Get("/api/v1/admin/stripe/:uid/customer") + protected async getCustomerBySubscription(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const customer = await this.stripeService.getCustomerBySubscription(uid); + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/services/common/StripeService/StripeService.ts b/src/services/common/StripeService/StripeService.ts index 96151a96..a6163dfa 100644 --- a/src/services/common/StripeService/StripeService.ts +++ b/src/services/common/StripeService/StripeService.ts @@ -26,7 +26,7 @@ export default class StripeService { line_items: [ { price: priceId, - quantity: subscription.nb_seats, + quantity: subscription.type === "STANDARD" ? subscription.nb_seats : 1, }, ], success_url: this.variables.APP_HOST + "/subscription/success", @@ -39,10 +39,15 @@ export default class StripeService { public async createClientPortalSession(subscriptionId: string) { const subscription = await this.client.subscriptions.retrieve(subscriptionId); - + return this.client.billingPortal.sessions.create({ customer: subscription.customer as string, return_url: this.variables.APP_HOST + "/subscription/manage", }); } + + public async getCustomerBySubscription(subscriptionId: string) { + const subscription = await this.client.subscriptions.retrieve(subscriptionId); + return this.client.customers.retrieve(subscription.customer as string); + } }