Sylar Sylar - 7 months ago 53
Ruby Question

Stripe Rails: Invalid integer: 1.06

I can't get my head around this one.

My form passes a params of

106
which is
£1.06
.

enter image description here

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
.

Answer

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