#Meldin

1 messages ยท Page 1 of 1 (latest)

grim martenBOT
dense nest
#

๐Ÿ‘‹ happy to help

#

I'm not sure I follow

agile kindle
#

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?

dense nest
#

have you created a webhook endpoint on your dashboard?

agile kindle
#

Yes I did

#

But this is test mode

dense nest
#

what's your account ID?

agile kindle
#

acct_1NDDuGKcHEQbmQX7

dense nest
#

the endpoint URL you used is wrong api/stripe/product

#

making the webhook endpoint disabled

agile kindle
#

Ahhaaa I see

#

I though that has nothing to do when is in test mode

dense nest
#

it's the URL that Stripe uses to send the event to you

grim martenBOT
agile kindle
#

Got it so I guess I should change that to /api/stripe/webhook

grim martenBOT
floral tinsel
#

@agile kindle Hi tarzan had to step out. You all good?

agile kindle
#

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

floral tinsel
#

Yes that's right

agile kindle
#

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?

floral tinsel
agile kindle
#

Yes

floral tinsel
#

So something like: stripe listen --forward-to http://localhost:4242

agile kindle
#

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

floral tinsel
#

Are you using 4242 or 3000 for the port?

agile kindle
#

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,
});
}

floral tinsel
#

In your stripe listen command are you using port 3000? And is your server running on port 3000?

agile kindle
#

Yes I am running the app on 3000 and stripe listen command is 3000 also.

#

Sorry for confusion

floral tinsel
#

So your code is running and everything works as expected? The only thing that doesn't work is your log statements?

agile kindle
#

That's correct

floral tinsel
#

Oh ok. Not a node expert so I can't say

#

Maybe try using another logging library

agile kindle
#

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

floral tinsel
#

You're referring to checkout sessions?

agile kindle
#

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

floral tinsel