Class: Omnipay::Middleware

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

Overview

This is the actual Rack middleware It is responsible for formatting callback responses via a CallbackPhase instance

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

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



15
16
17
# File 'lib/omnipay/middleware.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Rack::Reponse

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

Parameters:

  • env (Hash)

    the request’s environment

Returns:

  • (Rack::Reponse)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omnipay/middleware.rb', line 23

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 gateway for this uid
  gateway = Omnipay.gateways.find(uid)
  return @app.call(env) unless gateway

  # Handle the IPN phase
  return call_ipn(request, gateway) if ipn_phase?(request, uid)

  # Handle the callback phase
  if callback_phase?(request, uid)

    # If no IPN : send the ipn request before
    if !gateway.ipn_enabled?
      call_ipn(request, gateway, :force => true)
    end

    return call_callback(request, gateway)
  end

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

end