Class: Omnipay::Gateway

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

Overview

Instance of a gateway connection. Has an uid, and encapsulates an adapter strategy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Gateway

The options hash must include the following keys :

- :uid => the gateways uid
- :adapter => the adapter class

The options hash may also include the following keys :

- :config => the configration hash passed to the adapter for its initialization

Parameters:

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

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/omnipay/gateway.rb', line 16

def initialize(opts = {})
  @uid            = opts[:uid]
  @adapter_class  = opts[:adapter]
  @config         = opts[:config] || {}

  raise ArgumentError.new("missing parameter :uid") unless @uid
  raise ArgumentError.new("missing parameter :adapter") unless @adapter_class

  @adapter = @adapter_class.new(@config)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/omnipay/gateway.rb', line 6

def adapter
  @adapter
end

#adapter_classObject (readonly)

Returns the value of attribute adapter_class.



6
7
8
# File 'lib/omnipay/gateway.rb', line 6

def adapter_class
  @adapter_class
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/omnipay/gateway.rb', line 6

def config
  @config
end

#uidObject (readonly)

Returns the value of attribute uid.



6
7
8
# File 'lib/omnipay/gateway.rb', line 6

def uid
  @uid
end

Instance Method Details

#callback_hash(request) ⇒ Object



65
66
67
# File 'lib/omnipay/gateway.rb', line 65

def callback_hash(request)
  @adapter.callback_hash(request).merge(:raw => request.params)
end

#ipn_enabled?Boolean

Is IPN enabled?

Returns:

  • (Boolean)


71
72
73
# File 'lib/omnipay/gateway.rb', line 71

def ipn_enabled?
  @adapter_class.ipn?
end

#ipn_hash(request) ⇒ Hash

The formatted response hashes

Returns:

  • (Hash)

    the processed response which will be present in the request environement under ‘omnipay.response’



61
62
63
# File 'lib/omnipay/gateway.rb', line 61

def ipn_hash(request)
  @adapter.ipn_hash(request).merge(:raw => request.params)
end

#payment_redirection(opts = {}) ⇒ Rack::Response

The Rack::Response corresponding to the redirection to the payment page The options hash must contain the following keys :

  • :base_uri : the current http scheme + host (used in the post-payment redirection)

  • :amount [Integer] : the amount to pay, in cents

Depending on the adapter used, the options hash may have other mandatory keys. Refer to the adapter’s documentation for more details

Parameters:

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

    The attributes of the current payment. Will be passed on to the adapter.

Returns:

  • (Rack::Response)

    the GET or POST redirection to the payment provider

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omnipay/gateway.rb', line 35

def payment_redirection(opts = {})
  base_uri = get_base_uri(opts)
  raise ArgumentError.new('Missing parameter :base_uri') unless base_uri

  ipn_url      = "#{base_uri}#{Omnipay.configuration.base_path}/#{uid}/ipn"
  callback_url = "#{base_uri}#{Omnipay.configuration.base_path}/#{uid}/callback"

  method, url, params = @adapter.request_phase(opts, ipn_url, callback_url)

  if method == 'GET'
    redirect_url = url + (url.include?('?') ? '&' : '?') + Rack::Utils.build_query(params)
    Rack::Response.new.tap{|response| response.redirect(redirect_url)}

  elsif method == 'POST'
    form = AutosubmitForm.new(url, params)
    Rack::Response.new([form.html], 200, {'Content-Type' => 'text/html;charset=utf-8'})

  else
    raise ArgumentError.new('the returned method is neither GET nor POST')

  end
end