Search code examples
stripe-paymentsvercel

Stripe checkout giving errors in vercel but not in localhost


I was using stripe CLI with webhook for testing stripe checkout. I used localhost:3000/api/webhook along with test key and everything worked fine in localhost.

Now I update everything in vercel and put the webhook URL in stripe to mydomain.com/api/webhook with updated keys and upgrading no longer works...

enter image description here

enter image description here

This is the errors I have in vercel:

enter image description here

enter image description here enter image description here enter image description here

This is the code in which the error orginates:

import { auth, currentUser } from "@clerk/nextjs";
import { NextResponse } from "next/server";

import prismadb from "@/lib/prismadb";
import { stripe } from "@/lib/stripe";
import { absoluteUrl } from "@/lib/utils";

const settingsUrl = absoluteUrl("/settings");

export async function GET() {
  try {
    const { userId } = auth();
    const user = await currentUser();

    if (!userId || !user) {
      return new NextResponse("Unauthorized", { status: 401 });
    }

    const userSubscription = await prismadb.userSubscription.findUnique({
      where: {
        userId
      }
    })

    if (userSubscription && userSubscription.stripeCustomerId) {
      const stripeSession = await stripe.billingPortal.sessions.create({
        customer: userSubscription.stripeCustomerId,
        return_url: settingsUrl,
      })

      return new NextResponse(JSON.stringify({ url: stripeSession.url }))
    }

    const stripeSession = await stripe.checkout.sessions.create({
      success_url: settingsUrl,
      cancel_url: settingsUrl,
      payment_method_types: ["card"],
      mode: "subscription",
      billing_address_collection: "auto",
      customer_email: user.emailAddresses[0].emailAddress,
      line_items: [
        {
          price_data: {
            currency: "USD",
            product_data: {
              name: "RoleplayPal Pro",
              description: "Create Custom AI RoleplayPals"
            },
            unit_amount: 999,
            recurring: {
              interval: "month"
            }
          },
          quantity: 1,
        },
      ],
      metadata: {
        userId
      },
    })

    return new NextResponse(JSON.stringify({ url: stripeSession.url }))
  } catch (error) {
    console.log("[STRIPE]", error);
    return new NextResponse("Internal Error", { status: 500 });
  }
};

How can I resolve this issue?


Solution

  • you need to create a webhook on stripe to listen the event, the webhool url must be the real website that you have, i.e: https://www.roleplaypals.fun.api/stripe or just www.roleplaypals.fun.api/stripe(not quiet sure which one), then select the event that you need to listen, checkout session, subscrition etc(don't select all it will be too much then maybe it won't affect right away). then copy paste the new webhook secret in vercel then finally redeploy the latest functionnable deployment. it should work