From 125879ada4478e3397fe08d128c17d1b4ab44d53 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Tue, 9 Jan 2024 14:21:05 +0200 Subject: [PATCH] fix(medusa-payment-stripe): change webhook environment variables to plugin options (#6034) * fix(medusa-payment-stripe): change webhook environment variables to plugin options * access options through service --- .changeset/spotty-rivers-provide.md | 5 +++++ .../src/api/stripe/hooks/route.ts | 12 +++++++----- .../medusa-payment-stripe/src/api/utils/utils.ts | 4 +++- .../medusa-payment-stripe/src/core/stripe-base.ts | 4 ++++ packages/medusa-payment-stripe/src/types/index.ts | 10 ++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 .changeset/spotty-rivers-provide.md diff --git a/.changeset/spotty-rivers-provide.md b/.changeset/spotty-rivers-provide.md new file mode 100644 index 0000000000..9c8721f64d --- /dev/null +++ b/.changeset/spotty-rivers-provide.md @@ -0,0 +1,5 @@ +--- +"medusa-payment-stripe": patch +--- + +fix(medusa-payment-stripe): change webhook environment variables to plugin options diff --git a/packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts b/packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts index 35132700a6..5de5879aaa 100644 --- a/packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts +++ b/packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts @@ -1,11 +1,13 @@ import { MedusaRequest, MedusaResponse } from "@medusajs/medusa" import { constructWebhook } from "../../utils/utils" - -const WEBHOOK_DELAY = process.env.STRIPE_WEBHOOK_DELAY ?? 5000 // 5s -const WEBHOOK_RETRIES = process.env.STRIPE_WEBHOOK_RETRIES ?? 3 +import StripeProviderService from "../../../services/stripe-provider" export const POST = async (req: MedusaRequest, res: MedusaResponse) => { try { + const pluginOptions = req.scope.resolve( + "stripeProviderService" + ).options + const event = constructWebhook({ signature: req.headers["stripe-signature"], body: req.body, @@ -16,8 +18,8 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => { // we delay the processing of the event to avoid a conflict caused by a race condition await eventBus.emit("medusa.stripe_payment_intent_update", event, { - delay: WEBHOOK_DELAY, - attempts: WEBHOOK_RETRIES, + delay: pluginOptions.webhook_delay || 5000, + attempts: pluginOptions.webhook_retries || 3, }) } catch (err) { res.status(400).send(`Webhook Error: ${err.message}`) diff --git a/packages/medusa-payment-stripe/src/api/utils/utils.ts b/packages/medusa-payment-stripe/src/api/utils/utils.ts index 275afe5168..4b5eb43bc1 100644 --- a/packages/medusa-payment-stripe/src/api/utils/utils.ts +++ b/packages/medusa-payment-stripe/src/api/utils/utils.ts @@ -4,10 +4,12 @@ import { IdempotencyKeyService, PostgresError, } from "@medusajs/medusa" +import { ConfigModule, MedusaContainer } from "@medusajs/types" import { MedusaError } from "@medusajs/utils" import { AwilixContainer } from "awilix" import { EOL } from "os" import Stripe from "stripe" +import { StripeOptions } from "../../types" const PAYMENT_PROVIDER_KEY = "pp_stripe" @@ -255,4 +257,4 @@ async function completeCartIfNecessary({ ) } } -} +} \ No newline at end of file diff --git a/packages/medusa-payment-stripe/src/core/stripe-base.ts b/packages/medusa-payment-stripe/src/core/stripe-base.ts index df800aeade..20692508b4 100644 --- a/packages/medusa-payment-stripe/src/core/stripe-base.ts +++ b/packages/medusa-payment-stripe/src/core/stripe-base.ts @@ -40,6 +40,10 @@ abstract class StripeBase extends AbstractPaymentProcessor { abstract get paymentIntentOptions(): PaymentIntentOptions + get options(): StripeOptions { + return this.options_ + } + getStripe() { return this.stripe_ } diff --git a/packages/medusa-payment-stripe/src/types/index.ts b/packages/medusa-payment-stripe/src/types/index.ts index 7f3fea76b7..795631d99a 100644 --- a/packages/medusa-payment-stripe/src/types/index.ts +++ b/packages/medusa-payment-stripe/src/types/index.ts @@ -15,6 +15,16 @@ export interface StripeOptions { * Set a default description on the intent if the context does not provide one */ payment_description?: string + /** + * The delay in milliseconds before processing the webhook event. + * @defaultValue 5000 + */ + webhook_delay?: number + /** + * The number of times to retry the webhook event processing in case of an error. + * @defaultValue 3 + */ + webhook_retries?: number } export interface PaymentIntentOptions {