0

我需要传递 user_id,不是登录用户,而是视图中的 user_id。

我正在写管理部分。

我正在使用用户电子邮件发出发布请求并重定向到适当的页面。所以我想让路线类似于:

     /:user_id/pay

为适当的用户调用付费操作。

在我看来,我想显示电子邮件和指向此操作的链接:

<% @users.each do |user| %>
 <li>
  <%= user.email %>
  <%= link_to "Pay", some_controller_pay(user.id) %>
 </li>

编辑

    def pay
    require 'httpclient'
    require 'xmlsimple'
    clnt = HTTPClient.new
    user = User.find(params[:user_id])
    credentials = {
        'USER' => 'payer_1342623102_biz_api1.gmail.com',
       'PWD' => '1342623141',
       'SIGNATURE' => 'Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL '
     }

    header =  {"X-PAYPAL-SECURITY-USERID" => "payer_1342623102_biz_api1.gmail.com",
                   "X-PAYPAL-SECURITY-PASSWORD" => "1342623141",
                   "X-PAYPAL-SECURITY-SIGNATURE" => "Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL ",
                   "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
                   "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
                   "X-PAYPAL-APPLICATION-ID" =>  "APP-80W284485P519543T"
                    }
    data = {"actionType" => "PAY",
               "receiverList.receiver(0).email"=> user.email,
               "receiverList.receiver(0).amount" => "10",
               "currencyCode" => "USD",
               "cancelUrl" => root_path,
               "returnUrl" => root_path,
               "requestEnvelope.errorLanguage" => "en_US"}
    uri = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"
    res = clnt.post(uri, data, header)
    @xml = XmlSimple.xml_in(res.content)
    @payKey = @xml["payKey"].to_s()
    @payKey = @payKey.tr("[]", "")
    @payKey = @payKey[1..20]
    redirect_to "https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=#{@payKey}"

结束 PayPal 说发生了一些错误。

我尝试检查变量,所以我应该考虑:

 <%= @xml%>
 <%= payKey%>

但我不能工作,我怎么能检查他们?

在开发日志中:

  Redirected to https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=
  Completed 302 Found in 1193ms (ActiveRecord: 1.0ms)

所以它不插入链接,是吗?我如何获得所有响应信息

我怎么能做到这一点?

4

2 回答 2

2

由于您已经有一个User模型,您可以添加pay另一个RESTful 操作

resources :users do
  member do
    get 'pay' # or post/put, whatever is appropriate
  end
end

这会自动设置一个命名路由pay_user

pay_user GET    /users/:id/pay(.:format)  users#pay

包括一个pay_user_path帮助你的意见:

<%= link_to "Pay", pay_user_path(user.id) %>

在您的UserControllerthis 中,它与任何其他操作一样工作:

def pay
  @user = User.find(params[:id])
  # do something with @user
end
于 2012-07-20T07:39:08.900 回答
1

路线.rb

match "/:user_id/pay" => "some_controller#pay", :as => "some_controller_pay"

some_controller.rb

def pay
  user = User.find(params[:user_id])
  ....
于 2012-07-20T07:30:27.520 回答