feat: Clean up payment links endpoints

This commit is contained in:
Ahmed Bouhuolia
2024-09-25 19:37:09 +02:00
parent 323b95de7b
commit 1cc71eb368
14 changed files with 183 additions and 132 deletions

View File

@@ -1,20 +1,21 @@
import config from '@/config';
import { StripePaymentService } from './StripePaymentService';
import { Inject, Service } from 'typedi';
import { StripePaymentService } from '../StripePayment/StripePaymentService';
import HasTenancyService from '../Tenancy/TenancyService';
import { ISaleInvoice } from '@/interfaces';
import { Inject, Service } from 'typedi';
import { StripeInvoiceCheckoutSessionPOJO } from '@/interfaces/StripePayment';
import { PaymentLink } from '@/system/models';
import { initializeTenantSettings } from '@/api/middleware/SettingsMiddleware';
import config from '@/config';
const origin = 'http://localhost';
@Service()
export class CreateInvoiceCheckoutSession {
@Inject()
private stripePaymentService: StripePaymentService;
private tenancy: HasTenancyService;
@Inject()
private tenancy: HasTenancyService;
private stripePaymentService: StripePaymentService;
/**
* Creates a new Stripe checkout session from the given sale invoice.
@@ -23,17 +24,18 @@ export class CreateInvoiceCheckoutSession {
* @returns {Promise<StripeInvoiceCheckoutSessionPOJO>}
*/
async createInvoiceCheckoutSession(
tenantId: number,
publicPaymentLinkId: number
publicPaymentLinkId: string
): Promise<StripeInvoiceCheckoutSessionPOJO> {
const { SaleInvoice } = this.tenancy.models(tenantId);
// Retrieves the payment link from the given id.
const paymentLink = await PaymentLink.query()
.findOne('linkId', publicPaymentLinkId)
.where('resourceType', 'SaleInvoice')
.throwIfNotFound();
const tenantId = paymentLink.tenantId;
await initializeTenantSettings(tenantId);
const { SaleInvoice } = this.tenancy.models(tenantId);
// Retrieves the invoice from associated payment link.
const invoice = await SaleInvoice.query()
.findById(paymentLink.resourceId)

View File

@@ -4,7 +4,7 @@ import { ServiceError } from '@/exceptions';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { PaymentLink } from '@/system/models';
import { GetInvoicePaymentLinkMetaTransformer } from './GetInvoicePaymentLinkTransformer';
import { GetInvoicePaymentLinkMetaTransformer } from '../Sales/Invoices/GetInvoicePaymentLinkTransformer';
import { initalizeTenantServices } from '@/api/middleware/TenantDependencyInjection';
@Service()

View File

@@ -0,0 +1,37 @@
import { Inject, Service } from 'typedi';
import { GetInvoicePaymentLinkMetadata } from './GetInvoicePaymentLinkMetadata';
import { CreateInvoiceCheckoutSession } from './CreateInvoiceCheckoutSession';
import { StripeInvoiceCheckoutSessionPOJO } from '@/interfaces/StripePayment';
@Service()
export class PaymentLinksApplication {
@Inject()
private getInvoicePaymentLinkMetadataService: GetInvoicePaymentLinkMetadata;
@Inject()
private createInvoiceCheckoutSessionService: CreateInvoiceCheckoutSession;
/**
* Retrieves the invoice payment link.
* @param {string} paymentLinkId
* @returns {}
*/
public getInvoicePaymentLink(paymentLinkId: string) {
return this.getInvoicePaymentLinkMetadataService.getInvoicePaymentLinkMeta(
paymentLinkId
);
}
/**
* Create the invoice payment checkout session from the given payment link id.
* @param {string} paymentLinkId - Payment link id.
* @returns {Promise<StripeInvoiceCheckoutSessionPOJO>}
*/
public createInvoicePaymentCheckoutSession(
paymentLinkId: string
): Promise<StripeInvoiceCheckoutSessionPOJO> {
return this.createInvoiceCheckoutSessionService.createInvoiceCheckoutSession(
paymentLinkId
);
}
}

View File

@@ -1,6 +1,9 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { GetSaleInvoice } from '../Sales/Invoices/GetSaleInvoice';
import { CreatePaymentReceived } from '../Sales/PaymentReceived/CreatePaymentReceived';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
@Service()
export class CreatePaymentReceiveStripePayment {
@@ -10,28 +13,52 @@ export class CreatePaymentReceiveStripePayment {
@Inject()
private createPaymentReceivedService: CreatePaymentReceived;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
*
* @param {number} tenantId
* @param {number} saleInvoiceId
* @param {number} paidAmount
* @param {number} tenantId
* @param {number} saleInvoiceId
* @param {number} paidAmount
*/
async createPaymentReceived(
tenantId: number,
saleInvoiceId: number,
paidAmount: number
) {
const invoice = await this.getSaleInvoiceService.getSaleInvoice(
tenantId,
saleInvoiceId
);
await this.createPaymentReceivedService.createPaymentReceived(tenantId, {
customerId: invoice.customerId,
paymentDate: new Date(),
amount: paidAmount,
depositAccountId: 1002,
statement: '',
entries: [{ invoiceId: saleInvoiceId, paymentAmount: paidAmount }],
const { accountRepository } = this.tenancy.repositories(tenantId);
// Create a payment received transaction under UOW envirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Finds or creates a new stripe payment clearing account (current asset).
const stripeClearingAccount =
accountRepository.findOrCreateStripeClearing({}, trx);
// Retrieves the given invoice to create payment transaction associated to it.
const invoice = await this.getSaleInvoiceService.getSaleInvoice(
tenantId,
saleInvoiceId
);
// Create a payment received transaction associated to the given invoice.
await this.createPaymentReceivedService.createPaymentReceived(
tenantId,
{
customerId: invoice.customerId,
paymentDate: new Date(),
amount: paidAmount,
exchangeRate: 1,
referenceNo: '',
statement: '',
depositAccountId: stripeClearingAccount.id,
entries: [{ invoiceId: saleInvoiceId, paymentAmount: paidAmount }],
},
{},
trx
);
});
}
}

View File

@@ -1,6 +1,4 @@
import { Inject } from 'typedi';
import { CreateInvoiceCheckoutSession } from './CreateInvoiceCheckoutSession';
import { StripeInvoiceCheckoutSessionPOJO } from '@/interfaces/StripePayment';
import { CreateStripeAccountService } from './CreateStripeAccountService';
import { CreateStripeAccountLinkService } from './CreateStripeAccountLink';
import { CreateStripeAccountDTO } from './types';
@@ -14,9 +12,6 @@ export class StripePaymentApplication {
@Inject()
private createStripeAccountLinkService: CreateStripeAccountLinkService;
@Inject()
private createInvoiceCheckoutSessionService: CreateInvoiceCheckoutSession;
@Inject()
private exchangeStripeOAuthTokenService: ExchangeStripeOAuthTokenService;
@@ -51,22 +46,6 @@ export class StripePaymentApplication {
);
}
/**
* Creates the Stripe checkout session from the given sale invoice.
* @param {number} tenantId
* @param {string} paymentLinkId
* @returns {Promise<StripeInvoiceCheckoutSessionPOJO>}
*/
public createSaleInvoiceCheckoutSession(
tenantId: number,
paymentLinkId: number
): Promise<StripeInvoiceCheckoutSessionPOJO> {
return this.createInvoiceCheckoutSessionService.createInvoiceCheckoutSession(
tenantId,
paymentLinkId
);
}
/**
* Retrieves Stripe OAuth2 connect link.
* @returns {string}

View File

@@ -7,6 +7,7 @@ import {
} from '@/interfaces/StripePayment';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Tenant } from '@/system/models';
import { initalizeTenantServices } from '@/api/middleware/TenantDependencyInjection';
@Service()
export class StripeWebhooksSubscriber {
@@ -41,6 +42,8 @@ export class StripeWebhooksSubscriber {
const tenantId = parseInt(metadata.tenantId, 10);
const saleInvoiceId = parseInt(metadata.saleInvoiceId, 10);
await initalizeTenantServices(tenantId);
// Get the amount from the event
const amount = event.data.object.amount_total;