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
, orline_items.images
in this API version. Please useline_items.price
orline_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
, orline_items.images
in this API version. Please useline_items.price
orline_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
2 Answers
Reset to default 6The 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],
},
},
}));
本文标签:
版权声明:本文标题:javascript - StripeInvalidRequestError: You cannot use `line_items.amount`, `line_items.currency`, `line_items.name`, `line_item 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742308131a2450331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论