简体   繁体   中英

How can I allow Stripe to accept whole and decimal values on rails?

Implemented Stripe on aa rails project. Stripe takes amount of '500' and sends 5.00 to their API by default. If I do amount.to_i*100 I can get '500' to stay 500, but then when amount = 5.67 getting 5.00 still.

How can I change the controller, view or field to allow for both correctly?

CONTROLLER CODE

@amount = params[:amount].to_i*100

VIEW CODE

<input class="form-field two-decimals" name="amount" type="number" step="any" >

您可以这样做:

@amount = sprintf("%.2f",(params[:amount])).delete '.'

Stripe takes only integer, to get proper integer you can multiply amount to 100.

(@amount * 100).to_i => 44.53 * 100 = 4453.0.to_i and converting to integer 4453

def charge
    begin
        charged = Stripe::Charge.create(
          :amount   => (@amount * 100).to_i,
          :currency => "usd",
          :customer => @customer_id
        )
    rescue Stripe::InvalidRequestError, Stripe::AuthenticationError, Stripe::APIConnectionError, Stripe::StripeError => e
        puts e.message
        return false
    end
    charged
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM