diff --git a/packages/core/modules-sdk/src/definitions.ts b/packages/core/modules-sdk/src/definitions.ts index cb33ed1a916cb..d35499b2cc6bf 100644 --- a/packages/core/modules-sdk/src/definitions.ts +++ b/packages/core/modules-sdk/src/definitions.ts @@ -199,7 +199,7 @@ export const ModulesDefinition: { label: upperCaseFirst(Modules.PAYMENT), isRequired: false, isQueryable: true, - dependencies: ["logger"], + dependencies: ["logger", Modules.EVENT_BUS], defaultModuleDeclaration: { scope: MODULE_SCOPE.INTERNAL, resources: MODULE_RESOURCE_TYPE.SHARED, diff --git a/packages/modules/payment/src/services/payment-module.ts b/packages/modules/payment/src/services/payment-module.ts index b59730a9c4ff0..f61ded419eb55 100644 --- a/packages/modules/payment/src/services/payment-module.ts +++ b/packages/modules/payment/src/services/payment-module.ts @@ -11,6 +11,7 @@ import { FilterablePaymentProviderProps, FilterablePaymentSessionProps, FindConfig, + IEventBusModuleService, IPaymentModuleService, ModuleJoinerConfig, ModulesSdkTypes, @@ -36,6 +37,7 @@ import { MathBN, MedusaContext, MedusaError, + Modules, ModulesSdkUtils, PaymentCollectionStatus, PaymentSessionStatus, @@ -50,7 +52,11 @@ import { RefundReason, } from "@models" import { joinerConfig } from "../joiner-config" -import { PaymentModuleOptions } from "../types" +import { + PaymentCollectionEventData, + PaymentCollectionEvents, + PaymentModuleOptions, +} from "../types" import PaymentProviderService from "./payment-provider" type InjectedDependencies = { @@ -61,6 +67,7 @@ type InjectedDependencies = { paymentSessionService: ModulesSdkTypes.IMedusaInternalService paymentCollectionService: ModulesSdkTypes.IMedusaInternalService paymentProviderService: PaymentProviderService + [Modules.EVENT_BUS]?: IEventBusModuleService } const generateMethodForModels = { @@ -91,6 +98,7 @@ export default class PaymentModuleService protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService protected paymentProviderService_: PaymentProviderService + protected readonly eventBusModuleService_?: IEventBusModuleService constructor( { @@ -101,6 +109,7 @@ export default class PaymentModuleService paymentSessionService, paymentProviderService, paymentCollectionService, + [Modules.EVENT_BUS]: eventBusModuleService, }: InjectedDependencies, protected readonly options: PaymentModuleOptions ) { @@ -115,6 +124,7 @@ export default class PaymentModuleService this.paymentSessionService_ = paymentSessionService this.paymentProviderService_ = paymentProviderService this.paymentCollectionService_ = paymentCollectionService + this.eventBusModuleService_ = eventBusModuleService } __joinerConfig(): ModuleJoinerConfig { @@ -1174,6 +1184,10 @@ export default class PaymentModuleService sharedContext ) await this.maybeUpdatePaymentCollection_(payment.payment_collection_id) + await this.eventBusModuleService_?.emit({ + name: PaymentCollectionEvents.COLLECTION_UPDATED, + data: { id: payment.payment_collection_id }, + }) break } @@ -1191,6 +1205,7 @@ export default class PaymentModuleService ) let payment = session.payment + let emitEvent = false if (!payment) { const { id } = await this.authorizePaymentSession_( @@ -1206,6 +1221,7 @@ export default class PaymentModuleService }, sharedContext ) + emitEvent = true } const _capturedAmount = payment.captures.reduce( @@ -1227,6 +1243,7 @@ export default class PaymentModuleService }, sharedContext ) + emitEvent = true } if (MathBN.gt(refunded_amount, _refundedAmount)) { await this.refundPayment_( @@ -1239,9 +1256,16 @@ export default class PaymentModuleService }, sharedContext ) + emitEvent = true } await this.maybeUpdatePaymentSession_(session_id, data) await this.maybeUpdatePaymentCollection_(session.payment_collection_id) + if (emitEvent) { + await this.eventBusModuleService_?.emit({ + name: PaymentCollectionEvents.COLLECTION_UPDATED, + data: { id: session.payment_collection_id }, + }) + } break } @@ -1250,7 +1274,7 @@ export default class PaymentModuleService case "processing": { const session = await this.paymentSessionService_.retrieve( session_id, - { select: ["status"] }, + { select: ["status", "payment_collection_id"] }, sharedContext ) if ( @@ -1264,6 +1288,10 @@ export default class PaymentModuleService { id: session_id, data, status }, sharedContext ) + await this.eventBusModuleService_?.emit({ + name: PaymentCollectionEvents.COLLECTION_UPDATED, + data: { id: session.payment_collection_id }, + }) } break } @@ -1274,6 +1302,10 @@ export default class PaymentModuleService sharedContext ) await this.maybeUpdatePaymentCollection_(session.payment_collection_id) + await this.eventBusModuleService_?.emit({ + name: PaymentCollectionEvents.COLLECTION_UPDATED, + data: { id: session.payment_collection_id }, + }) break } default: { diff --git a/packages/modules/payment/src/types/index.ts b/packages/modules/payment/src/types/index.ts index 3941af3b59a0e..6c027651ed8c2 100644 --- a/packages/modules/payment/src/types/index.ts +++ b/packages/modules/payment/src/types/index.ts @@ -32,3 +32,11 @@ export type PaymentModuleOptions = Partial & { options?: Record }[] } + +export type PaymentCollectionEventData = { + id: string +} + +export enum PaymentCollectionEvents { + COLLECTION_UPDATED = "payment-collection.updated", +}