#Hugo.G
1 messages · Page 1 of 1 (latest)
Hello! Can you give me some more details - where are you seeing a 307?
When i put this command in my terminal :
stripe listen --forward-to localhost:3000/api/webhook
I can share you maybe my code if it helps you ?
Does your webhook logic include a redirect of some sort?
`
import { NextResponse } from "next/server"
import Stripe from "stripe"
import { stripe } from "@/lib/stripe"
import { db } from "@/server/db"
export async function POST(req: Request) {
const body = await req.text()
const signature = req.headers.get("Stripe-Signature") as string
let event: Stripe.Event
try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_SECRET!
)
} catch (error: any) {
return new NextResponse(Webhook Error: ${error.message}, { status: 400 })
}
const session = event.data.object as Stripe.Checkout.Session
if (event.type === "checkout.session.completed") {
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
)
if (!session?.metadata?.userId) {
return new NextResponse("User id is required", { status: 400 });
}
await db.orgSubscription.create({
data: {
orgId: session?.metadata?.orgId as string,
stripeSubscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
stripePriceId: subscription.items.data[0]?.price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
},
})
}
if (event.type === "invoice.payment_succeeded") {
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
)
await db.orgSubscription.update({
where: {
stripeSubscriptionId: subscription.id,
},
data: {
stripePriceId: subscription.items.data[0]?.price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
},
})
}
return new NextResponse(null, { status: 200 })
};
`
it is my pages/api/webhook/index.ts
I don't see any anything redirect related in that code, but is your framework doing some redirect?
If you look at those events in your dashboard you should be able to see the response coming back from your server
I looked at the first example from your screenshot (evt_1OCtEzCOxzvO5mt0Ti1EYPw9) and it looks like you have a redirect that's pointing to /login - so you need to look into your code and see why that's happening
I'd guess that means somehting is wrong with your routes then
Are you missing an 's' at the end? From the first screenshot your hitting http://localhost:3000/api/webhook, but your route.ts file is in a 'webhooks' folder
Ok so you would want to debug to see whether it ever reaches your webhookHandler