StripeInvalidRequestError: You cannot use `line_items.amount`, `line_items.currency`, `line_items.name`, `line_items.description`, or `line_items

Viewed 16

im building a amazon clone and im getting the following error when i try to integrate stripe with the clone can someone please help me. the video im referring to is this one :https://www.youtube.com/watch?v=4E0WOUYF-QI&t=4092s

the error snippet:

error - StripeInvalidRequestError: You cannot use line_items.amount, line_items.currency, line_items.name, line_items.description, or line_items.images in this API version. Please use line_items.price or line_items.price_data. Please see https://stripe.com/docs/payments/checkout/migrating-prices for more information.

the code snippet:

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

export default async (req, res) => { const { items, email } = req.body;

const transformedItems = items.map((item) => ({
    description: item.description,
    quantity: 1,
    price_data: {
        currency: "gbp",
        unit_amount: item.price * 100,
        product_data: {
            name: item.title,
            images: [item.image],
        },
    },
}));

const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    shipping_rates: ["shr_1LkVMHSArY9HEMGlxjejfRWf"],
    shipping_address_collection: {
        allowed_countries: ["GB", "US", "CA"],
    },
    line_items: transformedItems,
    mode: "payment",
    success_url: `${process.env.HOST}/success`,
    cancel_url: `${process.env.HOST}/checkout`,
    metadata: {
        email,
        images: JSON.stringify(items.map((item) => item.image)),
    },
});

res.status(200).json({ id: session.id });

};

thank you

1 Answers

The problem here is with the transformedItems function. The API version which you've initialized stripe with, requires the product's description (i.e. item.description) to be inside the product_data object.

Rewriting your function by simply moving the description inside the object described would simply be:

const transformedItems = items.map((item) => ({
    quantity: 1,
    price_data: {
        currency: "gbp",
        unit_amount: item.price * 100,
        product_data: {
            name: item.title,
            description: item.description, //description here
            images: [item.image],
        },
    },
}));

This information was presented in the documentation link that the error provided you, but you've probably missed it.

Related