Skip to content

Commit

Permalink
feat: support payment collection update event
Browse files Browse the repository at this point in the history
  • Loading branch information
silenaker committed Nov 20, 2024
1 parent 2baa098 commit 4d6256a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/modules-sdk/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 34 additions & 2 deletions packages/modules/payment/src/services/payment-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
FilterablePaymentProviderProps,
FilterablePaymentSessionProps,
FindConfig,
IEventBusModuleService,
IPaymentModuleService,
ModuleJoinerConfig,
ModulesSdkTypes,
Expand All @@ -36,6 +37,7 @@ import {
MathBN,
MedusaContext,
MedusaError,
Modules,
ModulesSdkUtils,
PaymentCollectionStatus,
PaymentSessionStatus,
Expand All @@ -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 = {
Expand All @@ -61,6 +67,7 @@ type InjectedDependencies = {
paymentSessionService: ModulesSdkTypes.IMedusaInternalService<any>
paymentCollectionService: ModulesSdkTypes.IMedusaInternalService<any>
paymentProviderService: PaymentProviderService
[Modules.EVENT_BUS]?: IEventBusModuleService
}

const generateMethodForModels = {
Expand Down Expand Up @@ -91,6 +98,7 @@ export default class PaymentModuleService
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<PaymentSession>
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<PaymentCollection>
protected paymentProviderService_: PaymentProviderService
protected readonly eventBusModuleService_?: IEventBusModuleService

constructor(
{
Expand All @@ -101,6 +109,7 @@ export default class PaymentModuleService
paymentSessionService,
paymentProviderService,
paymentCollectionService,
[Modules.EVENT_BUS]: eventBusModuleService,
}: InjectedDependencies,
protected readonly options: PaymentModuleOptions
) {
Expand All @@ -115,6 +124,7 @@ export default class PaymentModuleService
this.paymentSessionService_ = paymentSessionService
this.paymentProviderService_ = paymentProviderService
this.paymentCollectionService_ = paymentCollectionService
this.eventBusModuleService_ = eventBusModuleService
}

__joinerConfig(): ModuleJoinerConfig {
Expand Down Expand Up @@ -1174,6 +1184,10 @@ export default class PaymentModuleService
sharedContext
)
await this.maybeUpdatePaymentCollection_(payment.payment_collection_id)
await this.eventBusModuleService_?.emit<PaymentCollectionEventData>({
name: PaymentCollectionEvents.COLLECTION_UPDATED,
data: { id: payment.payment_collection_id },
})
break
}

Expand All @@ -1191,6 +1205,7 @@ export default class PaymentModuleService
)

let payment = session.payment
let emitEvent = false

if (!payment) {
const { id } = await this.authorizePaymentSession_(
Expand All @@ -1206,6 +1221,7 @@ export default class PaymentModuleService
},
sharedContext
)
emitEvent = true
}

const _capturedAmount = payment.captures.reduce(
Expand All @@ -1227,6 +1243,7 @@ export default class PaymentModuleService
},
sharedContext
)
emitEvent = true
}
if (MathBN.gt(refunded_amount, _refundedAmount)) {
await this.refundPayment_(
Expand All @@ -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<PaymentCollectionEventData>({
name: PaymentCollectionEvents.COLLECTION_UPDATED,
data: { id: session.payment_collection_id },
})
}
break
}

Expand All @@ -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 (
Expand All @@ -1264,6 +1288,10 @@ export default class PaymentModuleService
{ id: session_id, data, status },
sharedContext
)
await this.eventBusModuleService_?.emit<PaymentCollectionEventData>({
name: PaymentCollectionEvents.COLLECTION_UPDATED,
data: { id: session.payment_collection_id },
})
}
break
}
Expand All @@ -1274,6 +1302,10 @@ export default class PaymentModuleService
sharedContext
)
await this.maybeUpdatePaymentCollection_(session.payment_collection_id)
await this.eventBusModuleService_?.emit<PaymentCollectionEventData>({
name: PaymentCollectionEvents.COLLECTION_UPDATED,
data: { id: session.payment_collection_id },
})
break
}
default: {
Expand Down
8 changes: 8 additions & 0 deletions packages/modules/payment/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ export type PaymentModuleOptions = Partial<ModuleServiceInitializeOptions> & {
options?: Record<string, unknown>
}[]
}

export type PaymentCollectionEventData = {
id: string
}

export enum PaymentCollectionEvents {
COLLECTION_UPDATED = "payment-collection.updated",
}

0 comments on commit 4d6256a

Please sign in to comment.