我正在关注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简而言之,我如何才能在不传递参数数量的情况下做到这一点?
谢谢!
发布于 2011-01-11 02:41:28
感谢你们的投入。我最终将数量存储在用户的会话中,类似于session[:amount],然后在他们完成该过程后立即将其设置为nil。这样它就对用户隐藏了,省去了我创建新对象或加密的麻烦。
发布于 2011-01-10 21:06:54
在URL中发送数量是非常糟糕的做法-它允许一个人改变价格,并以他在url中指定的数量购买您正在销售的任何东西。
我可以看到两种解决这个问题的方法:
你可以对你传递的参数进行加密,并从中解密它们。看看如何加密here
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
https://stackoverflow.com/questions/4567695
复制相似问题