feat: listen to stripe account updated webhook

This commit is contained in:
Ahmed Bouhuolia
2024-09-21 23:59:54 +02:00
parent ad74007d58
commit e04f5d26a3
7 changed files with 90 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ import { initalizeTenantServices } from '@/api/middleware/TenantDependencyInject
@Service()
export class GetInvoicePaymentLinkMetadata {
@Inject()
tenancy: HasTenancyService;
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
@@ -23,12 +23,9 @@ export class GetInvoicePaymentLinkMetadata {
async getInvoicePaymentLinkMeta(linkId: string) {
const paymentLink = await PaymentLink.query()
.findOne('linkId', linkId)
.where('resourceType', 'SaleInvoice')
.throwIfNotFound();
//
if (paymentLink.resourceType !== 'SaleInvoice') {
throw new ServiceError('');
}
// Validate the expiry at date.
if (paymentLink.expiryAt) {
const currentDate = moment();

View File

@@ -82,7 +82,7 @@ class GetInvoicePaymentLinkEntryMetaTransformer extends ItemEntryTransformer {
];
};
itemName(entry) {
public itemName(entry) {
return entry.item.name;
}

View File

@@ -1,10 +1,18 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { CreatePaymentReceiveStripePayment } from '../CreatePaymentReceivedStripePayment';
import { StripeCheckoutSessionCompletedEventPayload } from '@/interfaces/StripePayment';
import {
StripeCheckoutSessionCompletedEventPayload,
StripeWebhookEventPayload,
} from '@/interfaces/StripePayment';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Tenant } from '@/system/models';
@Service()
export class StripeWebhooksSubscriber {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private createPaymentReceiveStripePayment: CreatePaymentReceiveStripePayment;
@@ -16,6 +24,10 @@ export class StripeWebhooksSubscriber {
events.stripeWebhooks.onCheckoutSessionCompleted,
this.handleCheckoutSessionCompleted.bind(this)
);
bus.subscribe(
events.stripeWebhooks.onAccountUpdated,
this.handleAccountUpdated.bind(this)
);
}
/**
@@ -35,10 +47,38 @@ export class StripeWebhooksSubscriber {
// Convert from Stripe amount (cents) to normal amount (dollars)
const amountInDollars = amount / 100;
// Creates a new payment received transaction.
await this.createPaymentReceiveStripePayment.createPaymentReceived(
tenantId,
saleInvoiceId,
amountInDollars
);
}
/**
* Handles the account updated.
* @param {StripeWebhookEventPayload}
*/
async handleAccountUpdated({ event }: StripeWebhookEventPayload) {
const { metadata } = event.data.object;
const account = event.data.object;
const tenantId = parseInt(metadata.tenantId, 10);
if (!metadata?.paymentIntegrationId || !metadata.tenantId) return;
// Find the tenant or throw not found error.
await Tenant.query().findById(tenantId).throwIfNotFound();
// Check if the account capabilities are active
if (account.capabilities.card_payments === 'active') {
const { PaymentIntegration } = this.tenancy.models(tenantId);
// Marks the payment method integration as active.
await PaymentIntegration.query()
.findById(metadata?.paymentIntegrationId)
.patch({
active: true,
});
}
}
}