Stripe Rails: Invalid integer: 1.06

邮差的信 提交于 2020-01-04 06:03:09

问题


I can't get my head around this one.

My form passes a params of 106 which is £1.06.

Charging the card:

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

How to prevent:

Invalid integer: 1.06

Where does the integer comes from? I have converted that to float.


回答1:


According to Stripe API Reference amount parameter should be integer and it is perceived as cents. So you should pass params[:amount] directly as 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


来源:https://stackoverflow.com/questions/36837952/stripe-rails-invalid-integer-1-06

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!