Get customer from subscription

This commit is contained in:
Vins 2024-04-05 11:48:58 +02:00
parent 254b5cb374
commit 755cac0bc6
2 changed files with 30 additions and 8 deletions

View File

@ -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<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);
} 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;
}
}
}
@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

@ -45,4 +45,9 @@ export default class StripeService {
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);
}
}