feat: Emit Stripe webhooks to events in the system

This commit is contained in:
Ahmed Bouhuolia
2024-09-19 10:25:13 +02:00
parent 77f628509c
commit 2ebb4595a8
6 changed files with 92 additions and 31 deletions

View File

@@ -0,0 +1,44 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { CreatePaymentReceiveStripePayment } from '../CreatePaymentReceivedStripePayment';
import { StripeCheckoutSessionCompletedEventPayload } from '@/interfaces/StripePayment';
@Service()
export class StripeWebhooksSubscriber {
@Inject()
private createPaymentReceiveStripePayment: CreatePaymentReceiveStripePayment;
/**
* Attaches the subscriber to the event dispatcher.
*/
public attach(bus) {
bus.subscribe(
events.stripeWebhooks.onCheckoutSessionCompleted,
this.handleCheckoutSessionCompleted.bind(this)
);
}
/**
* Handles the checkout session completed webhook event.
* @param {StripeCheckoutSessionCompletedEventPayload} payload -
*/
async handleCheckoutSessionCompleted({
event,
}: StripeCheckoutSessionCompletedEventPayload) {
const { metadata } = event.data.object;
const tenantId = parseInt(metadata.tenantId, 10);
const saleInvoiceId = parseInt(metadata.saleInvoiceId, 10);
// Get the amount from the event
const amount = event.data.object.amount_total;
// Convert from Stripe amount (cents) to normal amount (dollars)
const amountInDollars = amount / 100;
await this.createPaymentReceiveStripePayment.createPaymentReceived(
tenantId,
saleInvoiceId,
amountInDollars
);
}
}