您不能多次使用Stripe令牌

时间:2016-04-27 12:47:53

标签: ruby-on-rails stripe-payments

我似乎无法收取卡,然后在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 thistoken.id没有token只是String

1 个答案:

答案 0 :(得分:0)

看起来您在两个位置使用令牌:

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

还在这里:

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

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

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

相关条纹文档: 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
)