Ruby on rails Stripe - set default amount to shopping cart subtotal -
i trying connect shopping cart stripe , set amount order subtotal of cart. tried defining order_subtotal in orders model , tried pass though amount field in stripe code following error when go through check out: invalid integer: order_subtotal
i cant find online explains how connect amount varies stripe using ruby language. 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 %> <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 |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <% if @order_items.size == 0 %> <p class="text-center"> there no items in shopping cart. please <%= link_to "go back", root_path %> , add items cart. </p> <% else %> <% @order_items.each |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
in charges_controller.rb
replace
:amount => :order_subtotal
with
:amount => order_subtotal
you adding symbol instead of result of order_subtotal
method.
also in cart/show.html.erb want
<span><%= order_subtotal %></span>
instead of
<span>:order_subtotal</span>
about line:
<p class="text-center">order subtotal=<%= order_subtotal= @order_items.sum(:total_price)%></p>
since seem not have order instance when cart being charged for, cannot access order#order_subtotal
method. it's therefore best have helper method cart_subtotal
calculate displayed values instead of doing right in template.
Comments
Post a Comment