Codestudio Codestudio - 1 year ago 70
Ruby Question

Stripe Payment Amount

I have integrated stripe payments into my application. It is configured in a way so that the user can input the price for an item they want to sell as a decimal on a pin model. When the price is inputed in the form it is displayed as a dollar amount in the view. However, when you select the buy now button for that item and the stripe payments modal pops-up it displays the price in cents (i.e. I input "10.0" and submit it so it displays "Price $: 10.0 but when I select buy now Stripe interprets it as cents and displays "Pay $.10" in the stripe payment modal.)

I thought about changing the input price to cents for users that way Stripe could interpret it better but that would result in a bad UI.

Is there any simple way to fix this so that the amount displayed is uniform for both the input and output?


<%= form_tag charges_path, id: 'chargesForm' do %>
<script src=""></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> Buy Now!</button>

var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value =;
document.getElementById("stripeEmail").value =;
document.getElementById('btn-buy').addEventListener('click', function(e) {{
name: <%= @pin.manufacturer %>',
description: '<%= @pin.description %>',
amount: '<%= @pin.price %>'
<% end %>


<div id="pins" class="transitions-enabled">
<% @pins.each do |pin| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= link_to (image_tag pin.image.url(:medium)), pin %>
<%= pin.manufacturer %>
<%= pin.price %>
<%= pin.description %>
<% if pin.is_multi? %>
<%= pin.quantity %>
<% end %>


class AddPriceToPins < ActiveRecord::Migration
def change
add_column :pins, :price, :decimal


class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy, :bid]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 9)

def pin_params
params.require(:pin).permit(:description, :price, :image, :image2, :image3, :image4, :image5, :manufacturer, :model)

Answer Source

Stripe's API always expects you to pass amounts in the smallest currency unit. In USD it means passing the amount as cents.

You don't need to change your UI here, you simply need to change your code to pass the amount as cents when you call Since you have the amount already in @pin.price you'll want to multiply it by 100 and round the value:{
  name: '<%= @pin.manufacturer %>',
  description: '<%= @pin.description %>',
  amount: '<%= (@pin.price * 100).floor %>'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download