#bwestwood_webhooks

1 messages ยท Page 1 of 1 (latest)

nova kernelBOT
#

๐Ÿ‘‹ 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.

worldly bobcat
#
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 });
}
karmic ridgeBOT
mint stump
worldly bobcat
#

Okay thank you

mint stump
#

No problem! Happy to help ๐Ÿ˜„

worldly bobcat
#

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?

mint stump
#

No, it should be Subscription ID in the format of sub_xxx, not Checkout Session ID

worldly bobcat
#

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

mint stump
#

Ah I see! Then this is correct

worldly bobcat
#

yea maybe that id error is somewhere else in my code

#

I will keep checking thank you