#0_jennifer_0
1 messages ยท Page 1 of 1 (latest)
Hello
Are you forwarding to your local endpoint via the CLI?
Or do you have an https endpoint set up?
I am forwarding the endpoint to my local endpoint via the CLI for now http://localhost:3000/stripe-webhooks
Okay can you show me your endpoint code?
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",
});
}```
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
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
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
stripe listen --forward-to localhost:3000/stripe-webhooks
which works and in the dashboard shows that its listening
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
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
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
okay I setup postman and can access the endpoint
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?
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.
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?
no the console logs are still not getting triggered when I forward an event to the endpoint via the Stripe CLI
Hm odd. Are the URLs you used for the --forward-to option when running stripe listen and in your Postman request the same?
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 ๐
Haha, such is the life of a dev. Glad to hear you were able to get to the bottom of it!
thanks! and thanks for your help!