#Hugo.G

1 messages · Page 1 of 1 (latest)

flat berryBOT
fast barn
#

Hello! Can you give me some more details - where are you seeing a 307?

proud atlas
#

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 ?

fast barn
#

Does your webhook logic include a redirect of some sort?

proud atlas
#

`
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

fast barn
#

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

proud atlas
#

Mmmh I'm going to check this

#

Okay now i got a 404 not found

fast barn
#

I'd guess that means somehting is wrong with your routes then

proud atlas
#

yeah..

#

Don't understand my route is just there :/

fast barn
flat berryBOT
proud atlas
#

Yeah i just rename it right now

#

I got a 500

copper cedar
#

Ok so you would want to debug to see whether it ever reaches your webhookHandler