Skip to content

Commit fcee74e

Browse files
committed
fix(payment-stripe): handle Stripe webhook signature errors
Wrap the constructEvent call in constructWebhookEvent with a try/catch that rethrows through buildError, matching the error-handling convention used by every other method in this provider. Stripe's constructEvent throws a StripeSignatureVerificationError on an invalid or missing signature; normalizing it here keeps webhook failures consistent with the rest of the module's error output.
1 parent dde167d commit fcee74e

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/payment-stripe": patch
3+
---
4+
5+
fix(payment-stripe): normalize errors thrown while constructing Stripe webhook events

packages/modules/providers/payment-stripe/src/core/__tests__/stripe-base.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ProviderWebhookPayload } from "@medusajs/framework/types"
12
import type { StripeOptions } from "../../types"
23

34
import IdealProviderService from "../../services/stripe-ideal"
@@ -76,4 +77,17 @@ describe("StripeBase", () => {
7677
expect(params.capture_method).toBe("manual")
7778
})
7879
})
80+
81+
describe("constructWebhookEvent", () => {
82+
it("wraps signature verification failures with a normalized error", () => {
83+
const service = new StripeProviderService({}, defaultOptions)
84+
85+
expect(() =>
86+
service.constructWebhookEvent({
87+
rawData: "{}",
88+
headers: { "stripe-signature": "invalid-signature" },
89+
} as ProviderWebhookPayload["payload"])
90+
).toThrow("An error occurred in constructWebhookEvent")
91+
})
92+
})
7993
})

packages/modules/providers/payment-stripe/src/core/stripe-base.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,15 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
801801
constructWebhookEvent(data: ProviderWebhookPayload["payload"]): Stripe.Event {
802802
const signature = data.headers["stripe-signature"] as string
803803

804-
return this.stripe_.webhooks.constructEvent(
805-
data.rawData as string | Buffer,
806-
signature,
807-
this.options_.webhookSecret
808-
)
804+
try {
805+
return this.stripe_.webhooks.constructEvent(
806+
data.rawData as string | Buffer,
807+
signature,
808+
this.options_.webhookSecret
809+
)
810+
} catch (error) {
811+
throw this.buildError("An error occurred in constructWebhookEvent", error)
812+
}
809813
}
810814
protected buildError(message: string, error: Error): Error {
811815
const errorDetails =

0 commit comments

Comments
 (0)