【问题标题】:Paypal Express ActiveMerchant integrationPaypal Express ActiveMerchant 集成
【发布时间】:2011-06-01 20:20:52
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: ruby-on-rails paypal activemerchant railscasts


    【解决方案1】:

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

    我可以看到解决此问题的两种方法:
    1.您可以对您传递的参数进行加密,并从 URL 中对其进行解密。看看如何加密here
    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-01-01
        • 2018-06-29
        • 2012-03-14
        • 2014-01-15
        • 2012-06-30
        • 2013-11-11
        • 2023-03-25
        • 2014-02-27
        • 2011-02-03
        相关资源
        最近更新 更多