Stripe Payment Next.js: Webhook Issue (401 Unauthorized)
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
import Stripe from "stripe"
import { headers } from "next/headers"
import { NextResponse } from "next/server"
import prismadb from "@/lib/prismadb"
import { stripe } from "@/lib/stripe"
export async function POST(req: Request) {
const body = await req.text()
const signature = 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(
}
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 prismadb.userSubscription.create({
data: {
userId: session?.metadata?.userId,
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 prismadb.userSubscription.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 })
};
import { headers } from "next/headers"
import { NextResponse } from "next/server"
import prismadb from "@/lib/prismadb"
import { stripe } from "@/lib/stripe"
export async function POST(req: Request) {
const body = await req.text()
const signature = 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 prismadb.userSubscription.create({
data: {
userId: session?.metadata?.userId,
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 prismadb.userSubscription.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 })
};
1 Reply
Masai LionOP
import Stripe from "stripe"
export const stripe = new Stripe(process.env.STRIPE_API_KEY!, {
apiVersion: "2024-04-10",
typescript: true,
});
export const stripe = new Stripe(process.env.STRIPE_API_KEY!, {
apiVersion: "2024-04-10",
typescript: true,
});