Line_item metadata set in stripe.checkout.sessions.create not returned in webhook

4,464 views
Skip to first unread message

Tom Rafferty

unread,
Jul 9, 2020, 6:11:12 PM7/9/20
to Stripe API Discussion
Hi All,

I am trying to 

I create the checkout session like this, where order_items is a parameter containing order data:

-----
const handleOrderPlaced = (business_id, location_id, location_currency, table_id, stripe_acc, order_items) => {

   
const line_items = [ ];
   
for await (const item of order_items) {
        line_items
.push({
            price_data
: {
                currency
: location_currency,
                product_data
: {
                    name
: item.item_name,
                    description
: item.item_description,
                    metadata: {
                        item_id
: item.item_id
                   
}

               
},
                unit_amount
: item.item_price
           
},
            quantity
: item.item_quantity
         
});
   
}

   
try {
       
const data: any = {
            payment_method_types
: ['card'],
            mode
: 'payment',
            line_items
: line_items,
            payment_intent_data
: {
                application_fee_amount
: 10,
           
},
           
metadata: {
                business_id
: business_id,
                location_id
: location_id,
                table_id
: table_id
           
},

            success_url
: `${envVariableMapper.envSystemURL}/checkoutRedirect?&message=success`,
            cancel_url
: `${envVariableMapper.envSystemURL}/checkoutRedirect?message=fail`
       
};

       
const connectData = {
            stripeAccount
: stripe_acc
       
}

       
const session = await stripe.checkout.sessions.create(data, connectData);

   
} catch (err) {

   
}
}

-----

I then have a webhook for the checkout.session.completed event and use stripe.checkout.sessions.listLineItems(session_id, { stripeAccount: account }) to get line item data for the completed checkout.

The checkout session level metadata, highlighted green is included in the webhook. The line item specific metadata, highlighted yellow, is not returned in listLineItems. For each item returned by listLineItems.data the price.metadata = {}.

Is there another way to set line item specific metadata, or somewhere else I should be accessing it?

Thanks,
Tom

Remi J.

unread,
Jul 9, 2020, 7:22:33 PM7/9/20
to api-d...@lists.stripe.com
Hello Tom,

In your code you are setting the metadata on the Product and not on the Price. This means that you will only see the metadata if you look at the product itself. This information is quite deep inside the LineItem resource and you'd have to use the Expand feature to expand all the relevant objects by passing `expand: ['line_items.data.price.product']`. Doing so would yield the metadata back, which I've confirmed locally.

I don't think putting metadata on the Product itself is what you want though. Why don't you put the information directly on the line item itself like this:

line_items.push({
  price_data: {
    currency: location_currency,
    product_data: {
      name: item.item_name,
      description: item.item_description,
    },
    unit_amount: item.item_price,
  },
  quantity: item.item_quantity,
  metadata: {
    item_id: item.item_id,
  },
});


That way the information would be returned in the resource by simply expanding the `line_items` property.

Hope this helps!
Remi


--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Tom Rafferty

unread,
Jul 10, 2020, 4:01:02 PM7/10/20
to Stripe API Discussion
Hi Remi,

Thanks for your response.

I moved metadata to the place you suggested:

for await (const item of order_items) {
line_items.push({
price_data: {
currency: location_currency,
product_data: {
name: item.item_name,
description: item.item_description
},
unit_amount: item.item_price
},
quantity: item.item_quantity,
metadata: {
item_id: item.item_id
}
});
}

I then receive the following error:

An error occurred while updating business: {"type":"StripeInvalidRequestError","raw":{"code":"parameter_unknown","doc_url":"https://stripe.com/docs/error-codes/parameter-unknown","message":"Received unknown parameter: line_items[0][metadata]","param":"line_items[0][metadata]","type":"invalid_request_error"

It seems from the docs that line_items.price_data.product_data.metadata is the only place you can put it without getting this error: https://stripe.com/docs/api/checkout/sessions/create

I'm using Stripe 8.69.0

Thanks,
Tom
To unsubscribe from this group and stop receiving emails from it, send an email to api-d...@lists.stripe.com.

Remi J.

unread,
Jul 10, 2020, 5:05:02 PM7/10/20
to api-d...@lists.stripe.com
Hey Tom,

Sorry about the confusion here. You're right that LineItems don't support metadata and I must have tested the wrong version of my code, I'm sorry. So I think your original approach was correct. You just need to explicitly expand `line_items.data.price.product` to read the metadata back in your code from the Product itself. It's pretty deeply nested but it will work!

I'll flag internally that metadata on line items would be useful in your case!

Best,
Remi

To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.
Reply all
Reply to author
Forward
0 new messages