0

我需要为具有电子邮件参数的用户调用方法。它是在 PayPal 中支付的功能,我正在设置返回 url 和取消 url。

这是我的方法,在 user.rb 中:

   def pay email
require 'httpclient'
require 'xmlsimple'
clnt = HTTPClient.new
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"=> 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}"
 end

我得到错误:

      undefined local variable or method `root_path'

为什么我不能使用这种方法?为什么我不能使用 redirect_to ?

4

1 回答 1

2

我不认为 root_path 或 root_url 在模型类中默认可用。这是设计使然,因为您的模型不应该了解控制器/视图/类似会话的操作。您可以通过在模型中包含它来访问 root_url:

include Rails.application.routes.url_helpers

更好的解决方案是将此信息传递给 order.pay 方法。

于 2012-07-19T15:54:27.883 回答