[英]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. 我试图将购物车连接到Stripe,并将金额设置为购物车的订单小计。 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 我尝试在订单模型中定义order_subtotal,并尝试通过条码中的amount字段传递它,但在结帐时出现以下错误:无效的整数: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 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 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 _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 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 your charges_controller.rb
replace 在您的charges_controller.rb
替换
:amount => :order_subtotal
with 与
:amount => order_subtotal
You are adding a symbol instead of the result of the order_subtotal
method. 您要添加符号而不是order_subtotal
方法的结果。
Also in your cart/show.html.erb you probably want 同样在您的cart / show.html.erb中,您可能想要
<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. 由于在为购物车Order#order_subtotal
时似乎没有订单实例,因此无法访问Order#order_subtotal
方法。 It's therefore best to have a helper method like cart_subtotal
to calculate displayed values instead of doing that right in the template. 因此,最好是使用诸如cart_subtotal
类的辅助方法来计算显示的值,而不是直接在模板中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.