Kris Kris - 4 months ago 41
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!

charges_controller.rb

class ChargesController < ApplicationController
def new
end

def create
# Amount in cents
@amount = :order_subtotal


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

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

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


cart/show.html.erb

<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>
</div>
<% end %>
<label class="amount">
<span>:order_subtotal</span>
</label> </article>

<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"
> </script>
<% end %> </div>


_shopping_cart.html.erb

<% if !@order_item.nil? && @order_item.errors.any? %>
<div class="alert alert-danger">
<ul>
<% @order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% 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.
</p>
<% 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 %>


order.rb

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
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

Answer

In your charges_controller.rb replace

:amount => :order_subtotal

with

: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

<span>:order_subtotal</span>

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.