使用 API ID 通过 Stripe 一次性付款

Dar*_*ava 0 stripe-payments

我使用 Stripe 创建了一个订阅服务。我可以订阅用户使用定期付款。这是相关代码(节点):

  // Create the subscription
  const subscription = await stripe.subscriptions.create({
    customer: req.body.customerId,
    items: [{ price: req.body.priceId }],
    expand: ['latest_invoice.payment_intent'],
  });
Run Code Online (Sandbox Code Playgroud)

这有效并使用priceId仪表板中所示的:

在此处输入图片说明

但是,当我销售不经常出现的产品时,它就会失败。我收到错误:

The price specified is set to `type=one_time` but this field only accepts prices with `type=recurring`
Run Code Online (Sandbox Code Playgroud)

我理解错误,但我不确定是否可以将订阅设置为不重复。

我的应用程序有 3 层:

  • 一旦脱落
  • 每月
  • 每年

理想情况下,我不想添加一个全新的代码段来处理似乎是订阅所做的一个子集的事情,但即使我这样做了,该paymentIntent对象似乎只需要一个数量而不是 API ID,如图所示. 有没有办法使用我已经建立的基础设施来做到这一点?

Pau*_*jes 5

您不能以非重复价格创建订阅,而对于一次性付款,您可以使用 PaymentIntent。

价格旨在用于订阅和 Checkout 产品,但您仍然可以将其中的数据用于 PaymentIntent。例如:

// get the price 
const price = await stripe.prices.retrieve(req.body.priceId);

// check if the price is recurring or not
if (price.recurring !== null) {
  // Create the subscription
  const subscription = await stripe.subscriptions.create({
    customer: req.body.customerId,
    items: [{ price: req.body.priceId }],
    expand: ['latest_invoice.payment_intent'],
  });

  // do something with the subscription

} else {
  const pi = await stripe.paymentIntents.create({
    customer: req.body.customerId,
    currency: 'usd',
    amount: price.unit_amount,
  });
 
  // do something with the PaymentIntent
}
Run Code Online (Sandbox Code Playgroud)