Skip to content

Commit

Permalink
fix(payment): add webhook options
Browse files Browse the repository at this point in the history
  • Loading branch information
silenaker committed Oct 15, 2024
1 parent 8b65f89 commit d7303ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/core/types/src/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,23 +1082,25 @@ export interface IPaymentModuleService extends IModuleService {
* ```
*/
processEvent(data: ProviderWebhookPayload): Promise<void>

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
}
9 changes: 3 additions & 6 deletions packages/medusa/src/api/hooks/payment/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PaymentModuleOptions } from "@medusajs/framework/types"
import { Modules, PaymentWebhookEvents } from "@medusajs/framework/utils"

import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions packages/modules/payment/src/services/payment-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FilterablePaymentProviderProps,
FilterablePaymentSessionProps,
FindConfig,
InternalModuleDeclaration,
IPaymentModuleService,
ModuleJoinerConfig,
ModulesSdkTypes,
Expand Down Expand Up @@ -51,6 +50,7 @@ import {
RefundReason,
} from "@models"
import { joinerConfig } from "../joiner-config"
import { PaymentModuleOptions } from "../types"
import PaymentProviderService from "./payment-provider"

type InjectedDependencies = {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class PaymentModuleService
paymentProviderService,
paymentCollectionService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
protected readonly options: PaymentModuleOptions
) {
// @ts-ignore
super(...arguments)
Expand Down Expand Up @@ -854,6 +854,10 @@ export default class PaymentModuleService
)
}

get webhookOptions() {
return this.options.webhook
}

@InjectManager()
async listPaymentProviders(
filters: FilterablePaymentProviderProps = {},
Expand Down
5 changes: 5 additions & 0 deletions packages/modules/payment/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import {
Logger,
ModuleProviderExports,
ModuleServiceInitializeOptions,
PaymentModuleWebhookOptions,
} from "@medusajs/framework/types"

export type InitializeModuleInjectableDependencies = {
logger?: Logger
}

export type PaymentModuleOptions = Partial<ModuleServiceInitializeOptions> & {
/**
* The webhook options that control how to handle received payment webhook events
*/
webhook?: PaymentModuleWebhookOptions
/**
* Providers to be registered
*/
Expand Down

0 comments on commit d7303ad

Please sign in to comment.