Skip to content

Commit

Permalink
feat(medusa-payment-stripe): Add delay to Stripe webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Dec 10, 2023
1 parent d9e29e8 commit 3f99ed5
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 86 deletions.
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 })
}
18 changes: 0 additions & 18 deletions packages/medusa-payment-stripe/src/api/hooks/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/medusa-payment-stripe/src/api/hooks/stripe.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/medusa-payment-stripe/src/api/index.ts

This file was deleted.

11 changes: 11 additions & 0 deletions packages/medusa-payment-stripe/src/api/middlewares.ts
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 packages/medusa-payment-stripe/src/api/stripe/hooks/route.ts
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)
}
8 changes: 2 additions & 6 deletions packages/medusa-payment-stripe/src/api/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ export async function handlePaymentHook({
container,
paymentIntent,
}: {
event: { type: string; id: string }
event: Stripe.Event
container: AwilixContainer
paymentIntent: {
id: string
metadata: { cart_id?: string; resource_id?: string }
last_payment_error?: { message: string }
}
paymentIntent: Stripe.PaymentIntent
}): Promise<{ statusCode: number }> {
const logger = container.resolve("logger")

Expand Down
24 changes: 24 additions & 0 deletions packages/medusa-payment-stripe/src/subscribers/stripe.ts
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",
},
}
3 changes: 1 addition & 2 deletions packages/medusa/src/api/routes/store/auth/create-session.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { IsEmail, IsNotEmpty } from "class-validator"
import jwt from "jsonwebtoken"
import { EntityManager } from "typeorm"
import { defaultRelations } from "."
import AuthService from "../../../../services/auth"
import CustomerService from "../../../../services/customer"
import { validator } from "../../../../utils/validator"
import { defaultRelations } from "."

/**
* @oas [post] /store/auth
Expand Down

0 comments on commit 3f99ed5

Please sign in to comment.