stripe / stripe-node

Node.js library for the Stripe API.
https://stripe.com
MIT License
3.88k stars 751 forks source link

metadata empty when retrieving a Checkout Session's line items #1991

Closed Mohamed-1954 closed 10 months ago

Mohamed-1954 commented 10 months ago

Describe the bug

Hello everyone, I've created a stripe checkout session and in the line_items property I'm passing the products I receive from the request's body. In the product_data of each product I added a metadata that contains the product's id. Then I created a webhook that listens to the checkout.session.completed event type, where I retrieve the line_items so that I can create an order document (because I'm working with mongoose) and save it in my database. The problem is: I can't find the id I defined in each product's metadata.

To Reproduce

N/A

Expected behavior

When retrieving the line_items and looping through the data property, I expect to find the id I defined in the metadata of each product.

Code snippets

// creating a checkout session
try {
        const cart: ICart[] = req.body.cart;
        const accountEmail: string = req.body.account;
        const tax: number = req.body.tax;
        const discountId: string = req.body.discount; 

        const cartItems: Stripe.Checkout.SessionCreateParams.LineItem[] =
            cart.map((item): Stripe.Checkout.SessionCreateParams.LineItem => {
                return {
                    price_data: {
                        currency: "mad",
                        product_data: {
                            name: item?.name,
                            description: item?.description,
                            images: item?.images,
                            metadata: { id: item?._id },
                        },
                        unit_amount: item?.amount * 100,
                    },
                    quantity: item?.quantity,
                };
            });

        // create the checkout session
        const session = await stripe.checkout.sessions.create({
            currency: "mad",
            ui_mode: "hosted",
            line_items: [
                ...(cartItems as Stripe.Checkout.SessionCreateParams.LineItem[]),
            ],
            billing_address_collection: "required",
            mode: "payment",
            customer_email: accountEmail,
            metadata: {
                email: accountEmail,
                tax: tax,
                discount: discountId,
            },
            success_url: `${env.APP_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
            cancel_url: `${env.APP_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
        });
        res.status(200).json({
            status: "success",
            session,
            clientSecret: session.client_secret,
        });
} catch (err) {
        console.log(err);
}

// in the webhook's swith case
// retrieving the line items
case "checkout.session.completed":
      const checkoutSessionCompleted = event.data.object;

      // Retrieve the session's line items
      const lineItems = (await stripe.checkout.sessions.listLineItems(checkoutSessionCompleted.id)).data.map((item) => {
      return {
              _id: item.price?.metadata?.id, // returns undefined
              quantity: item.quantity, // returns the quantity
             }
         });

OS

Wndows

Node version

Node v21.2.0

Library version

stripe-node v14.11.0

API version

2023-10-16

Additional context

No response

remi-stripe commented 10 months ago

Hey @Mohamed-1954, Github issues are limited to bugs with the SDK itself. Your question is an integration support question better directed at our support team: https://support.stripe.com/contact

You mentioned you set the metadata on the Product itself using product_data so the metadata would only live on that object. When you expand the line items of the Session you get the line items which don't have metadata or the Price which does but won't have the metadata you set since it was set on the Product. You have to expand the product property and look at the metadata on that resource. If you need further help, make sure to reach out to our support team: https://support.stripe.com/contact