javascript 条带无效请求错误:不能使用"行项目.金额"、"行项目.货币"、"行项目.名称"、"行项目.说明"

qlvxas9a  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(112)

我试图发起从条纹付款,但它再次失败。下面是我的代码。请帮助我为什么会发生这种情况。
节点版本:版本18.7.0
NPM版本:8.4.1
Stripe_api.js

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

checkout.js

function Checkout() {
  const items = useSelector(selectItems);
  const total = useSelector(selectTotal);
  const [session] = useSession();
    const createCheckoutSession = async() => {
        const stripe = await stripePromise;

    //checkout session
    const checkoutSession = await axios.post(
      '/api/Stripe/create-checkout-session',
      {
        items: items,
        email: session.user.email,
      }     
    );
    const result = await stripe.redirectToCheckout({
        sessionId: checkoutSession.data.id,
      });
  
      if (result.error) {
        alert(result.error.message);
      }
}
  return (
    <div className="bg-gray-100">
      <Header />
      <main className="lg:flex max-w-screen-2xl mx-auto">
        {/* Left*/}
        <div>
          <div className="flex flex-col p-5 space-y-10 bg-white">
            <h1 className="text-3xl border-b pb-4">
              {items.length === 0
                ? "Your Amazon Basket is empty"
                : "Shopping Basket"}
            </h1>

            {items.map((item, i) => (
              <CheckoutProduct
                key={i}
                id={item.id}
                title={item.title}
                rating={item.rating}
                price={item.price}
                description={item.description}
                category={item.category}
                image={item.image}
                hasPrime={item.hasPrime}
              />
            ))}
          </div>
        </div>

        <div>
          {items.length > 0 && (
            <>
              <h2 className="whitespace-nowrap">
                Subtotal ({items.length} items):
                <span className="font-bold">
                  <Currency quantity={total} currency="GBP" />
                </span>
              </h2>

              <button
                role="link"
                onClick = {createCheckoutSession}
                disabled={!session}
                className={`button mt-2 ${
                  !session &&
                  "from-gray-300 to-gray-500 border-gray-200 text-gray-300 cursor-not-allowed"
                }`}
              >
                {!session ? "Sign in to checkout" : "Proceed to checkout"}
              </button>
            </>
          )}
        </div>
      </main>
    </div>
  );
}

export default Checkout;

数据通过console. log(items)完美加载

[
  {
    id: 2,
    title: 'Mens Casual Premium Slim Fit T-Shirts ',
    price: 22.3,
    description: 'Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.',
    category: "men's clothing",
    image: 'https://****/****/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg',
    hasPrime: false
  }
]

错误
条带无效请求错误:您不能在此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。在StripeError. generate(C:\Users\shikshgu\shopping site\Amazon\node_modules\stripe\lib\Error. js:36:16)在res. to JSON中生成。然后,在. StripeAPI错误消息(C:\Users\shikshgu\shopping site\Amazon\node_modules\stripe\lib\StripeResource. js:201:35)在进程中生成。进程滴答和拒绝(节点:内部/进程/任务队列:95:5){类型:"条带无效请求错误",原始:{message:'您不能在此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。',request_log_url:"https://dashboard.stripe.com/test/logs/req_28SN2DhsGvhHVF?t = 1665815298",键入以下内容:"无效请求错误",标头:{服务器:'txtx',日期:"2022年10月15日星期六06:28:18 GMT","内容类型":"应用程序/json"、"内容长度":"470",连接:"保持活动"、"访问控制允许凭据":"真"、"访问控制允许方法":"获取、发布、头、选项、删除"、"访问控制允许源":"*"、"访问控制暴露标头":"请求ID、条带管理版本、需要X-Stripe外部身份验证、需要X-Stripe特权会话"、"访问控制最大年龄":"300","缓存控制":"无缓存、无存储"、"幂等键":"849332d0 - 1d08 - 4052-bc3a-3c92b503d4cd","原始请求":"请求_28SN2DhsGvhHVF","请求标识":"req_28SN2DhsGvhHVF","条带化应重试":"假"、"条带版本":'2022 - 08 - 01','严格交通安全':最大年龄为63072000;包括子域;预加载'},状态代码:400,请求ID:'req_28SN2DhsGvhHVF '},原始类型:"无效请求错误",代码:未定义,doc_url:未定义,

vlf7wbxs

vlf7wbxs1#

使用create API时,只能使用line_items.priceline_items.price_data。请参阅创建会话。create
从条带产品中拷贝Price ID并将其放入。
获取此价格ID

将其放入line_items

price

  • 这是我的python函数,你的工作原理类似 *

相关问题