next.js 条带无效请求错误:不能使用“行_项.金额”、“行_项.货币”、“行_项.名称”、“行_项.说明"或”行_项

igetnqfo  于 2022-11-23  发布在  其他
关注(0)|答案(2)|浏览(153)

我正在构建一个Amazon克隆,当我尝试将条带与克隆集成时,我遇到以下错误。有人能帮助我吗?我指的视频是:https://www.youtube.com/watch?v=4E0WOUYF-QI&t=4092s
错误代码段:
错误-条带无效请求错误:在此API版本中不能使用line_items.amountline_items.currencyline_items.nameline_items.descriptionline_items.images。请使用line_items.priceline_items.price_data。有关详细信息,请参阅https://stripe.com/docs/payments/checkout/migrating-prices
代码片段:

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 });
};
8yoxcaq7

8yoxcaq71#

这里的问题是transformedItems函数。您初始化stripe所用的API版本要求产品的描述(即item.description)位于product_data对象中。
通过简单地将描述移动到所描述的对象内来重写函数,将简单地为:

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],
        },
    },
}));

此信息显示在错误提供给您的documentation链接中,但您可能错过了它。

4bbkushb

4bbkushb2#

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],
      },
    },
  }));

相关问题