Class: Omnipay::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/omnipay/gateway.rb

Overview

This is the actual Rack middleware

It is associated with an adapter instance, and responsible for monitoring the incoming request and - depending on their path - forwarding them for processing to a RequestPhase or CallbackPhase instance for processing

Constant Summary collapse

BASE_PATH =
'/pay'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ Gateway

The gateway may be initialized with a block returning a hash instead of a hash. In this case, the block’s only argument is the uid, and the returned hash must contain the adapter class and its configuration

Parameters:

  • app (Rack application)

    : The rack app it should forward to if the request doens’t match a monitored path

  • options (Hash) (defaults to: {})

    : must contains the following configuration options

    • :uid : the subpath to monitor

    • :adapter : the adapter class to use

    • :config : the config hash which will be passed at the adapter for initialization



26
27
28
29
30
31
32
33
34
35
# File 'lib/omnipay/gateway.rb', line 26

def initialize(app, options={}, &block)
  @app = app

  @adapter_options = options
  @adapter_config_block = block

  # Refreshed at each request
  @uid = nil
  @request = nil
end

Instance Method Details

#call(env) ⇒ Object

The standard rack middleware call. Will be processed by an instance of RequestPhase or CallbackPhase if the path matches the adapter’s uid. Will forward to the app otherwise



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/omnipay/gateway.rb', line 39

def call(env)

  # Get the current request
  @request = Rack::Request.new(env)

  # Check if the path is good, and extract the uid
  @uid = extract_uid_from_path(@request.path)
  return @app.call(env) unless @uid

  # Get the adapter config for this uid (to handle dynamic configuration)
  adapter = Adapter.new(@uid, callback_url, @adapter_options, @adapter_config_block)
  return @app.call(env) unless adapter.valid?

  # Handle the request phase
  if request_phase?
    return RequestPhase.new(@request, adapter).response
  end

  # Handle the callback phase
  if callback_phase?
    CallbackPhase.new(@request, adapter).update_env!
  end

  # Forward to the app
  @app.call(env)

end