feat: Stripe payment integration

This commit is contained in:
Ahmed Bouhuolia
2024-09-21 16:50:22 +02:00
parent 8de8695b25
commit 7756b5b304
24 changed files with 691 additions and 102 deletions

View File

@@ -1,5 +1,6 @@
import { Service, Inject } from 'typedi';
import { Request, Response, Router, NextFunction } from 'express';
import { body, param } from 'express-validator';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import BaseController from '@/api/controllers/BaseController';
import { PaymentServicesApplication } from '@/services/PaymentServices/PaymentServicesApplication';
@@ -19,6 +20,25 @@ export class PaymentServicesController extends BaseController {
'/',
asyncMiddleware(this.getPaymentServicesSpecificInvoice.bind(this))
);
router.get('/state', this.getPaymentMethodsState.bind(this));
router.post(
'/:paymentMethodId',
[
param('paymentMethodId').exists(),
body('name').optional().isString(),
body('options.bankAccountId').optional().isNumeric(),
body('options.clearningAccountId').optional().isNumeric(),
body('options.showVisa').optional().isBoolean(),
body('options.showMasterCard').optional().isBoolean(),
body('options.showDiscover').optional().isBoolean(),
body('options.showAmer').optional().isBoolean(),
body('options.showJcb').optional().isBoolean(),
body('options.showDiners').optional().isBoolean(),
],
this.validationResult,
asyncMiddleware(this.updatePaymentMethod.bind(this))
);
return router;
}
@@ -26,7 +46,7 @@ export class PaymentServicesController extends BaseController {
* Retrieve accounts types list.
* @param {Request} req - Request.
* @param {Response} res - Response.
* @return {Response}
* @return {Promise<Response | void>}
*/
private async getPaymentServicesSpecificInvoice(
req: Request<{ invoiceId: number }>,
@@ -44,4 +64,58 @@ export class PaymentServicesController extends BaseController {
next(error);
}
}
/**
* Edits the given payment method settings.
* @param {Request} req - Request.
* @param {Response} res - Response.
* @return {Promise<Response | void>}
*/
private async updatePaymentMethod(
req: Request<{ paymentMethodId: number }>,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { paymentMethodId } = req.params;
const updatePaymentMethodDTO = this.matchedBodyData(req);
try {
await this.paymentServicesApp.editPaymentMethod(
tenantId,
paymentMethodId,
updatePaymentMethodDTO
);
return res.status(200).send({
id: paymentMethodId,
message: 'The given payment method has been updated.',
});
} catch (error) {
next(error);
}
}
/**
* Retrieves the payment state providing state.
* @param {Request} req - Request.
* @param {Response} res - Response.
* @param {NextFunction} next - Next function.
* @return {Promise<Response | void>}
*/
private async getPaymentMethodsState(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
try {
const paymentMethodsState =
await this.paymentServicesApp.getPaymentMethodsState(tenantId);
return res.status(200).send({ data: paymentMethodsState });
} catch (error) {
next(error);
}
}
}

View File

@@ -1,26 +1,29 @@
import { NextFunction, Request, Response, Router } from 'express';
import { body } from 'express-validator';
import { Service, Inject } from 'typedi';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import { StripePaymentApplication } from '@/services/StripePayment/StripePaymentApplication';
import BaseController from '../BaseController';
@Service()
export class StripeIntegrationController {
export class StripeIntegrationController extends BaseController {
@Inject()
private stripePaymentApp: StripePaymentApplication;
router() {
public router() {
const router = Router();
router.post('/account', asyncMiddleware(this.createAccount.bind(this)));
router.post(
'/account_session',
asyncMiddleware(this.createAccountSession.bind(this))
'/account_link',
[body('stripe_account_id').exists()],
this.validationResult,
asyncMiddleware(this.createAccountLink.bind(this))
);
router.post(
'/:linkId/create_checkout_session',
this.createCheckoutSession.bind(this)
);
return router;
}
@@ -65,8 +68,7 @@ export class StripeIntegrationController {
const accountId = await this.stripePaymentApp.createStripeAccount(
tenantId
);
res.status(201).json({
return res.status(201).json({
accountId,
message: 'The Stripe account has been created successfully.',
});
@@ -82,20 +84,20 @@ export class StripeIntegrationController {
* @param {NextFunction} next - The Express next middleware function.
* @returns {Promise<void>}
*/
public async createAccountSession(
public async createAccountLink(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { account } = req.body;
const { stripeAccountId } = this.matchedBodyData(req);
try {
const clientSecret = await this.stripePaymentApp.createStripeAccount(
const clientSecret = await this.stripePaymentApp.createAccountLink(
tenantId,
account
stripeAccountId
);
res.status(200).json({ clientSecret });
return res.status(200).json({ clientSecret });
} catch (error) {
next(error);
}

View File

@@ -15,7 +15,7 @@ export class StripeWebhooksController {
@Inject()
private eventPublisher: EventPublisher;
router() {
public router() {
const router = Router();
router.post(
@@ -35,7 +35,7 @@ export class StripeWebhooksController {
* @param {Response} res - The Express response object.
* @param {NextFunction} next - The Express next middleware function.
*/
public async handleWebhook(req: Request, res: Response, next: NextFunction) {
private async handleWebhook(req: Request, res: Response, next: NextFunction) {
try {
let event = req.body;
const sig = req.headers['stripe-signature'];