From d7303ad94ee9777c9ee7337de01aecf1437e5ae6 Mon Sep 17 00:00:00 2001 From: shij Date: Thu, 10 Oct 2024 10:43:17 +0800 Subject: [PATCH] fix(payment): add webhook options --- packages/core/types/src/payment/service.ts | 10 ++++++---- .../medusa/src/api/hooks/payment/[provider]/route.ts | 9 +++------ .../modules/payment/src/services/payment-module.ts | 8 ++++++-- packages/modules/payment/src/types/index.ts | 5 +++++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/core/types/src/payment/service.ts b/packages/core/types/src/payment/service.ts index 430b2eaf6a5bf..a5e642e24799a 100644 --- a/packages/core/types/src/payment/service.ts +++ b/packages/core/types/src/payment/service.ts @@ -1082,23 +1082,25 @@ export interface IPaymentModuleService extends IModuleService { * ``` */ processEvent(data: ProviderWebhookPayload): Promise + + get webhookOptions(): PaymentModuleWebhookOptions | undefined } /** - * The options that the Payment Module accepts. + * The webhook events handling options that the Payment Module accepts. */ -export interface PaymentModuleOptions { +export interface PaymentModuleWebhookOptions { /** * The delay in milliseconds before processing the webhook event. * * @defaultValue 5000 */ - webhook_delay?: number + delay?: number /** * The number of times to retry the webhook event processing in case of an error. * * @defaultValue 3 */ - webhook_retries?: number + retries?: number } diff --git a/packages/medusa/src/api/hooks/payment/[provider]/route.ts b/packages/medusa/src/api/hooks/payment/[provider]/route.ts index ae9ca3ba7afe7..784fae2c355a4 100644 --- a/packages/medusa/src/api/hooks/payment/[provider]/route.ts +++ b/packages/medusa/src/api/hooks/payment/[provider]/route.ts @@ -1,4 +1,3 @@ -import { PaymentModuleOptions } from "@medusajs/framework/types" import { Modules, PaymentWebhookEvents } from "@medusajs/framework/utils" import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" @@ -7,9 +6,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => { try { const { provider } = req.params - const options: PaymentModuleOptions = - // @ts-expect-error "Not sure if .options exists on a module" - req.scope.resolve(Modules.PAYMENT).options || {} + const options = req.scope.resolve(Modules.PAYMENT).webhookOptions || {} const event = { provider, @@ -25,8 +22,8 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => { data: event, }, { - delay: options.webhook_delay || 5000, - attempts: options.webhook_retries || 3, + delay: options.delay ?? 5000, + attempts: options.retries ?? 3, } ) } catch (err) { diff --git a/packages/modules/payment/src/services/payment-module.ts b/packages/modules/payment/src/services/payment-module.ts index 9f0337f38e1f1..54b83c03161c6 100644 --- a/packages/modules/payment/src/services/payment-module.ts +++ b/packages/modules/payment/src/services/payment-module.ts @@ -11,7 +11,6 @@ import { FilterablePaymentProviderProps, FilterablePaymentSessionProps, FindConfig, - InternalModuleDeclaration, IPaymentModuleService, ModuleJoinerConfig, ModulesSdkTypes, @@ -51,6 +50,7 @@ import { RefundReason, } from "@models" import { joinerConfig } from "../joiner-config" +import { PaymentModuleOptions } from "../types" import PaymentProviderService from "./payment-provider" type InjectedDependencies = { @@ -102,7 +102,7 @@ export default class PaymentModuleService paymentProviderService, paymentCollectionService, }: InjectedDependencies, - protected readonly moduleDeclaration: InternalModuleDeclaration + protected readonly options: PaymentModuleOptions ) { // @ts-ignore super(...arguments) @@ -854,6 +854,10 @@ export default class PaymentModuleService ) } + get webhookOptions() { + return this.options.webhook + } + @InjectManager() async listPaymentProviders( filters: FilterablePaymentProviderProps = {}, diff --git a/packages/modules/payment/src/types/index.ts b/packages/modules/payment/src/types/index.ts index f19bd65ca7128..3941af3b59a0e 100644 --- a/packages/modules/payment/src/types/index.ts +++ b/packages/modules/payment/src/types/index.ts @@ -2,6 +2,7 @@ import { Logger, ModuleProviderExports, ModuleServiceInitializeOptions, + PaymentModuleWebhookOptions, } from "@medusajs/framework/types" export type InitializeModuleInjectableDependencies = { @@ -9,6 +10,10 @@ export type InitializeModuleInjectableDependencies = { } export type PaymentModuleOptions = Partial & { + /** + * The webhook options that control how to handle received payment webhook events + */ + webhook?: PaymentModuleWebhookOptions /** * Providers to be registered */