Merge branch 'dev' into staging

This commit is contained in:
Maxime Lalo 2024-04-05 15:34:17 +02:00
commit 4eac51bf65
2 changed files with 32 additions and 10 deletions

View File

@ -19,19 +19,19 @@ export default class StripeController extends ApiController {
* @description Create a new checkout session * @description Create a new checkout session
*/ */
@Post("/api/v1/admin/stripe", [authHandler]) @Post("/api/v1/admin/stripe", [authHandler])
protected async post(req: Request, response: Response) { protected async createStripeSubscriptionCheckout(req: Request, response: Response) {
try { try {
const officeId: string = req.body.user.office_Id; const officeId: string = req.body.user.office_Id;
//add office id to request body //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<Subscription>(req.body, { strategy: "excludeAll" }); const subscriptionEntity = Subscription.hydrate<Subscription>(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); this.httpCreated(response, stripeSession);
} catch (error) { } catch (error) {
@ -41,7 +41,7 @@ export default class StripeController extends ApiController {
} }
@Get("/api/v1/admin/stripe/:uid", [authHandler]) @Get("/api/v1/admin/stripe/:uid", [authHandler])
protected async get(req: Request, response: Response) { protected async getClientPortalSession(req: Request, response: Response) {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!uid) { if (!uid) {
@ -57,4 +57,21 @@ export default class StripeController extends ApiController {
return; return;
} }
} }
}
@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;
}
}
}

View File

@ -26,7 +26,7 @@ export default class StripeService {
line_items: [ line_items: [
{ {
price: priceId, price: priceId,
quantity: subscription.nb_seats, quantity: subscription.type === "STANDARD" ? subscription.nb_seats : 1,
}, },
], ],
success_url: this.variables.APP_HOST + "/subscription/success", success_url: this.variables.APP_HOST + "/subscription/success",
@ -39,10 +39,15 @@ export default class StripeService {
public async createClientPortalSession(subscriptionId: string) { public async createClientPortalSession(subscriptionId: string) {
const subscription = await this.client.subscriptions.retrieve(subscriptionId); const subscription = await this.client.subscriptions.retrieve(subscriptionId);
return this.client.billingPortal.sessions.create({ return this.client.billingPortal.sessions.create({
customer: subscription.customer as string, customer: subscription.customer as string,
return_url: this.variables.APP_HOST + "/subscription/manage", 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);
}
} }