Ruby on rails Stripe - 将默认金额设置为购物车小计

时间:2016-07-15 19:46:02

标签: ruby-on-rails ruby stripe-payments

我正在尝试将我的购物车连接到Stripe,并将金额设置为购物车的订单小计。我尝试在我的订单模型中定义order_subtotal,并尝试通过条带代码中的金额字段传递它,但是当我检查时出现以下错误:无效的整数:order_subtotal

我无法找到任何在线解释如何使用ruby语言连接不同条带的金额。任何帮助将不胜感激,谢谢!

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

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

1 个答案:

答案 0 :(得分:0)

charges_controller.rb替换

:amount => :order_subtotal

:amount => order_subtotal

您正在添加符号,而不是order_subtotal方法的结果。

同样在 cart / show.html.erb 中,您可能需要

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

而不是

<span>:order_subtotal</span>

关于这一行:

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

由于您的购物车收费时似乎没有订单实例,因此您无法访问Order#order_subtotal方法。因此,最好使用cart_subtotal之类的辅助方法来计算显示值,而不是在模板中正确执行此操作。