Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 935
Sylar Avatar asked May 29 '26 22:05

Sylar


1 Answers

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
like image 121
Maxim Pontyushenko Avatar answered Jun 01 '26 21:06

Maxim Pontyushenko