0

I need to call method for user, which has email parametr. It is function for paying in PayPal and I'm setting return url and cancel url.

Here is my method, in 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

and I get error:

      undefined local variable or method `root_path'

Why I can't use this method ? And why I can't use redirect_to ?

Denys Medynskyi
  • 2,353
  • 8
  • 39
  • 70

1 Answers1

2

I don't think root_path or root_url is available by default from within a model class. This is by design, since your model should not have knowledge of the controller/view/session-like operations. You can get access to the root_url by including this in the model though:

include Rails.application.routes.url_helpers

A better solution would be to pass this information into the order.pay method.

steakchaser
  • 5,198
  • 1
  • 26
  • 34
  • I put it in method, but it gave me :undefined method `include' for # – Denys Medynskyi Jul 19 '12 at 15:56
  • When I put it in Model it gives me - undefined method `redirect_to' – Denys Medynskyi Jul 19 '12 at 15:57
  • @DenMed I think you are confusing models and controllers. You can't redirect in a model, that is the responsibility of a controller. – Peter Brown Jul 19 '12 at 16:00
  • redirect_to is part of ActionController::Base, you really should not/can't being using this in a model. Is there a way you could restructure your code so that the model executes the call to paypal, but then the controller does the redirect based on the result of the call to paypal. – steakchaser Jul 19 '12 at 16:02
  • How I can get results of calling method in my controller ? – Denys Medynskyi Jul 19 '12 at 16:03