#stan-trees_code
1 messages · Page 1 of 1 (latest)
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.
- stan-trees_code, 6 hours ago, 4 messages
- stan-trees_code, 7 hours ago, 21 messages
- stan-trees_api, 16 hours ago, 62 messages
👋 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/1240114149112152227
📝 Have more to share? Add more details, code, screenshots, videos, etc. below.
const app = require('express')();
const bodyParser = require('body-parser');
const fulfillOrder = (lineItems) => {
// TODO: fill me in
console.log("Fulfilling order", lineItems);
}
app.post('/webhook', bodyParser.raw({ type: 'application/json' }), async (request, response) => {
const payload = request.body;
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret);
} catch (err) {
return response.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
const sessionWithLineItems = await stripe.checkout.sessions.retrieve(
event.data.object.id,
{
expand: ['line_items'],
}
);
const lineItems = sessionWithLineItems.line_items;
// Fulfill the purchase...
fulfillOrder(lineItems);
}
response.status(200).end();
});
app.listen(4242, () => console.log('Running on port 4242'));```
after completing checkout i dont get anything in console
im running 2 terminals right now, one for stripe and one for running this
on one of them i get this
which is the stripe terminal
other one i only get this
even after i complete checkout
your terminal is showing that your server is responding with 400 http response code. You can add an additional line in your webhook code to log the err.message also to see what it says
oh
im getting this now
If a webhook request is being forwarded by a third-party tool, ensure that the exact request body, including JSON formatting and new line style, is preserved.
Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing```
so the constructEvent expects the raw request body to be passed in to it.
It looks like you're using Express.js - the sequence of middleware matters. Does your node middleware configure parse the requests somewhere else prior to the code you've shown? As an example, if you have app.use(express.json()); prior to your route, this will parse the requests in all JSON, not in raw form before reaching to your webhook function.
dont think so
const endpointSecret = ''
const app = require('express')();
const bodyParser = require('body-parser');
const fulfillOrder = (lineItems) => {
// TODO: fill me in
console.log("Fulfilling order", lineItems);
}
app.post('/webhook', bodyParser.raw({ type: 'application/json' }), async (request, response) => {
const payload = request.body;
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret);
} catch (err) {
console.log(err.message)
return response.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
const sessionWithLineItems = await stripe.checkout.sessions.retrieve(
event.data.object.id,
{
expand: ['line_items'],
}
);
const lineItems = sessionWithLineItems.line_items;
// Fulfill the purchase...
fulfillOrder(lineItems);
}
response.status(200).end();
});
app.listen(4242, () => console.log('Running on port 4242'));
the full code
the other point you'll want to check is whether you're using the correct webhook secret key - are you using the webhook secret from the Stripe CLI?
ohhh no wonder
i was using the one from the dashboard i think
thanks
if you're forwarding webhook events using the Stripe CLI, you would want to use the CLI webhook secret
is there anotherway to forward webhook events?
i dont want to run 2 terminals in the future
you can use a third party service like ngrok which provides a public url for you to use and tunnels to your localhost. IMO, you could find ngrok even more troublesome to use cause the URL will always change (for the free version) and you have to always update the URL for your webhook endpoint
so 2 terminals is best way to go? also another question how do i identify who did the payment?
i have this for my session payment link
const session = await stripe.checkout.sessions.create({
line_items: [
{
price: price.id,
quantity: 1,
},
],
mode: 'payment',
success_url: 'http://localhost:4242/webhook',
cancel_url: 'https://discord.gg/DphWdkkUcm',
payment_method_types: ['card'],
payment_intent_data: {
transfer_group: 'ORDER_91'
}
})``` and i thought the payment_intent_data would have done something buti dont see any of that in the response from the checkout webhook
object: "list",
data: [
{
id: "li_1PGWwDJEoFgb8rxdYIVvLvyt",
object: "item",
amount_discount: 0,
amount_subtotal: 5000,
amount_tax: 0,
amount_total: 5000,
currency: "usd",
description: "My Product",
price: {
id: "price_1PGWwCJEoFgb8rxdoNO6WvB1",
object: "price",
active: true,
billing_scheme: "per_unit",
created: 1715737644,
currency: "usd",
custom_unit_amount: null,
livemode: false,
lookup_key: null,
metadata: {},
nickname: null,
product: "prod_Q6kRxfur71vsRX",
recurring: null,
tax_behavior: "unspecified",
tiers_mode: null,
transform_quantity: null,
type: "one_time",
unit_amount: 5000,
unit_amount_decimal: "5000",
},
quantity: 1,
}
],
has_more: false,
url: "/v1/checkout/sessions/cs_test_a1ER8yjQDlHz6Iak6m0K9LLfzrt6BvSLQNOQzribcM0kn2jj0siiljAp3L/line_items",
}``` the object i got
what kind of identification information are you looking for? can you give an example?
hmmm well is there any way of me being able to pass information into the creation of session link? and then i'd be able to receive it in the webhook response
like a unique id or something
If you pass in the metadata in https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-metadata, only the Checkout Session object will contain the metadata.
If you pass the metadata into subscription_data.metadata (this is for mode="subscription") - the Subscription will contain that metadata, and so will the Invoice Line Items : https://stripe.com/docs/api/invoices/line_item#invoice_line_item_object-metadata
If you pass in the metadata into payment_intent_data.metadata (this is for mode="payment") - the PaymentIntent will contain that metadata : https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
i used this on creating session
const session = await stripe.checkout.sessions.create({
line_items: [
{
price: price.id,
quantity: 1,
},
],
mode: 'payment',
success_url: 'http://localhost:4242/webhook',
cancel_url: 'https://discord.gg/DphWdkkUcm',
payment_method_types: ['card'],
payment_intent_data: {
metadata: {
amount: amount,
id: "test1234"
}
}
})``` but i couldnt find that value in the backend side
what's the corresponding PaymentIntent id, can you paste that here?
i found a payment_intent with a value and an empty metadata object
payment_intent: "pi_3PGX4BJEoFgb8rxd0pvJ1x1R", ?
sorry ill be back
i can see it in the PaymentIntent : https://dashboard.stripe.com/test/events/evt_3PGX4BJEoFgb8rxd0l5LjYSk
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
hmm how do i get that object?
const sessionWithLineItems = await stripe.checkout.sessions.retrieve(
event.data.object.id,
{
expand: ['line_items'],
}
);
const lineItems = sessionWithLineItems;
// Fulfill the purchase...
fulfillOrder(lineItems);```
ah nevermind got it
thanks!