#Meldin
1 messages ยท Page 1 of 1 (latest)
Thank you Tarzan
So I was going through webhook docs and I made a post route which is /api/stripe/webhook. Now in that route I have sime console.logs like payment received. When I make succesfull purchase everything is good but I am not sure does webhook working properly bc I dont see those logs on my terminal
Here is the code : export async function POST(request) {
let event = request;
try {
const rawBody = await buffer(request);
const signature = request.headers["stripe-signature"];
console.log("This is the signature from api/stripe/webhook: ", signature);
event = stripe.webhooks.constructEvent(
rawBody,
signature,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (error) {
console.log("This is the error from api/stripe/webhook: ", error.message);
}
console.log("This is the event from api/webhook: ", event);
let session = null;
if (event.type === "checkout.session.completed") {
session = event.data.object;
// Fulfill the order
console.log("Payment was received");
}
return new NextResponse(session, {
status: 200,
});
}
Stripe sends the post requst automatically to /webhook route, is that right?
have you created a webhook endpoint on your dashboard?
what's your account ID?
acct_1NDDuGKcHEQbmQX7
the endpoint URL you used is wrong api/stripe/product
making the webhook endpoint disabled
it's the URL that Stripe uses to send the event to you
Got it so I guess I should change that to /api/stripe/webhook
@agile kindle Hi tarzan had to step out. You all good?
Hey
np
I trying to fix the issue with webhook
I change the url route which is /api/stripe/webhook
now if I want to test that webhook and to see logs in my terminal I should run stripe listen right?
Also, I am running the app localy on localhost:3000
Yes that's right
I know there is a some command like stripe listen force localhost:3000 to redirect the webhook to localhost in order to test it
do you know how that command looks like?
Oh I think you're referring to forward-to: https://stripe.com/docs/cli/listen#listen-forward-to
Yes
So something like: stripe listen --forward-to http://localhost:4242
Yes I tried and I get 200 POST http://localhost:3000 [evt_ .....] which is great and it means its working. But I cannot see console.log anywhere on my terminal neiter on dashbord. And the reason that I am looking for that is I have to see what event gives me back bc I have to do some business logic if payment is succesfull. Like update the qty of the product. Here is the code stripe listen --forward-to http://localhost:4242
Are you using 4242 or 3000 for the port?
I am sorry here is the code: import { NextResponse, NextRequest } from "next/server";
import Stripe from "stripe";
import { buffer } from "micro";
import { client } from "@/sanity/lib/client";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// export const runtime = "nodejs";
export async function POST(request) {
let event = request;
try {
const rawBody = await buffer(request);
const signature = request.headers["stripe-signature"];
console.log("This is the signature from api/stripe/webhook: ", signature);
event = stripe.webhooks.constructEvent(
rawBody,
signature,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (error) {
console.log("This is the error from api/stripe/webhook: ", error.message);
}
console.log("This is the event from api/webhook: ", event);
let session = null;
if (event.type === "checkout.session.completed") {
session = event.data.object;
// Fulfill the order
// HERE SHOULD BE THE CODE TO UPDATE THE PRODUCT
console.log("Payment was received");
}
return new NextResponse(session, {
status: 200,
});
}
In your stripe listen command are you using port 3000? And is your server running on port 3000?
Yes I am running the app on 3000 and stripe listen command is 3000 also.
Sorry for confusion
So your code is running and everything works as expected? The only thing that doesn't work is your log statements?
That's correct
Thank you very much for your time. I know its little bit strange.
Have a great day
OOO one more thing please is there any property to specify session to expire
You're referring to checkout sessions?
yes
So when payment is succesfull there is a session id in the url, so I would like to limit time for that session id
You can pass: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-expires_at
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.