admin管理员组

文章数量:1332890

I'm building an Amazon clone and I'm getting the following error when I try to integrate stripe with the clone. Can someone please help me? The video I'm referring to is this one: ;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 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 });
};

I'm building an Amazon clone and I'm getting the following error when I try to integrate stripe with the clone. Can someone please help me? The video I'm referring to is this one: https://www.youtube./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./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 });
};
Share Improve this question edited Oct 1, 2022 at 14:15 juliomalves 50.5k23 gold badges177 silver badges168 bronze badges asked Sep 24, 2022 at 4:15 Nihal_20Nihal_20 711 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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

本文标签: