From 078d94fc882b1fb9c23bb831541f262dac9871da Mon Sep 17 00:00:00 2001 From: shij Date: Sun, 29 Sep 2024 13:38:41 +0800 Subject: [PATCH] fix(payment): add webhook options --- packages/core/types/src/payment/service.ts | 16 +++++-------- .../src/api/hooks/payment/[provider]/route.ts | 10 ++++---- .../payment/src/services/payment-module.ts | 24 +++++-------------- packages/modules/payment/src/types/index.ts | 5 ++++ 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/packages/core/types/src/payment/service.ts b/packages/core/types/src/payment/service.ts index fc19672f30e4c..a5e642e24799a 100644 --- a/packages/core/types/src/payment/service.ts +++ b/packages/core/types/src/payment/service.ts @@ -443,12 +443,6 @@ export interface IPaymentModuleService extends IModuleService { sharedContext?: Context ): Promise - retrievePaymentSession( - paymentSessionId: string, - config?: FindConfig, - sharedContext?: Context - ): Promise - /* ********** PAYMENT SESSION ********** */ /** @@ -1088,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 4a43b6596760e..a7a9d64d11df7 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/types" import { ModuleRegistrationName, PaymentWebhookEvents } from "@medusajs/utils" import { MedusaRequest, MedusaResponse } from "../../../../types/routing" @@ -7,9 +6,8 @@ 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(ModuleRegistrationName.PAYMENT).options || {} + const options = + req.scope.resolve(ModuleRegistrationName.PAYMENT).webhookOptions || {} const event = { provider, @@ -25,8 +23,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 14f9011446e37..d3811520dd576 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, @@ -50,6 +49,7 @@ import { RefundReason, } from "@models" import { joinerConfig } from "../joiner-config" +import { PaymentModuleOptions } from "../types" import PaymentProviderService from "./payment-provider" type InjectedDependencies = { @@ -101,7 +101,7 @@ export default class PaymentModuleService paymentProviderService, paymentCollectionService, }: InjectedDependencies, - protected readonly moduleDeclaration: InternalModuleDeclaration + protected readonly options: PaymentModuleOptions ) { // @ts-ignore super(...arguments) @@ -558,22 +558,6 @@ export default class PaymentModuleService return result[0] } - @InjectManager("baseRepository_") - // @ts-expect-error - async retrievePaymentSession( - id: string, - config: FindConfig = {}, - @MedusaContext() sharedContext?: Context - ): Promise { - const session = await this.paymentSessionService_.retrieve( - id, - config, - sharedContext - ) - - return this.baseRepository_.serialize(session) - } - @InjectManager("baseRepository_") // @ts-expect-error async listPaymentSessions( @@ -833,6 +817,10 @@ export default class PaymentModuleService ) } + get webhookOptions() { + return this.options.webhook + } + @InjectManager("baseRepository_") async listPaymentProviders( filters: FilterablePaymentProviderProps = {}, diff --git a/packages/modules/payment/src/types/index.ts b/packages/modules/payment/src/types/index.ts index bee03db2dba0f..6623c18776125 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/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 */