Stripe Rails:无效的整数:1.06

Syl*_*lar 2 ruby ruby-on-rails stripe-payments

我无法理解这一个.

我的形式通过一则params的106£1.06.

在此输入图像描述

充电卡:

amount = params[:amount].to_f
begin
  charge = Stripe::Charge.create(
    :amount => amount / 100,
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )
rescue Stripe::CardError => e
  # The card has been declined
end
Run Code Online (Sandbox Code Playgroud)

如何预防:

无效的整数:1.06

整数从哪里来?我把它转换成了float.

Max*_*nko 5

根据Stripe API Reference,参数应该是整数,并且它被视为美分.所以你应该params[:amount]直接传递金额.

begin
  charge = Stripe::Charge.create(
    :amount => params[:amount],
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )
rescue Stripe::CardError => e
  # The card has been declined
end
Run Code Online (Sandbox Code Playgroud)