feat: integrate Stripe payment to invoices

This commit is contained in:
Ahmed Bouhuolia
2024-09-18 19:24:01 +02:00
parent df706d2573
commit 4665f529e6
24 changed files with 540 additions and 80 deletions

View File

@@ -0,0 +1,47 @@
import { Service, Inject } from 'typedi';
import { Request, Response, Router, NextFunction } from 'express';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import BaseController from '@/api/controllers/BaseController';
import { PaymentServicesApplication } from '@/services/PaymentServices/PaymentServicesApplication';
@Service()
export class PaymentServicesController extends BaseController {
@Inject()
private paymentServicesApp: PaymentServicesApplication;
/**
* Router constructor.
*/
router() {
const router = Router();
router.get(
'/',
asyncMiddleware(this.getPaymentServicesSpecificInvoice.bind(this))
);
return router;
}
/**
* Retrieve accounts types list.
* @param {Request} req - Request.
* @param {Response} res - Response.
* @return {Response}
*/
private async getPaymentServicesSpecificInvoice(
req: Request<{ invoiceId: number }>,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
try {
const paymentServices =
await this.paymentServicesApp.getPaymentServicesForInvoice(tenantId);
return res.status(200).send({ paymentServices });
} catch (error) {
next(error);
}
}
}

View File

@@ -258,6 +258,11 @@ export default class SaleInvoicesController extends BaseController {
// Pdf template id.
check('pdf_template_id').optional({ nullable: true }).isNumeric().toInt(),
// Payment methods.
check('payment_methods').optional({ nullable: true }).isArray({ min: 1 }),
check('payment_methods.*.payment_integration_id').exists(),
check('payment_methods.*.enable').exists().isBoolean(),
];
}

View File

@@ -0,0 +1,55 @@
import { StripePaymentService } from '@/services/StripePayment/StripePaymentService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import { CreateStripeAccountDTO } from './types';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
@Service()
export class CreateStripeAccountService {
@Inject()
private stripePaymentService: StripePaymentService;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
/**
* Creates a new Stripe account.
* @param {number} tenantId
* @param {CreateStripeAccountDTO} stripeAccountDTO
* @returns {Promise<string>}
*/
async createStripeAccount(
tenantId: number,
stripeAccountDTO?: CreateStripeAccountDTO
): Promise<string> {
const { PaymentIntegration } = this.tenancy.models(tenantId);
const stripeAccount = await this.stripePaymentService.createAccount();
const stripeAccountId = stripeAccount.id;
const parsedStripeAccountDTO = {
...stripeAccountDTO,
name: 'Stripe',
};
// Stores the details of the Stripe account.
await PaymentIntegration.query().insert({
name: parsedStripeAccountDTO.name,
accountId: stripeAccountId,
enable: false,
service: 'Stripe',
});
// Triggers `onStripeIntegrationAccountCreated` event.
await this.eventPublisher.emitAsync(
events.stripeIntegration.onAccountCreated,
{
tenantId,
stripeAccountDTO,
stripeAccountId,
}
);
return stripeAccountId;
}
}

View File

@@ -0,0 +1,24 @@
import { Service, Inject } from 'typedi';
import { CreateStripeAccountService } from './CreateStripeAccountService';
import { CreateStripeAccountDTO } from './types';
@Service()
export class StripeIntegrationApplication {
@Inject()
private createStripeAccountService: CreateStripeAccountService;
/**
* Creates a new Stripe account for the tenant.
* @param {TenantContext} tenantContext - The tenant context.
* @param {string} label - The label for the Stripe account.
* @returns {Promise<string>} The ID of the created Stripe account.
*/
public async createStripeAccount(
tenantId: number,
stripeAccountDTO?: CreateStripeAccountDTO
): Promise<string> {
return this.createStripeAccountService.createStripeAccount(
tenantId,
stripeAccountDTO
);
}
}

View File

@@ -2,12 +2,16 @@ import { NextFunction, Request, Response, Router } from 'express';
import { Service, Inject } from 'typedi';
import { StripePaymentService } from '@/services/StripePayment/StripePaymentService';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import { StripeIntegrationApplication } from './StripeIntegrationApplication';
@Service()
export class StripeIntegrationController {
@Inject()
private stripePaymentService: StripePaymentService;
@Inject()
private stripeIntegrationApp: StripeIntegrationApplication;
router() {
const router = Router();
@@ -20,9 +24,19 @@ export class StripeIntegrationController {
}
public async createAccount(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
try {
const accountId = await this.stripePaymentService.createAccount();
res.status(201).json({ accountId });
const accountId = await this.stripeIntegrationApp.createStripeAccount(
tenantId
);
res
.status(201)
.json({
accountId,
message: 'The Stripe account has been created successfully.',
});
} catch (error) {
next(error);
}

View File

@@ -0,0 +1,6 @@
export interface CreateStripeAccountDTO {
name: string;
}