#mh_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1285182023220330517
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- mh_webhooks, 3 minutes ago, 61 messages
after the checkout is completed . iam trying to save the order to the DB
but its not working
i have two routes
app.use("/api/stripe", stripeWebhookRoutes);
app.use("/api/stripe", stripeRoutes);
and this is the stripe code
So it seems like the error comes from your own database and not Stripe.
the reason iam asking that iam getting a 500 error in the events . from the webhook. and the payment is not completed
Response
HTTP status code
500 (Internal Server Error)
Internal Server Error
so i cant verify the payment after the session is completed
and also the metadat is empty . Session metadata: {} .
userId not found in session metadata
const customer = await stripe.customers.create({
metadata: {
userId: req.body.userId, // Ensure userId is included here
cart: JSON.stringify(req.body.cartItems),
},
});
payment_intent_data: {
metadata: {
order_id: req.body.orderId,
userId: req.body.userId // Ensure userId is included here as well
}
},
You see the error in Stripe events because your own app responds with an error.
You need to fix the problem with your own database to solve this.
i tried to add test name .payment_intent_data: {
metadata: {
order_id: req.body.orderId,
userId: '12345667'
}
},its also not working.
do you still think that its from my DB?
Are you sure that req.body.userId is not empty?
yes. i replace it also with a test name. . and also i console logged the name before the request is made, it awas in the console log. so iam sure that the userId is not emplty
first i need to create the customer with the metadata (with userID), then create the stripe session with the metadata(with UserId) then the webhook will be called and the order will be saved .right?
The problem is that you're setting the metadata on the PaymentIntent object and not on Checkout Session object.
phone_number_collection: { enabled: true },
line_items,
mode: "payment",
customer: customer.id,
success_url: http://localhost:3000/checkout-success,
cancel_url: http://localhost:3000/cart,
payment_intent_data: {
metadata: {
order_id: req.body.orderId,
userId: req.body.userId // Ensure userId is included here as well
}
},
client_reference_id: req.body.userId // Optionally use user ID as a reference
});
it should be like this you mean phone_number_collection: { enabled: true },
line_items,
mode: "payment",
customer: customer.id,
success_url: http://localhost:3000/checkout-success,
cancel_url: http://localhost:3000/cart
metadata: {
order_id: req.body.orderId,
userId: req.body.userId // Ensure userId is included here as well
}
client_reference_id: req.body.userId // Optionally use user ID as a reference
});?
Yes, this will set the metadata on the Session.
still the same error.userId not found in session metadata
UserId is missing from session metadata
Could you please share the Checkout Session ID?
just a minute
cs_test_a1YTGWmHfVc0FfqtPgoeK1tertScPpfPHs0i8JRpCnoAljnhKkQExhrjIZ
Checkout Session was completed! cs_test_a1YTGWmHfVc0FfqtPgoeK1tertScPpfPHs0i8JRpCnoAljnhKkQExhrjIZ
I don't see the metadata being set on this Checkout Session.
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card", "ideal"],
shipping_address_collection: {
allowed_countries: ["US", "CA", "KE"],
},
shipping_options: [
{
shipping_rate_data: {
type: "fixed_amount",
fixed_amount: {
amount: 0,
currency: "eur",
},
display_name: "Free shipping",
delivery_estimate: {
minimum: { unit: "business_day", value: 5 },
maximum: { unit: "business_day", value: 7 },
},
},
},
{
shipping_rate_data: {
type: "fixed_amount",
fixed_amount: { amount: 1500, currency: "eur" },
display_name: "Next day air",
delivery_estimate: {
minimum: { unit: "business_day", value: 1 },
maximum: { unit: "business_day", value: 1 },
},
},
},
],
phone_number_collection: { enabled: true },
line_items,
mode: "payment",
customer: customer.id,
success_url: http://localhost:3000/checkout-success,
cancel_url: http://localhost:3000/cart,
metadata: { // Include metadata directly on the checkout session
order_id: req.body.orderId,
userId: req.body.userId
},
client_reference_id: req.body.userId // Optionally pass user ID as a reference
});
this is the exact code that i have
This was created 40 min ago, do you have a newer example ID?
ok . just a minute
HTTP status code
200 (OK)
{
"received": true
} i was clicking on resend in the events liestner. where i should re-create a new session after fix the metaData to be directly is the session object
Checkout Session was completed! cs_test_a1hfklHuIbiImLqsZwUL67OI5bYPPjF5sEFAXOiadedrgo7f5T2korvhAA
Order saved to database successfully.
thanks. i was stuck in this for 3 days
can you share with me the link to the docs where is has this info?
where to place the metadata
It's mostly about knowing what object you're expecting to get in the webhook event payload: checkout.session.completed carries a Checkout Session, and payment_intent.succeeded contains a PaymentIntent, for example. You need to set the metadata on the object you're intending to use later.
However, you can as well access the PaymentIntent via the CheckoutSession.payment_intent: https://docs.stripe.com/api/payment_intents/retrieve
Happy to help.