title: Ruby on rails Stripe - set default amount to shopping cart subtotal
body: <p>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</p>
  
  <p>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!</p>
  
  <p>charges_controller.rb</p>
  
  <pre><code>class ChargesController &lt; ApplicationController
      def new
      end
  
      def create
        # Amount in cents
         @amount = :order_subtotal
  
  
        customer = Stripe::Customer.create(
          :email =&gt; params[:stripeEmail],
          :source  =&gt; params[:stripeToken]
        )
  
        charge = Stripe::Charge.create(
          :customer    =&gt; customer.id,
          :amount      =&gt;  :order_subtotal,
          :description =&gt; 'Rails Stripe customer',
          :currency    =&gt; 'usd'
        )
  
      rescue Stripe::CardError =&gt; e
        flash[:error] = e.message
        redirect_to new_charge_path
      end end
  </code></pre>
  
  <p>cart/show.html.erb</p>
  
  <pre><code>&lt;div class="shopping-cart"&gt;   &lt;%= render "shopping_cart" %&gt; &lt;%= form_tag charges_path do %&gt;   &lt;article&gt;
      &lt;% if flash[:error].present? %&gt;
        &lt;div id="error_explanation"&gt;
          &lt;p&gt;&lt;%= flash[:error] %&gt;&lt;/p&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
      &lt;label class="amount"&gt;
        &lt;span&gt;:order_subtotal&lt;/span&gt;
      &lt;/label&gt;   &lt;/article&gt;
  
    &lt;script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key=ENV[PUBLISHABLE_KEY]
            data-description="Checkout"
            data-amount= "amount"
            data-locale="auto"
            data-shipping-address="null"
            &gt;   &lt;/script&gt;
             &lt;% end %&gt; &lt;/div&gt;
  </code></pre>
  
  <p>_shopping_cart.html.erb</p>
  
  <pre><code>&lt;% if !@order_item.nil? &amp;&amp; @order_item.errors.any? %&gt;
    &lt;div class="alert alert-danger"&gt;
      &lt;ul&gt;
      &lt;% @order_item.errors.full_messages.each do |msg| %&gt;
        &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
      &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  &lt;% if @order_items.size == 0 %&gt;
    &lt;p class="text-center"&gt;
      There are no items in your shopping cart.  Please &lt;%= link_to "go back", root_path %&gt; and add some items to your cart.
    &lt;/p&gt;
  &lt;% else %&gt;
    &lt;% @order_items.each do |order_item| %&gt;
      &lt;%= render 'carts/cart_row', product: order_item.product, order_item: order_item, show_total: true %&gt;
    &lt;% end %&gt;
     &lt;p class="text-center"&gt;Order SubTotal=&lt;%= order_subtotal= @order_items.sum(:total_price)%&gt;&lt;/p&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>order.rb</p>
  
  <pre><code>class Order &lt; 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   
  end   
  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
  </code></pre>