Class: Omnipay::Adapter

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

Overview

Base adapter class Inherited by specific implementations

Defined Under Namespace

Classes: ConfigField

Constant Summary collapse

DEFAULT_ADAPTER_CONFIG =
{
  :sandbox => ConfigField.new('when enabled, the payment are not actually withdrawn', true, nil)
}
DEFAULT_PAYMENT_CONFIG =
{
  :amount       => ConfigField.new('The amount (in cents) to pay', nil, true),
  :reference    => ConfigField.new('The local reference of the payment', nil, true),
  :currency     => ConfigField.new('The ISO 4217 code for the currency to use', 'USD', true),
  :locale       => ConfigField.new('The ISO 639-1 locale code for the payment page', 'en', true),
  :title        => ConfigField.new('The title to display on the payment page', nil, false),
  :description  => ConfigField.new('A description to display on the payment page', nil, false),
  :template_url => ConfigField.new('The url for the template to use on the payment page', nil, false)
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Adapter

Actual adapter instance logic



94
95
96
97
98
99
# File 'lib/omnipay/adapter.rb', line 94

def initialize(config = {})
  payment_config = config.delete(:payment)

  @adapter_config = build_adapter_config(config)
  @payment_config = build_payment_config(payment_config)
end

Class Method Details

.adapter_configObject

Accessor to the adapter config



39
40
41
# File 'lib/omnipay/adapter.rb', line 39

def self.adapter_config
  @adapter_config ||= Omnipay::Helpers.deep_dup(DEFAULT_ADAPTER_CONFIG)
end

.config(name, explaination) ⇒ Object

Add a new config field to the adapter. Is mandatory and unfilled. If default value or optional => should be a payment config



46
47
48
# File 'lib/omnipay/adapter.rb', line 46

def self.config(name, explaination)
  adapter_config[name] = ConfigField.new(explaination, nil, true)
end

.custom_payment_param(name, explaination, opts = {}) ⇒ Object

Add a new field to the payment config Options are :mandatory (default false) and :default (default nil)



62
63
64
65
66
67
# File 'lib/omnipay/adapter.rb', line 62

def self.custom_payment_param(name, explaination, opts = {})
  if payment_config.has_key?(name)
    raise ArgumentError, "Cannot add custom payment param #{name}, as it already exists and is : #{payment_config[name].explaination}"
  end
  payment_config[name] = ConfigField.new(explaination, opts[:default], !!opts[:mandatory])
end

.default_payment_params(default_values) ⇒ Object

Setup default values for existing payment params



70
71
72
73
74
# File 'lib/omnipay/adapter.rb', line 70

def self.default_payment_params(default_values)
  default_values.each do |name, default_value|
    payment_config[name] && ( payment_config[name].value = default_value )
  end
end

.enable_ipnObject

Setup if IPN or not



81
82
83
# File 'lib/omnipay/adapter.rb', line 81

def self.enable_ipn
  @ipn_enabled = true
end

.ipn?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/omnipay/adapter.rb', line 85

def self.ipn?
  !!@ipn_enabled
end

.payment_configObject

Accessor to the payment config



56
57
58
# File 'lib/omnipay/adapter.rb', line 56

def self.payment_config
  @payment_config ||= Omnipay::Helpers.deep_dup(DEFAULT_PAYMENT_CONFIG)
end

Instance Method Details

#callback_hash(request) ⇒ Object



153
154
155
156
# File 'lib/omnipay/adapter.rb', line 153

def callback_hash(request)
  request = Rack::Request.new(request.env.dup)
  return validate_callback_status(request)
end

#configObject

Config easy readers from : => ConfigField(…, value=‘bar’) to : config.foo # => ‘bar’



105
106
107
# File 'lib/omnipay/adapter.rb', line 105

def config
  @config ||= OpenStruct.new(Hash[@adapter_config.map{|name, config_field| [name, config_field.value]}])
end

#default_payment_paramsObject



109
110
111
# File 'lib/omnipay/adapter.rb', line 109

def default_payment_params
  @default_payment_params ||= OpenStruct.new(Hash[@payment_config.map{|name, config_field| [name, config_field.value]}])
end

#ipn_hash(request) ⇒ Object



147
148
149
150
# File 'lib/omnipay/adapter.rb', line 147

def ipn_hash(request)
  request = Rack::Request.new(request.env.dup)
  return validate_payment_notification(request)
end

#payment_page_ipn_redirection(params, ipn_url, callback_url) ⇒ Object

Raises:

  • (NoMethodError)


167
168
169
# File 'lib/omnipay/adapter.rb', line 167

def payment_page_ipn_redirection(params, ipn_url, callback_url)
  raise NoMethodError, "To redefine in adapter implementation"
end

#payment_page_redirection(params, callback_url) ⇒ Object

Logic to redefine in subclasses

Raises:

  • (NoMethodError)


163
164
165
# File 'lib/omnipay/adapter.rb', line 163

def payment_page_redirection(params, callback_url)      
  raise NoMethodError, "To redefine in adapter implementation"
end

#request_phase(params, ipn_url, callback_url) ⇒ Object

Public interface



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/omnipay/adapter.rb', line 118

def request_phase(params, ipn_url, callback_url)
  payment_params = Omnipay::Helpers.deep_dup(@payment_config)

  # Merge params with default payment config
  params.each do |name, value|
    payment_params[name] && payment_params[name].value = value
  end

  # Validate payment params
  payment_params.each do |name, config_field|
    if config_field.mandatory && config_field.value.nil?
      raise ArgumentError, "Mandatory payment parameter #{name} is not defined. It is supposed to be #{config_field.explaination}"          
    end
  end

  # {name => config_field} to OpenStruct(name => value)
  payment_params = OpenStruct.new(
    Hash[ payment_params.map{|name, config_field| [name, config_field.value]} ]
  )

  # Forward to the right method (redirection_with_ipn or redirection)
  if self.class.ipn?
    return payment_page_redirection_ipn(payment_params, ipn_url, callback_url)
  else
    return payment_page_redirection(payment_params, callback_url)
  end
end

#validate_callback_status(request) ⇒ Object

Raises:

  • (NoMethodError)


177
178
179
# File 'lib/omnipay/adapter.rb', line 177

def validate_callback_status(request)
  raise NoMethodError, "To redefine in adapter implementation"
end

#validate_payment_notification(request) ⇒ Object

Raises:

  • (NoMethodError)


172
173
174
# File 'lib/omnipay/adapter.rb', line 172

def validate_payment_notification(request)
  raise NoMethodError, "To redefine in adapter implementation"
end