#msarheed_api
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/1230045808444051456
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
According to the Stripe API create session documentation it should be possible to attach some metadata to the product_data when creating a session.
In my Nextjs application im creating a session, and attaching my own item id to the metadata, so I can retrieve this data in my Webhook and do something with it. However, when im retrieving the lineitems based on the session id, the metadata field just returns empty {}.
// Create session
const lineItems = cartDetailsArray.map((item: CartItem) => {
return {
price_data: {
currency: item.currency,
product_data: {
name: item.name,
images: [item.imageSrc],
metadata: {
hello: "world",
},
},
unit_amount: item.price,
},
quantity: item.quantity,
};
});
This is in my Webhook where I retrieve the line items:
// Make a call to the Stripe API to retrieve the line items from the session id
let lineItems = await stripe.checkout.sessions.retrieve(
checkoutSessionId, {
expand: ['line_items']
// I have also tried expand: ['line_items.data.price.product'], which didn't do much difference
});
The data that the api returns, the metadata is just empty.
line_items: {
object: "list",
data: [
{
id: "li_1P6H4yLdTRTdaXcDRNzCoj5d",
object: "item",
amount_discount: 0,
amount_subtotal: 9500,
amount_tax: 0,
amount_total: 9500,
currency: "dkk",
description: "Quattro Stagioni",
price: {
id: "price_1P6H4yLdTRTdaXcDfjNrwN6K",
object: "price",
active: false,
billing_scheme: "per_unit",
created: 1713293404,
currency: "dkk",
custom_unit_amount: null,
livemode: false,
lookup_key: null,
metadata: {},
nickname: null,
product: [Object],
recurring: null,
tax_behavior: "unspecified",
tiers_mode: null,
transform_quantity: null,
type: "one_time",
unit_amount: 9500,
unit_amount_decimal: "9500"
},
quantity: 1
}
],
has_more: false,
url: "/v1/checkout/sessions/cs_test_a1pAXDWq9LbFWlSuUQFDRXQQe5huSBXt5zDO0xSDPfVoKiUPm42PIe3Z2t/line_items"
},
Im a doing something wrong or what's the reason for not being able to retrieve the metadata from the product data? Or is there another way I can attach my own custom item id to each product, so I can later retrieve this id from the session_id?
That metadata could be on the Product object, inside the Price object. It's not directly the Price object
Could you please explain further?
Im attaching the metadata to the product_data object?
const lineItems = cartDetailsArray.map((item: CartItem) => {
return {
price_data: {
currency: item.currency,
product_data: {
name: item.name,
images: [item.imageSrc],
metadata: {
hello: "world",
},
},
// Multiply by 100 because the formattedPrice is in DKK and Stripe expects the amount in cents
unit_amount: item.price * 100,
},
quantity: item.quantity,
};
});
Yes, then it would be the metadata of the Product generated
Can you Call Retrieve Price API https://docs.stripe.com/api/prices/retrieve and pass in price_1P6H4yLdTRTdaXcDfjNrwN6K, and also expanding product?
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
expand: ['product']
yeah when doing that, im able to see the metadata. Can i in any way retrive the metadata when retriving the checkout session by sessionid?
let lineItems = await stripe.checkout.sessions.retrieve(
checkoutSessionId, {
expand: ['line_items']
});
If the user bought multiple items in their cart, then i need to make an api call to the retrive price endpoint for each item to retrive the product metadata. is there some way to retrive the metadata from the line items?