#earth-checkout-webhook
1 messages · Page 1 of 1 (latest)
app.post("/Checkout/:userID", async (req, res) => {
let items = [];
if (!req.session.user) {
return res.json({ success: false, message: "User not logged in." });
}
try {
const cart = await CartModel.findOne({
cartUser: req.params.userID,
});
if (cart) {
const cartItems = await CartModel.find({
cartUser: req.params.userID,
});
items = cartItems.map((item) => ({
name: item.name,
price: item.price,
}));
const customer = await stripe.customers.create({
email: req.session.user.email,
name: req.session.user.name,
// You can include additional customer details as needed
});
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: items.map((item) => ({
price_data: {
currency: "usd",
product_data: {
name: item.name,
},
unit_amount: item.price * 100,
},
quantity: 1,
})),
customer: customer.id,
mode: "payment",
success_url:
"https://20e4-2606-40-878b-32b-00-1260-c6ed.ngrok-free.app/home",
cancel_url:
"https://20e4-2606-40-878b-32b-00-1260-c6ed.ngrok-free.app/shop",
});
res.json({ sessionId: session.id });
}
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: "Server error." });
}
});
When I complete the checkout session, the checkout.session.completed event is never called. However, using CLI. Everything works perfectly when I call a test..
quoting here
thanks!
checkout.session.completed is a webhook event. It will be sent to a registered endpoint if you have that configured in your account
- Do you have a publicly reachable webhook endpoint configured for your Stripe account?
- Is it listening for the
checkout.session.completedevent?
This is using CLI. But yes, I have also listened for it using ngrok to expose localhost:3000 and listen for the checkout event. But nothing came of that.
Is it currently configured to do so?
Are you talking about the one configured on Stripe?
Yes. You said the CLI works. I'm guessing you are referring to the stripe listen --forward-to command
Yes. But it only works when I trigger an event like stripe trigger checkout.session.completed
When I go through the checkout session. It doesn't trigger the event.
Can you share the checkout session ID of a recently completed checkout session?
cs_test_b1H7PlrGHO8ZeaO2kxMlg9KjzY2PbHU40jCcQQUaJev2icsNIGirvfWSGM
This is an ID of a checkout session created with StripeJS.
When you say created with Stripe.js, do you mean using redirectToCheckout or the Node.js code you shared above?
stripe-node and Stripe.js are two separate libraries
Okay, Node.js
This Checkout Session was never completed, so no webhook event will fire
Yeah. When I use stripe.checkout.sessions.create
Here, let me actually go through a checkout.
When I create a session and go through it using a test card and random information, it completes. Yet, I don't think it posts to the webhook.
Can you share the ID of a completed checkout session?
Wait, how would I get the ID of a completed one made with NodeJS stripe.checkout.sessions.create?
If you look in your Stripe Dashboard you should be able to see them in the logs: https://dashboard.stripe.com/test/dashboard
req_8dZozuOIgTVxzm
I believe this should be an ID for a completed session.
earth-checkout-webhook
The event fired but it was not delivered
https://dashboard.stripe.com/test/events/evt_1OKphAFZeswgxgLXvVYz8LIu is an example Event. You have an ngrok endpoint that is currently dead
Okay! I was searching through my logs for my webhook, and just now, the events appeared. Earlier, they were not there.
I have an error from the events:
Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing
Okay so the next step is to read those docs and ensure that you are passing the exact raw body of the request. I think you're using Node.js which can get in the way a lot sadly