#0_jennifer_0

1 messages ยท Page 1 of 1 (latest)

smoky hemlockBOT
dreamy wasp
#

Hello

#

Are you forwarding to your local endpoint via the CLI?

#

Or do you have an https endpoint set up?

raw stream
dreamy wasp
#

Okay can you show me your endpoint code?

raw stream
#
import { NextResponse } from "next/server";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

export async function POST(Request) {
  const stripeWebHookSecret =
    "whsec...";

  // Corrected: Parse the request body as JSON
  const requestData = await Request.text();
  const sig = Request.headers["stripe-signature"];
  let event = Stripe.Event;

  try {
    if (!sig) {
      console.log("no signature");
    }

    event = stripe.webhooks.constructEvent(
      requestData,
      sig,
      stripeWebHookSecret
    );

    console.log("received ", event.type);
    // Handle the event
  } catch (err) {
    return NextResponse.json({ error: `Webhook Error: ${err.message}` });
  }
  switch (event.type) {
    case "payment_intent.succeeded":
      const paymentIntentSucceeded = event.data.object;
      if (paymentIntentSucceeded.status === "succeeded") {
        console.log("Tunafish!!");
      } else {
        console.log("Payment intent failed or has another status.");
      }
      break;
    // ... handle other event types
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  return NextResponse.json({
    status: 200,
    message: "successful webhook return",
  });
}```
dreamy wasp
#

Redact your webhook secret since that is sensitive

#

Can you add a log at the very beginning of your endpoint code to ensure your endpoint is being hit successfully at all

raw stream
#

I will try that

#

it doesnt look like the endpoint is being hit

#

in the dashboard under Webhook CLI responses th http response shows pending_webhooks: 2

dreamy wasp
#

Yeah so then that seems like an issue with your server code in general

#

Can you show me how you are forwarding via the CLI?

#

So I can make sure that looks right

raw stream
#

stripe listen --forward-to localhost:3000/stripe-webhooks

#

which works and in the dashboard shows that its listening

dreamy wasp
#

Hmm okay yeah that looks fine.... so yeah it seems like an issue with your server just not having that endpoint exist

#

I don't know much about Next.JS so I can't help much with that

#

But really you need to make sure you can hit that endpoint before doing anything else

raw stream
#

the endpoint definitely exists, it was setup the same way the other routes are with next.js app feature, app/api/stripe-webhooks/route.js I am wondering if its a cors issue

dreamy wasp
#

Have you tried to make a cURL request from your Terminal to that endpoint?

#

Or just fetch it from your client?

#

Like basically access it any other way than having a Webhook Event be sent to it

raw stream
#

okay I setup postman and can access the endpoint

drowsy ridge
#

Hi there ๐Ÿ‘‹ jumping in as my teammate needed to step away. And the endpoint is now printing a log of some sort when it is accessed?

raw stream
#

yes, it will show me that the endpoint has been reached but then can't read the signature but I am wondering if that is because postman isnt set up to verify the signature.

drowsy ridge
#

Postman isn't involved for signature verification, you're likely seeing that message because the request you're sending from Postman doesn't have a properly computed signature included on it, so it's likely expected that the signature verification process will fail.

#

I believe what my teammate was hoping to accomplish with hitting that endpoint via Postman, was to ensure it is up and accessible. Does the additional logging that was added now get triggered when an Event is forwarded by the Stripe CLI to your endpoint?

raw stream
#

no the console logs are still not getting triggered when I forward an event to the endpoint via the Stripe CLI

drowsy ridge
#

Hm odd. Are the URLs you used for the --forward-to option when running stripe listen and in your Postman request the same?

raw stream
#

oh its working now, in my vscode console when I forward an event I am not recieving a signature

#

okay I solved it, Request.headers["stripe-signature"] needs to be Request.headers.get("stripe-signature")

#

one line of code driving me nuts for days ๐Ÿ˜…

drowsy ridge
#

Haha, such is the life of a dev. Glad to hear you were able to get to the bottom of it!

raw stream
#

thanks! and thanks for your help!