3

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.

Sylar
  • 11,422
  • 25
  • 93
  • 166
  • I can't say exactly what's happened, without more information.... But it looks like somewhere, your code is expecting `amount` to be an `Integer` (and you're giving it a `Float`). My first guess is that in the `Stripe::Charge` model, you have a rails validation, which checks that the value is an integer? Or maybe the database table column type is `integer`? – Tom Lord Apr 25 '16 at 10:34
  • If I remove the `.to_f`, stripe will charge £106 instead of £1.06. My js is ok as the params it passes is `106`. I'll restart this server in a bit. – Sylar Apr 25 '16 at 10:37
  • Could you please post the params[:amount] value – Sambit Apr 25 '16 at 10:41

1 Answers1

6

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
Maxim Pontyushenko
  • 2,983
  • 2
  • 25
  • 36