1

ホストされている支払いページ( http://support.cheddargetter.com/kb/hosted-payment-pages/hosted-payment-pages-setup-guide )を使用して、RubyonRailsアプリケーションをCheddarGetterと統合しようとしています。

最後の部分を除いて、ほとんどすべてを理解しました。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

しかし、私はあまりよくわかりません..?if active? elsif cancelled? && pending?CheddarGetter APIが提供する応答を参照して、そこにもコードが必要だと思います。

いくつかの方向性をいただければ幸いです、ありがとう。

4

1 に答える 1

1

ディレクトリ内の独自のモジュールに配置し、アクセスしようとしている Web サイトが利用できない場合に備えて/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

cancelledなどの状態の他のロジックを追加しpending、顧客情報を新しいモジュール メソッドに渡します。さまざまな状態を処理するために switch ステートメントを使用したいだけかもしれません。


アップデート

また、config/application.rbファイルにこれがまだない場合は、必ず含めて、libフォルダーを自動ロードパスに追加してください。

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
于 2011-12-22T16:50:43.320 に答える