【问题标题】:You cannot use a Stripe token more than once您不能多次使用 Stripe 令牌
【发布时间】:2016-08-21 18:42:13
【问题描述】:

我似乎无法在 Rails 4 中对卡进行收费然后动态创建客户。

def charge
 token = params[:stripeToken] # can only be used once.
 begin
  charge = Stripe::Charge.create(
    :amount => 5000,
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )
 rescue Stripe::CardError => e
  # The card has been declined
 end

 if current_user.stripeid == nil
  customer = Stripe::Customer.create(card: token, ...)
  current_user.stripeid = customer.id
  current_user.save
 end
end

I have looked at this 但没有token.id 这样的东西,因为token 只是一个String

【问题讨论】:

  • 而且您没有在 JS 代码的任何地方提交令牌(即创建费用)?
  • 我正在使用 Checkout.js。

标签: ruby-on-rails stripe-payments


【解决方案1】:

您似乎在两个位置使用令牌:

charge = Stripe::Charge.create(
    :amount => 5000,
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )

还有这里:

customer = Stripe::Customer.create(card: token, ...)

事实上,从令牌创建 Stripe 费用还应该与卡一起创建一个客户,如果它不存在的话。您创建客户的步骤是不必要的。因此,只需从源中获取 Stripe 客户:

current_user.update_attribute(:stripeid, charge.source.customer)

相关的 Stripe 文档: https://stripe.com/docs/api/ruby#create_charge

编辑

如果您想更好地控制充电过程,请独立创建每个对象:

customer = Stripe::Customer.create(
  description: "Example customer",
  email: current_user.email
)

card = customer.sources.create(
  source: "<stripe token>"
  customer: customer.id
)

Stripe::Charge.create(
  amount: 5000,
  currency: "gbp",
  source: card.id,
  customer: customer.id
)

【讨论】:

  • 很酷,但你是说我不需要条纹客户吗?我从customer.id 得到这个undefined method id for nil
  • 奇怪,对我来说不是零。您使用的是最新的 Stripe API 吗?
  • 没有。让我更新看看。但我必须有一个条纹客户来跟踪他们的支出。
  • 然后分步创建客户、卡和费用。有关示例,请参阅我的更新答案。
  • 是的,就是这样。如果没有,我必须有一个条纹客户。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-10-31
  • 2018-07-09
  • 2022-11-21
  • 2015-08-25
  • 2016-10-15
  • 1970-01-01
  • 2016-10-21
  • 2022-10-31
相关资源
最近更新 更多