2

total_price中的所有产品cart都没有按应有的方式保存。无论实际总价是多少,它总是显示 1。这个问题是在安装货币 gemgoogle_currency gem之后出现的,所以我可以根据选择的语言环境更改产品本身的价格,而不仅仅是货币。

所以总价始终为 1,但同时我在购物过程中屏幕上显示的金额显示正确的数字,我的意思是如果我选择价格为 A 和 B 的 2 个产品,然后在购物车中(在屏幕上) 我有正确的 total_price = A+B .. 不仅如此,当我在填写信用卡字段后按下创建订单时,ActiveMerchant 将数据发送到贝宝,而贝宝的美元金额是我在购物车 A+B 中的总价格 ..

问题是:为什么应用程序获取权利total_price并将其发送到贝宝,但同时不能total_price在创建订单时将其存储在数据库中,而是保存 1。

当我创建订单时控制台会说什么:

   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `orders` (`card_expires_on`, `card_type`, `cart_id`, `created_at`, `first_name`, `ip_address`, `last_name`, `total`, `updated_at`, `user_id`) VALUES ('2012-04-01', 'visa', 5, '2012-04-18 21:35:38', 'Rosca', '127.0.0.1', 'Sergiu', 1, '2012-04-18 21:35:38', 7)

order_transaction详细信息:

SQL (0.5ms)  INSERT INTO `order_transactions` (`action`, `amount`, `authorization`, `created_at`, `message`, `order_id`, `params`, `success`, `updated_at`) VALUES ('purchase', 1, '0GT41652PP785722H', '2012-04-18 21:35:46', 'Success', 5, '--- \nbuild: \"2764190\"\nAck: Success\ntimestamp: \"2012-04-18T21:35:45Z\"\nTransactionID: 0GT41652PP785722H\namount: \"42.00\"\namount_currency_id: USD\ntransaction_id: 0GT41652PP785722H\nack: Success\nBuild: \"2764190\"\navs_code: X\nversion: \"62.0\"\nTimestamp: \"2012-04-18T21:35:45Z\"\nCorrelationID: 690aa904db3c\nAmount: \"42.00\"\nAVSCode: X\nVersion: \"62.0\"\ncvv2_code: M\nCVV2Code: M\ncorrelation_id: 690aa904db3c\n', 1, '2012-04-18 21:35:46')

关于如何创建订单的一些细节:

订单控制器.rb

  def create
    @order = current_cart.build_order(params[:order])
    @order.user_id = current_user.id
    @order.total = current_cart.total_price
#    raise current_cart.total_price.inspect
    @order.line_items = current_cart.line_items
    @order.ip_address = request.remote_ip
    if @order.save
      if @order.purchase
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
      else
        render :action => "failure"
      end
        respond_to do |format|
          format.html { redirect_to products_path, :notice => 
          'Thank you for your order.' }
          format.json { render :json => @order }
        end
    else
       render :action => 'new'
    end
  end

请注意,如果我在控制器中激活提升检查线,我会得到

#<Money cents:4200 currency:USD> 错误,在这种情况下,4200 是 2 个选定产品的 total_price。42.00 美元。这是应用程序需要将其存储在数据库中时,total_price 公式可以正常工作的又一证据。

总价:

total_pricecart.rbline_item.rb中形成

line_item.rb

  belongs_to :order
  belongs_to :product
  belongs_to :cart
  belongs_to :user

  def total_price
    product.price * quantity
  end

购物车.rb

has_many :line_items#, :dependent => :destroy
  has_one :order

  def to_s
    id
  end

  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(:product_id => product_id)
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

编辑

数据库中的order.total字段类型现在是整数(11),但我尝试使用小数(10,0)以美分存储价格,但无论如何它都不起作用,我总是得到 1 美元的总价。价格为products十进制(10,0)。

非常感谢您的帮助.. 可能对未来的开发人员有用。

您可能需要的任何其他信息,请告诉我。

4

1 回答 1

0

好吧,我将order.totalfrom字段更改decimalfloattype ,现在可以正常工作了。我有正确的金额发送到贝宝,以及为用户创建的订单中的正确金额。在使用之前,money gem它与decimal字段类型配合良好。

于 2012-04-19T11:58:34.630 回答