2

我正在关注Ryan Bates 的 railcast 146,它真的很有帮助。但是,我试图从流程中删除购物车对象,并单独处理订单。我遇到的问题是如何确定使用两次的金额:一次设置购买,然后一次实际执行。这是我采取的做法,但它暴露了 return_url 中的数量,我认为这可能是不好的做法:

class OrdersController < ApplicationController
  def express
    response = EXPRESS_GATEWAY.setup_purchase(params[:amount],
      :ip                => request.remote_ip,
      :return_url        => new_order_url(:amount=>params[:amount]),
      :cancel_return_url => root_url
    )
    redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
  end

  def new
    @order = Order.new(:express_token => params[:token], :price_in_cents=>params[:amount])
  end

然后在视图中,我添加了一个带有金额的隐藏字段,以便在创建订单时它具有内置的金额(我在订单模型中添加了一个 price_in_cents 字段)。它工作正常,但将金额作为参数公开可能有点不确定。最后,购买代码如下所示:

 def purchase
    response = process_purchase
    transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
    cart.update_attribute(:purchased_at, Time.now) if response.success?
    response.success?
  end

简而言之,如何在不传递参数中的金额的情况下做到这一点?

谢谢!

4

2 回答 2

0

感谢您的输入。我最终将金额存储在用户的会话中,例如session[:amount],然后nil在他们完成该过程后立即将其设置为。这样它就对用户隐藏了,并且省去了创建新对象或加密的麻烦。

于 2011-01-10T18:41:28.317 回答
0

在 url 中发送金额是一种非常糟糕的做法 - 它允许人们更改价格并以他在 URL 中指定的金额购买您所出售的任何东西。

我可以看到解决此问题的两种方法:
1. 您可以加密您传递的参数,并从 URL 解密它们。在此处查看如何加密
2. 您可以创建一个新实体来保存购买价格(或者如果您正在出售特定商品(因为您没有使用购物车) - 您可以传递该商品的 id 并查询因为它的价格,当你需要它)。像这样的东西:

class OrdersController < ApplicationController
  def express
    @product = Product.find(params(:product_id));
    response = EXPRESS_GATEWAY.setup_purchase(product.price_in_cents,
      :ip                => request.remote_ip,
      :return_url        => new_order_url(product.price_in_cents),
      :cancel_return_url => root_url
    )
    redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
  end

  def new
    @product = Product.find(params(:product_id));
    @order = Order.new(:express_token => params[:token], :price_in_cents=> @product.price_in_cents)
  end

于 2011-01-10T13:06:53.780 回答