我正在关注 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
然后在 View 中,我添加了一个包含金额的隐藏字段,以便在创建订单时它具有内置金额(我向订单模型添加了一个 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
简而言之,如何在不传递参数中的金额的情况下执行此操作?
谢谢!
最佳答案
在 url 中发送金额是一种非常糟糕的做法 - 它允许一个人更改价格并以他在 URL 中指定的金额购买您出售的任何东西。
我可以看到两种解决此问题的方法:
1、可以对传递的参数进行加密,从URL中解密。查看如何加密 here
2. 您可以创建一个新实体来保存购买价格(或者如果您正在销售特定商品(因为您没有使用购物车))——您可以传递该商品的 ID 并在您购买时查询它的价格需要它)。是这样的:
类 OrdersController < ApplicationController
def express
@product = Product.find(params(:product_id));
响应 = 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)
结尾
定义新
@product = Product.find(params(:product_id));
@order = Order.new(:express_token => params[:token], :price_in_cents=> @product.price_in_cents)
结尾
关于ruby-on-rails - Paypal Express ActiveMerchant 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4567695/