首页 > 解决方案 > React Stripe 结帐多个行项目

问题描述

我有一个带条带结帐的有效 React 应用程序。但我需要在结账时通过多个项目来付款。现在再次它只适用于一项。但是我有一组称为“项目”的对象,需要一次性支付。处理这个问题的最佳方法是什么?因此,如果您在我的代码中看到描述 Bounce House A 但项目数组可以有 [{description: 'Bounce House A', amount:200,qty:2},{description: 'Bounce House B', amount:250 ,数量:1}。

这是我当前的代码:

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY)

export default async (req, res) => {
  const { id, amount } = req.body

  try {
    const payment = await stripe.paymentIntents.create({
      amount,
      currency: 'USD',
      description: 'Bounce House A',
      payment_method: id,
      confirm: true,
    })
    console.log(payment)

    return res.status(200).json({
      confirm: 'abc123',
    })
  } catch (error) {}
}

标签: reactjsnext.jsstripe-payments

解决方案


您必须将这些数组存储在您的数据库中,并使用一个单一的“订单 ID”,您可以将其传递给 Stripe 的 PaymentIntent 下的metadata. 这样,您可以使用您设计的自定义订单 ID 查找与 PaymentIntent 关联的项目。

Stripemetadata只能存储字符串键值对,因此您不能在其中存储数组。您可能能够以某种一致的格式将数组存储为字符串,从而允许您将其反序列化回数组,但我不建议这样做。


推荐阅读