mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
feat: inactive associated Stripe payment link on invoice deleting
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import { StripePaymentService } from './StripePaymentService';
|
||||
import { Knex } from 'knex';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import events from '@/subscribers/events';
|
||||
|
||||
@Service()
|
||||
export class DeleteStripePaymentLinkInvoice {
|
||||
@@ -11,27 +13,53 @@ export class DeleteStripePaymentLinkInvoice {
|
||||
@Inject()
|
||||
private stripePayment: StripePaymentService;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
/**
|
||||
* Deletes the Stripe payment link associates to the given sale invoice.
|
||||
* @param {number} tenantId
|
||||
* @param {number} invoiceId
|
||||
* @param {number} tenantId -
|
||||
* @param {number} invoiceId -
|
||||
* @param {Knex.Transaction} knex -
|
||||
*/
|
||||
async deletePaymentLink(
|
||||
async deleteInvoicePaymentLink(
|
||||
tenantId: number,
|
||||
invoiceId: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||
const invoice = await SaleInvoice.query().findById(invoiceId);
|
||||
|
||||
const stripeAcocunt = { stripeAccount: 'acct_1Px3dSPjeOqFxnPw' };
|
||||
const invoice = await SaleInvoice.query(trx)
|
||||
.findById(invoiceId)
|
||||
.withGraphFetched('paymentMethods.paymentIntegration')
|
||||
.throwIfNotFound();
|
||||
|
||||
// It will be only one Stripe payment method associated to the invoice.
|
||||
const stripePaymentMethod = invoice.paymentMethods?.find(
|
||||
(method) => method.paymentIntegration?.service === 'Stripe'
|
||||
);
|
||||
const stripeAccountId = stripePaymentMethod?.paymentIntegration?.accountId;
|
||||
const paymentIntegrationId = stripePaymentMethod?.paymentIntegration?.id;
|
||||
|
||||
if (invoice.stripePlinkId && stripeAccountId) {
|
||||
const stripeAcocunt = { stripeAccount: stripeAccountId };
|
||||
const stripePlinkId = invoice.stripePlinkId;
|
||||
|
||||
if (invoice.stripePlinkId) {
|
||||
await this.stripePayment.stripe.paymentLinks.update(
|
||||
invoice.stripePlinkId,
|
||||
stripePlinkId,
|
||||
{ active: false },
|
||||
stripeAcocunt
|
||||
);
|
||||
// Triggers `onStripePaymentLinkInactivated` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.stripeIntegration.onPaymentLinkInactivated,
|
||||
{
|
||||
tenantId,
|
||||
saleInvoiceId: invoiceId,
|
||||
paymentIntegrationId,
|
||||
stripePlinkId,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject } from 'typedi';
|
||||
import { CreateStripeAccountService } from '@/api/controllers/StripeIntegration/CreateStripeAccountService';
|
||||
import { CreateStripeAccountDTO } from '@/api/controllers/StripeIntegration/types';
|
||||
import { SaleInvoiceStripePaymentLink } from './SaleInvoiceStripePaymentLink';
|
||||
import { DeleteStripePaymentLinkInvoice } from './DeleteStripePaymentLinkInvoice';
|
||||
|
||||
export class StripePaymentApplication {
|
||||
@Inject()
|
||||
private createStripeAccountService: CreateStripeAccountService;
|
||||
|
||||
@Inject()
|
||||
private saleInvoiceStripePaymentLinkService: SaleInvoiceStripePaymentLink;
|
||||
|
||||
@Inject()
|
||||
private deleteStripePaymentLinkInvoice: DeleteStripePaymentLinkInvoice;
|
||||
|
||||
/**
|
||||
* Creates a new Stripe account for Bigcapital.
|
||||
* @param {number} tenantId
|
||||
* @param {number} createStripeAccountDTO
|
||||
*/
|
||||
public createStripeAccount(
|
||||
tenantId: number,
|
||||
createStripeAccountDTO: CreateStripeAccountDTO
|
||||
) {
|
||||
return this.createStripeAccountService.createStripeAccount(
|
||||
tenantId,
|
||||
createStripeAccountDTO
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Stripe payment link for the given sale invoice.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} stripeIntegrationId - Stripe integration id.
|
||||
* @param {ISaleInvoice} saleInvoice - Sale invoice id.
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
public createSaleInvoicePaymentLink(
|
||||
tenantId: number,
|
||||
stripeIntegrationId: number,
|
||||
invoiceId: number
|
||||
) {
|
||||
return this.saleInvoiceStripePaymentLinkService.createPaymentLink(
|
||||
tenantId,
|
||||
stripeIntegrationId,
|
||||
invoiceId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the Stripe payment link associated with the given sale invoice.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} invoiceId - Sale invoice id.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteInvoicePaymentLink(
|
||||
tenantId: number,
|
||||
invoiceId: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
return this.deleteStripePaymentLinkInvoice.deleteInvoicePaymentLink(
|
||||
tenantId,
|
||||
invoiceId,
|
||||
trx
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
|
||||
import {
|
||||
ISaleInvoiceDeletedPayload,
|
||||
PaymentIntegrationTransactionLinkDeleteEventPayload,
|
||||
PaymentIntegrationTransactionLinkEventPayload,
|
||||
} from '@/interfaces';
|
||||
import { SaleInvoiceStripePaymentLink } from '../SaleInvoiceStripePaymentLink';
|
||||
@@ -25,10 +25,10 @@ export class CreatePaymentLinkOnInvoiceCreated extends EventSubscriber {
|
||||
events.paymentIntegrationLink.onPaymentIntegrationLink,
|
||||
this.handleCreatePaymentLinkOnIntegrationLink
|
||||
);
|
||||
// bus.subscribe(
|
||||
// events.saleInvoice.onDeleted,
|
||||
// this.handleDeletePaymentLinkOnInvoiceDeleted
|
||||
// );
|
||||
bus.subscribe(
|
||||
events.paymentIntegrationLink.onPaymentIntegrationDeleteLink,
|
||||
this.handleDeletePaymentLinkOnIntegrationLinkDelete
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,13 +59,15 @@ export class CreatePaymentLinkOnInvoiceCreated extends EventSubscriber {
|
||||
* Deletes the Stripe payment link once the associated invoice deleted.
|
||||
* @param {ISaleInvoiceDeletedPayload}
|
||||
*/
|
||||
private handleDeletePaymentLinkOnInvoiceDeleted = async ({
|
||||
saleInvoiceId,
|
||||
private handleDeletePaymentLinkOnIntegrationLinkDelete = async ({
|
||||
oldSaleInvoiceId,
|
||||
tenantId,
|
||||
}: ISaleInvoiceDeletedPayload) => {
|
||||
await this.deleteStripePaymentLinkInvoice.deletePaymentLink(
|
||||
trx,
|
||||
}: PaymentIntegrationTransactionLinkDeleteEventPayload) => {
|
||||
await this.deleteStripePaymentLinkInvoice.deleteInvoicePaymentLink(
|
||||
tenantId,
|
||||
saleInvoiceId
|
||||
oldSaleInvoiceId,
|
||||
trx
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user