Kris Kris - 1 year ago 133
Ruby Question

Ruby on rails Stripe - set default amount to shopping cart subtotal

I am trying to connect my shopping cart to Stripe and set the amount to the order subtotal of my cart. I tried defining order_subtotal in my orders model and tried to pass it though the amount field in my stripe code but I get the following error when I go through check out: Invalid integer: order_subtotal

I cant find any online that explains how to connect an amount that varies to stripe using the ruby language. Any help would be appreciated, Thanks!


class ChargesController < ApplicationController
def new

def create
# Amount in cents
@amount = :order_subtotal

customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]

charge = Stripe::Charge.create(
:customer =>,
:amount => :order_subtotal,
:description => 'Rails Stripe customer',
:currency => 'usd'

rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end end


<div class="shopping-cart"> <%= render "shopping_cart" %> <%= form_tag charges_path do %> <article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
<% end %>
<label class="amount">
</label> </article>

<script src="" class="stripe-button"
data-amount= "amount"
> </script>
<% end %> </div>


<% if !@order_item.nil? && @order_item.errors.any? %>
<div class="alert alert-danger">
<% @order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
<% if @order_items.size == 0 %>
<p class="text-center">
There are no items in your shopping cart. Please <%= link_to "go back", root_path %> and add some items to your cart.
<% else %>
<% @order_items.each do |order_item| %>
<%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %>
<% end %>
<p class="text-center">Order SubTotal=<%= order_subtotal= @order_items.sum(:total_price)%></p>

<% end %>


class Order < ActiveRecord::Base
belongs_to :order_status
has_many :order_items
before_create :set_order_status
before_save :update_subtotal

def subtotal
order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0}.sum
def order_subtotal
@order_items.sum(:total_price) end end private def set_order_status
self.order_status_id = 1 end

def update_subtotal
self[:subtotal] = subtotal end

Answer Source

In your charges_controller.rb replace

:amount => :order_subtotal


:amount => order_subtotal

You are adding a symbol instead of the result of the order_subtotal method.

Also in your cart/show.html.erb you probably want

<span><%= order_subtotal %></span>

instead of


About this line:

<p class="text-center">Order SubTotal=<%= order_subtotal= @order_items.sum(:total_price)%></p>

Since you seem not to have an order instance when your cart is being charged for, you cannot access Order#order_subtotal method. It's therefore best to have a helper method like cart_subtotal to calculate displayed values instead of doing that right in the template.