-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa-payment-stripe): Add delay to Stripe webhook
- Loading branch information
1 parent
d9e29e8
commit 3f99ed5
Showing
9 changed files
with
74 additions
and
86 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/medusa-payment-stripe/src/api/admin/orders/stripe-payments/[order_id]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa" | ||
import { getStripePayments } from "../../../../../controllers/get-payments" | ||
|
||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { | ||
const payments = await getStripePayments(req) | ||
res.json({ payments }) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { MiddlewaresConfig } from "@medusajs/medusa" | ||
import { raw } from "body-parser" | ||
|
||
export const config: MiddlewaresConfig = { | ||
routes: [ | ||
{ | ||
matcher: "/stripe/hooks", | ||
middlewares: [raw({ type: "application/json" })], | ||
}, | ||
], | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa" | ||
import { constructWebhook } from "../../utils/utils" | ||
|
||
const WEBHOOK_DELAY = process.env.STRIPE_WEBHOOK_DELAY ?? 2000 | ||
|
||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { | ||
let event | ||
|
||
try { | ||
event = constructWebhook({ | ||
signature: req.headers["stripe-signature"], | ||
body: req.body, | ||
container: req.scope, | ||
}) | ||
|
||
const eventBus = req.scope.resolve("eventBusService") | ||
|
||
// we delay the processing of the event to avoid a conflict caused by a race condition | ||
await eventBus.emit("medusa.stripe_payment_intent", event, { | ||
delay: WEBHOOK_DELAY, | ||
attempts: 2, | ||
}) | ||
} catch (err) { | ||
res.status(400).send(`Webhook Error: ${err.message}`) | ||
return | ||
} | ||
|
||
res.sendStatus(200) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { type SubscriberArgs, type SubscriberConfig } from "@medusajs/medusa" | ||
import Stripe from "stripe" | ||
import { handlePaymentHook } from "../api/utils/utils" | ||
|
||
export default async function stripeHandler({ | ||
data, | ||
container, | ||
}: SubscriberArgs<Stripe.Event>) { | ||
const event = data | ||
const paymentIntent = event.data.object as Stripe.PaymentIntent | ||
|
||
await handlePaymentHook({ | ||
event, | ||
container, | ||
paymentIntent, | ||
}) | ||
} | ||
|
||
export const config: SubscriberConfig = { | ||
event: "medusa.stripe_payment_intent", | ||
context: { | ||
subscriberId: "medusa.stripe_payment_intent", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters