Billing - REST API - new URLs
MODIFY
src/server/rest/v1/router/GlobalRouter.ts
src/server/rest/v1/router/GlobalRouter.ts
@@ -24,7 +24,6 @@ export default class GlobalRouter {
this.buildRouteAPI();
this.buildRouteUtils();
this.buildRouteDocs();
- this.buildRouteBilling();
this.buildUnknownRoute();
return this.router;
}
@@ -43,6 +42,7 @@ export default class GlobalRouter {
new TenantRouter().buildRoutes(),
new TransactionRouter().buildRoutes(),
new UserRouter().buildRoutes(),
+ new BillingRouter().buildRoutes(),
]);
}
@@ -54,10 +54,6 @@ export default class GlobalRouter {
this.router.use('/docs', new SwaggerRouter().buildRoutes());
}
- protected buildRouteBilling(): void {
- this.router.use('/docs', new BillingRouter().buildRoutes());
- }
-
protected buildUnknownRoute(): void {
this.router.use('*', (req: Request, res: Response, next: NextFunction) => {
if (!res.headersSent) {

MODIFY
src/server/rest/v1/router/api/BillingRouter.ts
src/server/rest/v1/router/api/BillingRouter.ts
+import { ServerAction, ServerRoute } from '../../../../../types/Server';
/* eslint-disable @typescript-eslint/no-misused-promises */
import express, { NextFunction, Request, Response } from 'express';
import BillingService from '../../service/BillingService';
import RouterUtils from '../RouterUtils';
-import { ServerAction } from '../../../../../types/Server';
export default class BillingRouter {
private router: express.Router;
@@ -13,32 +13,54 @@ export default class BillingRouter {
}
public buildRoutes(): express.Router {
- this.buildRouteBillingSetupPaymentMethod();
this.buildRouteBillingPaymentMethods();
+ // this.buildRouteBillingPaymentMethod(); - // No use case so far
+ // this.buildRouteBillingCreatePaymentMethod(); - // No use case so far
+ // this.buildRouteBillingUpdatePaymentMethod(); - // No use case so far
this.buildRouteBillingDeletePaymentMethod();
+ this.buildRouteBillingPaymentMethodSetup();
+ this.buildRouteBillingPaymentMethodAttach();
+ this.buildRouteBillingPaymentMethodDetach();
return this.router;
}
- protected buildRouteBillingSetupPaymentMethod(): void {
- this.router.put(`/${ServerAction.BILLING_SETUP_PAYMENT_METHOD}`, async (req: Request, res: Response, next: NextFunction) => {
- await RouterUtils.handleServerAction(BillingService.handleBillingSetupPaymentMethod.bind(this), ServerAction.BILLING_SETUP_PAYMENT_METHOD, req, res, next);
- });
- }
-
- // why in other routers we can see ServerRoute.REST_BILLING_PAYMENT_METHODS ????
- // don't get the difference
protected buildRouteBillingPaymentMethods(): void {
- this.router.get(`/${ServerAction.BILLING_PAYMENT_METHODS}`, async (req: Request, res: Response, next: NextFunction) => {
- // req.query.ID = req.params.id;
+ this.router.get(`/${ServerRoute.REST_BILLING_PAYMENT_METHODS}`, async (req: Request, res: Response, next: NextFunction) => {
+ req.body.userID = req.params.userID;
await RouterUtils.handleServerAction(BillingService.handleBillingGetPaymentMethods.bind(this), ServerAction.BILLING_PAYMENT_METHODS, req, res, next);
});
}
- // delete ou post ?
protected buildRouteBillingDeletePaymentMethod(): void {
- this.router.delete(`/${ServerAction.BILLING_DELETE_PAYMENT_METHOD}`, async (req: Request, res: Response, next: NextFunction) => {
- req.query.ID = req.params.id;
+ this.router.delete(`/${ServerRoute.REST_BILLING_PAYMENT_METHOD}`, async (req: Request, res: Response, next: NextFunction) => {
+ req.body.userID = req.params.userID;
+ req.body.paymentMethodId = req.params.paymentMethodID;
await RouterUtils.handleServerAction(BillingService.handleBillingDeletePaymentMethod.bind(this), ServerAction.BILLING_DELETE_PAYMENT_METHOD, req, res, next);
});
}
+
+ protected buildRouteBillingPaymentMethodSetup(): void {
+ this.router.post(`/${ServerRoute.REST_BILLING_PAYMENT_METHOD_SETUP}`, async (req: Request, res: Response, next: NextFunction) => {
+ // STRIPE prerequisite - ask for a setup intent first!
+ req.body.userID = req.params.userID;
+ await RouterUtils.handleServerAction(BillingService.handleBillingSetupPaymentMethod.bind(this), ServerAction.BILLING_SETUP_PAYMENT_METHOD, req, res, next);
+ });
+ }
+
+ protected buildRouteBillingPaymentMethodAttach(): void {
+ this.router.post(`/${ServerRoute.REST_BILLING_PAYMENT_METHOD_ATTACH}`, async (req: Request, res: Response, next: NextFunction) => {
+ // Creates a new payment method and attach it to the user as its default
+ req.body.userID = req.params.userID;
+ await RouterUtils.handleServerAction(BillingService.handleBillingSetupPaymentMethod.bind(this), ServerAction.BILLING_SETUP_PAYMENT_METHOD, req, res, next);
+ });
+ }
+
+ protected buildRouteBillingPaymentMethodDetach(): void {
+ this.router.post(`/${ServerRoute.REST_BILLING_PAYMENT_METHOD_DETACH}`, async (req: Request, res: Response, next: NextFunction) => {
+ // Detach a payment method from the user
+ req.body.userID = req.params.userID;
+ await RouterUtils.handleServerAction(BillingService.handleBillingSetupPaymentMethod.bind(this), ServerAction.BILLING_SETUP_PAYMENT_METHOD, req, res, next);
+ });
+ }
+
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -497,6 +497,12 @@ export enum ServerRoute {
REST_TENANTS = 'tenants',
- // shouldn't we have this rest here ?? for the route building
- // REST_BILLING_PAYMENT_METHODS = 'billing/paymentmethods'
+ // URLs for CRUD operations on PAYMENT METHODS
+ REST_BILLING_PAYMENT_METHODS = 'billing/users/:userID/payment-methods',
+ REST_BILLING_PAYMENT_METHOD = 'billing/users/:userID/payment-methods/:paymentMethodID',
+
+ // URLs for Non-CRUD Operations on PAYMENT METHODS
+ REST_BILLING_PAYMENT_METHOD_SETUP = 'billing/users/:userID/payment-methods/setup',
+ REST_BILLING_PAYMENT_METHOD_ATTACH = 'billing/users/:userID/payment-methods/attach',
+ REST_BILLING_PAYMENT_METHOD_DETACH = 'billing/users/:userID/payment-methods/detach',
}

Billing - REST API - new URLs