mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 23:30:32 +00:00
feat: Emit Stripe webhooks to events in the system
This commit is contained in:
@@ -23,6 +23,13 @@ export class StripeIntegrationController {
|
|||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Stripe account.
|
||||||
|
* @param {Request} req - The Express request object.
|
||||||
|
* @param {Response} res - The Express response object.
|
||||||
|
* @param {NextFunction} next - The Express next middleware function.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
public async createAccount(req: Request, res: Response, next: NextFunction) {
|
public async createAccount(req: Request, res: Response, next: NextFunction) {
|
||||||
const { tenantId } = req;
|
const { tenantId } = req;
|
||||||
|
|
||||||
@@ -31,17 +38,22 @@ export class StripeIntegrationController {
|
|||||||
tenantId
|
tenantId
|
||||||
);
|
);
|
||||||
|
|
||||||
res
|
res.status(201).json({
|
||||||
.status(201)
|
accountId,
|
||||||
.json({
|
message: 'The Stripe account has been created successfully.',
|
||||||
accountId,
|
});
|
||||||
message: 'The Stripe account has been created successfully.',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Stripe account session.
|
||||||
|
* @param {Request} req - The Express request object.
|
||||||
|
* @param {Response} res - The Express response object.
|
||||||
|
* @param {NextFunction} next - The Express next middleware function.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
public async createAccountSession(
|
public async createAccountSession(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { StripePaymentService } from '@/services/StripePayment/StripePaymentService';
|
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
import { NextFunction, Request, Response, Router } from 'express';
|
||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import config from '@/config';
|
|
||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import { SaleInvoiceStripePaymentLink } from '@/services/StripePayment/SaleInvoiceStripePaymentLink';
|
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||||
import { CreatePaymentReceiveStripePayment } from '@/services/StripePayment/CreatePaymentReceivedStripePayment';
|
import { StripeCheckoutSessionCompletedEventPayload } from '@/interfaces/StripePayment';
|
||||||
|
import { StripePaymentService } from '@/services/StripePayment/StripePaymentService';
|
||||||
|
import events from '@/subscribers/events';
|
||||||
|
import config from '@/config';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class StripeWebhooksController {
|
export class StripeWebhooksController {
|
||||||
@@ -12,7 +13,7 @@ export class StripeWebhooksController {
|
|||||||
private stripePaymentService: StripePaymentService;
|
private stripePaymentService: StripePaymentService;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
private createPaymentReceiveStripePayment: CreatePaymentReceiveStripePayment;
|
private eventPublisher: EventPublisher;
|
||||||
|
|
||||||
router() {
|
router() {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
@@ -26,10 +27,13 @@ export class StripeWebhooksController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Handles incoming Stripe webhook events.
|
||||||
|
* Verifies the webhook signature, processes the event based on its type,
|
||||||
|
* and triggers appropriate actions or events in the system.
|
||||||
*
|
*
|
||||||
* @param req
|
* @param {Request} req - The Express request object containing the webhook payload.
|
||||||
* @param res
|
* @param {Response} res - The Express response object.
|
||||||
* @param next
|
* @param {NextFunction} next - The Express next middleware function.
|
||||||
*/
|
*/
|
||||||
public async handleWebhook(req: Request, res: Response, next: NextFunction) {
|
public async handleWebhook(req: Request, res: Response, next: NextFunction) {
|
||||||
try {
|
try {
|
||||||
@@ -50,20 +54,12 @@ export class StripeWebhooksController {
|
|||||||
// Handle the event based on its type
|
// Handle the event based on its type
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'checkout.session.completed':
|
case 'checkout.session.completed':
|
||||||
const { metadata } = event.data.object;
|
// Triggers `onStripeCheckoutSessionCompleted` event.
|
||||||
const tenantId = parseInt(metadata.tenantId, 10);
|
this.eventPublisher.emitAsync(
|
||||||
const saleInvoiceId = parseInt(metadata.saleInvoiceId, 10);
|
events.stripeWebhooks.onCheckoutSessionCompleted,
|
||||||
|
{
|
||||||
// Get the amount from the event
|
event,
|
||||||
const amount = event.data.object.amount_total;
|
} as StripeCheckoutSessionCompletedEventPayload
|
||||||
|
|
||||||
// Convert from Stripe amount (cents) to normal amount (dollars)
|
|
||||||
const amountInDollars = amount / 100;
|
|
||||||
|
|
||||||
await this.createPaymentReceiveStripePayment.createPaymentReceived(
|
|
||||||
tenantId,
|
|
||||||
saleInvoiceId,
|
|
||||||
amountInDollars
|
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 'payment_intent.payment_failed':
|
case 'payment_intent.payment_failed':
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
|
||||||
|
|
||||||
export interface StripePaymentLinkCreatedEventPayload {
|
export interface StripePaymentLinkCreatedEventPayload {
|
||||||
tenantId: number;
|
tenantId: number;
|
||||||
paymentLinkId: string;
|
paymentLinkId: string;
|
||||||
saleInvoiceId: number;
|
saleInvoiceId: number;
|
||||||
stripeIntegrationId: number;
|
stripeIntegrationId: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StripeCheckoutSessionCompletedEventPayload {
|
||||||
|
event: any;
|
||||||
|
}
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ import { TriggerInvalidateCacheOnSubscriptionChange } from '@/services/Subscript
|
|||||||
import { EventsTrackerListeners } from '@/services/EventsTracker/events/events';
|
import { EventsTrackerListeners } from '@/services/EventsTracker/events/events';
|
||||||
import { CreatePaymentLinkOnInvoiceCreated } from '@/services/StripePayment/events/CreatePaymentLinkOnInvoiceCreated';
|
import { CreatePaymentLinkOnInvoiceCreated } from '@/services/StripePayment/events/CreatePaymentLinkOnInvoiceCreated';
|
||||||
import { InvoicePaymentIntegrationSubscriber } from '@/services/Sales/Invoices/subscribers/InvoicePaymentIntegrationSubscriber';
|
import { InvoicePaymentIntegrationSubscriber } from '@/services/Sales/Invoices/subscribers/InvoicePaymentIntegrationSubscriber';
|
||||||
|
import { StripeWebhooksSubscriber } from '@/services/StripePayment/events/StripeWebhooksSubscriber';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
return new EventPublisher();
|
return new EventPublisher();
|
||||||
@@ -296,6 +297,7 @@ export const susbcribers = () => {
|
|||||||
// Stripe Payment
|
// Stripe Payment
|
||||||
CreatePaymentLinkOnInvoiceCreated,
|
CreatePaymentLinkOnInvoiceCreated,
|
||||||
InvoicePaymentIntegrationSubscriber,
|
InvoicePaymentIntegrationSubscriber,
|
||||||
|
StripeWebhooksSubscriber,
|
||||||
|
|
||||||
...EventsTrackerListeners
|
...EventsTrackerListeners
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -716,5 +716,10 @@ export default {
|
|||||||
|
|
||||||
onPaymentLinkCreated: 'onStripePaymentLinkCreated',
|
onPaymentLinkCreated: 'onStripePaymentLinkCreated',
|
||||||
onPaymentLinkInactivated: 'onStripePaymentLinkInactivated'
|
onPaymentLinkInactivated: 'onStripePaymentLinkInactivated'
|
||||||
|
},
|
||||||
|
|
||||||
|
// Stripe Payment Webhooks
|
||||||
|
stripeWebhooks: {
|
||||||
|
onCheckoutSessionCompleted: 'onStripeCheckoutSessionCompleted'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user