#bwestwood_webhooks
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1216975209228275723
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { database } from "@/lib/prismadb";
import Stripe from "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) {
return new NextResponse("Invalid signature", { status: 400 });
}
const session = event.data.object as Stripe.Checkout.Session;
console.log("Session", session);
if (event.type === "customer.subscription.created") {
const subscription = await stripe.subscriptions.retrieve(
session.id as string
);
if (!session?.metadata?.userId) {
return new NextResponse("User id is required", { status: 400 });
}
const subscriptionCreated = await database.userSubscription.create({
data: {
userId: session?.metadata?.userId,
stripeSubscriptionId: subscription.id as string,
stripeCustomerId: subscription.customer as string,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
},
});
console.log("User pro subscription created", subscriptionCreated);
}
return new NextResponse("User subscription created", { status: 200 });
}
Error 400 was returned from your own Webhook server: https://dashboard.stripe.com/test/events/evt_1OtNOdIGYAlugcNbGMRJT39j with the error:
User id is required
Stripe has no visibility why your server returned such error to Stripe.
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Okay thank you
No problem! Happy to help ๐
last question
that method. that I am running stripe.subscriptions.retrieve
I pass in the ID of the session.id from the session of the event.data.object
is that correct?
No, it should be Subscription ID in the format of sub_xxx, not Checkout Session ID
Session {
id: 'sub_1OtNdsIGYAlugcNbQGQVIQdf',
object: 'subscription',
application: null,
application_fee_percent: null,
automatic_tax: { enabled: false, liability: null },
billing_cycle_anchor: 1711429968,
billing_cycle_anchor_config: null,
this is part of the session variable
i was using session.id
Ah I see! Then this is correct