mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
feat: Emit Stripe webhooks to events in the system
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user