ruby-on-rails - Paypal Express ActiveMerchant 集成

标签 ruby-on-rails paypal activemerchant railscasts

我正在关注 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/

相关文章:

ruby-on-rails - 将 float 格式化为最小小数位数

c# - Paypal API,HttpWebRequest 抛出 SSL WebException

activemerchant - 使用 Payflow Pro 重复充电

javascript - 登录sharetribe后sharetribe主页错误

ruby-on-rails - 充当偏执狂 : really destroy record that has paranoid children

ruby-on-rails - 将 ruby​​ 版本更新到 3.0.2 后,Rails 生成迁移不起作用

ruby-on-rails - PayPal 错误 : This transaction is invalid. 请返回收件人的网站以使用他们的常规结帐流程完成您的交易

paypal - 哪个基于 API 的支付提供商最适合众筹模式?

javascript - 如何在 PayPal 智能按钮交易中发送自定义数据字段

ruby-on-rails - 使用 Moneris Canada 在活跃商家中定期计费