2011-12-22 2 views
1

Ruby on Rails 응용 프로그램을 호스트 된 지불 페이지 (http://support.cheddargetter.com/kb/hosted-payment-pages/hosted-payment-pages-setup-guide)를 사용하여 CheddarGetter와 통합하려고합니다.사용자가 로그인하기 전에 Net :: HTTP를 사용하여 API에서 데이터를 요청 하시겠습니까?

저는 고객이 시스템에 로그인하기 전에 고객이 계속 활성화되어 있는지 확인하기 위해 고객 데이터를 API와 비교하여 마지막 부분을 제외한 모든 것을 알아 냈습니다.

분명히 그것은 일종의 HTTP 요청과 관련이 있습니다. 솔직히 말해서 나는 익숙하지 않습니다. 미안합니다. 코드는 다음과 같습니다.

uri = URI.parse("https://yoursite.chargevault.com/status?key=a1b2c3d4e6&code=yourcustomercode") 
http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
request = Net::HTTP::Get.new(uri.request_uri) 
status = http.request(request).body 

정확히이 코드를 어디에 넣어야합니까?

나는 내 user_session.rb 모델에 다음을 넣어 생각하고 있어요 :

class UserSession < Authlogic::Session::Base 
    before_create :check_status 

    private 
    def check status 
     # insert above code here 
    end 
end 

하지만 너무 확실하지 않다 ..? 또한 ..

어떤 방향으로, 감사 감사하겠습니다이 ..

답변

1

나는 퍼팅 추천 할 것 CheddarGetter의 API는 당신에게 줄 것이다 응답을 언급, 거기에 약간의 if active? elsif cancelled? && pending? 코드이 있어야한다 생각 그 자체에 그 모듈을 /lib 디렉토리에두고 액세스하려는 웹 사이트를 사용할 수없는 경우에도 Timeout에 전화를 겁니다. 아래에서 일반적인 예를 만들었으므로 필요에 따라 시간을 조정할 수 있습니다. /lib/customer_status.rb

require 'timeout' 
module CustomerStatus 
    class << self 
    def get_status 
     begin 
     Timeout::timeout(30) { 
      uri = URI.parse("https://yoursite.chargevault.com/status?key=a1b2c3d4e6&code=yourcustomercode") 
      http = Net::HTTP.new(uri.host, uri.port) 
      http.use_ssl = true 
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
      request = Net::HTTP::Get.new(uri.request_uri) 
      status = http.request(request).body 
     } # end timeout 

     return status 

     rescue Exception => e 
     # This will catch a timeout error or any other network connectivity error 
     Rails.logger.error "We had an error getting the customer's status: #{e}" 
     end 
    end 
    end 
end 

내부

그럼 그냥 같이 호출 할 수 있습니다

class UserSession < Authlogic::Session::Base 
    # include the new module we added 
    include CustomerStatus 

    before_create :check_status 

    private 
    def check status 
    raise someError unless (CustomerStatus.get_status.include?("active")) 
    end 
end 

난 당신이, pendingcancelled의 다른 로직에 추가 할 수 있습니다 고객 정보를 새 모듈 메소드에 전달하는 것뿐입니다. 다른 상태를 처리하기 위해 switch 문을 사용하고자 할 수 있습니다.

module YourAppNameGoesHere 
    class Application < Rails::Application 

    # Custom directories with classes and modules you want to be autoloadable. 
    config.autoload_paths += %W(#{config.root}/lib) 

    end 
end 
: 이미 config/application.rb 파일이없는 경우


또한 업데이트

, 그것은 자동로드 경로에 lib 폴더를 추가, 그래서 당신이 그것을 포함해야합니다

+0

매우 상세한 답변을 쓸 시간을 내 주셔서 대단히 감사드립니다. 내 응용 프로그램에서 코드가 작동하는지 확인하기 위해 노력했지만 "초기화되지 않은 상수 인 UserSession :: CustomerStatus"오류가 발생했습니다. 뭐가 잘못됐다고 생각하니? – cdotfeli

+0

Rails 서버를 다시 시작 했습니까? – iwasrobbed

+0

또한 업데이트 된 답변을 확인하여 레일즈 자동로드 경로에'lib' 폴더를 추가하여 시작시 해당 모듈을로드 할 수 있도록하십시오. – iwasrobbed

관련 문제