首页 > 解决方案 > 使用不同卡为现有客户创建费用

问题描述

所以我有一个客户已经创建了一张卡。
在前端,我提供了使用现有卡或其他卡的选项。
按照 API 文档,对于新卡,我创建令牌,将其发送到我的后端......

在后端:

const paymentInfo = {
  customer: customerId,
  amount: Number(total) * 100,
  currency: 'usd',
  source: existingCardId || token
}

const charge = await stripe.charges.create(paymentInfo)

如果我使用现有卡付款,则费用会通过,但如果我发送新令牌,则会收到错误消息:

Customer cus_G4V0KvxKMmln01 does not have a linked source with ID tok_1FYMLTAOg97eusNI2drudzlJ.

来自 API 文档: https ://stripe.com/docs/api/charges/create

来源 可选 要收取的付款来源。这可以是卡(即信用卡或借记卡)、银行账户、来源、令牌或关联账户的 ID。对于某些来源(即卡、银行帐户和附加来源),您还必须传递关联客户的 ID。

标签: node.jsstripe-payments

解决方案


我找到了解决方案:

if (token) {
  const card = await stripe.customers.createSource(customerId, {
    source: token
  })
  paymentInfo.source = card.id
}

推荐阅读