Module: Adyen::Form

Extended by:
ActionView::Helpers::TagHelper
Defined in:
lib/adyen/form.rb

Constant Summary collapse

ACTION_URL =

ADYEN FORM URL

"https://%s.adyen.com/hpp/select.shtml"

Class Method Summary collapse

Class Method Details

.calculate_signature(parameters) ⇒ Object



116
117
118
# File 'lib/adyen/form.rb', line 116

def self.calculate_signature(parameters)
   Adyen::Encoding.hmac_base64(parameters.delete(:shared_secret), calculate_signature_string(parameters))
end

.calculate_signature_string(parameters) ⇒ Object

MERCHANT SIGNATURE CALCULATION



105
106
107
108
109
110
111
112
113
114
# File 'lib/adyen/form.rb', line 105

def self.calculate_signature_string(parameters)
  merchant_sig_string = ""
  merchant_sig_string << parameters[:payment_amount].to_s    << parameters[:currency_code].to_s      <<
                         parameters[:ship_before_date].to_s  << parameters[:merchant_reference].to_s <<
                         parameters[:skin_code].to_s         << parameters[:merchant_account].to_s   <<
                         parameters[:session_validity].to_s  << parameters[:shopper_email].to_s      <<
                         parameters[:shopper_reference].to_s << parameters[:recurring_contract].to_s <<
                         parameters[:allowed_methods].to_s   << parameters[:blocked_methods].to_s    <<
                         parameters[:shopper_statement].to_s << parameters[:billing_address_type].to_s
end

.default_parametersObject

DEFAULT FORM / REDIRECT PARAMETERS



36
37
38
# File 'lib/adyen/form.rb', line 36

def self.default_parameters
  @default_arguments ||= {}
end

.default_parameters=(hash) ⇒ Object



40
41
42
# File 'lib/adyen/form.rb', line 40

def self.default_parameters=(hash)
  @default_arguments = hash
end

.do_parameter_transformations!(parameters = {}) ⇒ Object

POSTING/REDIRECTING TO ADYEN



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/adyen/form.rb', line 60

def self.do_parameter_transformations!(parameters = {})
  raise "YENs are not yet supported!" if parameters[:currency_code] == 'JPY' # TODO: fixme

  parameters.replace(default_parameters.merge(parameters))
  parameters[:recurring_contract] = 'DEFAULT' if parameters.delete(:recurring) == true
  parameters[:order_data]         = Adyen::Encoding.gzip_base64(parameters.delete(:order_data_raw)) if parameters[:order_data_raw]
  parameters[:ship_before_date]   = Adyen::Formatter::DateTime.fmt_date(parameters[:ship_before_date])
  parameters[:session_validity]   = Adyen::Formatter::DateTime.fmt_time(parameters[:session_validity])
  
  if parameters[:skin]
    skin = Adyen::Form.skin_by_name(parameters.delete(:skin))
    parameters[:skin_code]     ||= skin[:skin_code]
    parameters[:shared_secret] ||= skin[:shared_secret]
  end
end

.hidden_fields(parameters = {}) ⇒ Object



94
95
96
97
98
99
# File 'lib/adyen/form.rb', line 94

def self.hidden_fields(parameters = {})
  # Generate hidden input tags
  payment_parameters(parameters).map { |key, value|
    self.tag(:input, :type => 'hidden', :name => key.to_s.camelize(:lower), :value => value)
  }.join("\n")
end

.lookup_shared_secret(skin_code) ⇒ Object



28
29
30
# File 'lib/adyen/form.rb', line 28

def self.lookup_shared_secret(skin_code)
  skin = skin_by_code(skin_code)[:shared_secret] rescue nil
end

.payment_parameters(parameters = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/adyen/form.rb', line 76

def self.payment_parameters(parameters = {})
  do_parameter_transformations!(parameters)
  
  raise "Cannot generate form: :currency code attribute not found!"         unless parameters[:currency_code]
  raise "Cannot generate form: :payment_amount code attribute not found!"   unless parameters[:payment_amount]
  raise "Cannot generate form: :merchant_account attribute not found!"      unless parameters[:merchant_account]
  raise "Cannot generate form: :skin_code attribute not found!"             unless parameters[:skin_code]
  raise "Cannot generate form: :shared_secret signing secret not provided!" unless parameters[:shared_secret]

  # Merchant signature
  parameters[:merchant_sig] = calculate_signature(parameters)
  return parameters      
end

.redirect_signature(params, shared_secret = nil) ⇒ Object



128
129
130
131
# File 'lib/adyen/form.rb', line 128

def self.redirect_signature(params, shared_secret = nil)
  shared_secret ||= lookup_shared_secret(params[:skinCode])
  Adyen::Encoding.hmac_base64(shared_secret, redirect_signature_string(params))
end

.redirect_signature_check(params, shared_secret = nil) ⇒ Object



133
134
135
# File 'lib/adyen/form.rb', line 133

def self.redirect_signature_check(params, shared_secret = nil)
  params[:merchantSig] == redirect_signature(params, shared_secret)
end

.redirect_signature_string(params) ⇒ Object

REDIRECT SIGNATURE CHECKING



124
125
126
# File 'lib/adyen/form.rb', line 124

def self.redirect_signature_string(params)
  params[:authResult].to_s + params[:pspReference].to_s + params[:merchantReference].to_s + params[:skinCode].to_s
end

.redirect_url(parameters = {}) ⇒ Object



90
91
92
# File 'lib/adyen/form.rb', line 90

def self.redirect_url(parameters = {})
  self.url + '?' + payment_parameters(parameters).map { |(k, v)| "#{k.to_s.camelize(:lower)}=#{CGI.escape(v.to_s)}" }.join('&')
end

.register_skin(name, skin_code, shared_secret) ⇒ Object



16
17
18
# File 'lib/adyen/form.rb', line 16

def self.register_skin(name, skin_code, shared_secret)
  self.skins[name] = {:name => name, :skin_code => skin_code, :shared_secret => shared_secret }
end

.skin_by_code(skin_code) ⇒ Object



24
25
26
# File 'lib/adyen/form.rb', line 24

def self.skin_by_code(skin_code)
  self.skins.detect { |(name, skin)| skin[:skin_code] == skin_code }.last rescue nil      
end

.skin_by_name(skin_name) ⇒ Object



20
21
22
# File 'lib/adyen/form.rb', line 20

def self.skin_by_name(skin_name)
  self.skins[skin_name]
end

.skinsObject

SKINS



12
13
14
# File 'lib/adyen/form.rb', line 12

def self.skins
  @skins ||= {}
end

.url(environment = nil) ⇒ Object



50
51
52
53
# File 'lib/adyen/form.rb', line 50

def self.url(environment = nil)
  environment ||= Adyen.environment(environment)
  Adyen::Form::ACTION_URL % environment.to_s
end