Module: PayEx::API

Extended by:
API
Included in:
API
Defined in:
lib/payex/api.rb

Defined Under Namespace

Classes: ParamError

Instance Method Summary collapse

Instance Method Details

#_parse_params!(params, specs) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/payex/api.rb', line 67

def _parse_params!(params, specs)
  for name, options in specs
    begin
      params[name] = parse_param(params[name], options)
    rescue PayEx::API::ParamError => error
      param_error! %{#{name.inspect}: #{error.message}}
    end
  end
end

#get_request_body(params, specs) ⇒ Object



41
42
43
44
45
# File 'lib/payex/api.rb', line 41

def get_request_body(params, specs)
  parse_params(params, specs).tap do |params|
    params['hash'] = sign_params(params, specs)
  end
end

#hashed_params(params, specs) ⇒ Object



51
52
53
54
# File 'lib/payex/api.rb', line 51

def hashed_params(params, specs)
  specs.select { |key, options| options[:signed] }.
    keys.map { |key| params[key] }
end

#invoke!(wsdl, name, params, specs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/payex/api.rb', line 9

def invoke! wsdl, name, params, specs
  body = get_request_body(params, specs)
  response = invoke_raw! wsdl, name, body

  # Unwrap e.g. <Initialize8Response>
  response = response[response.keys.first]
  # Unwrap e.g. <Initialize8Result>
  response = response[response.keys.first]
  # Parse embedded XML document
  response = Nori.parse(response)
  # Unwrap <payex>
  response = response[response.keys.first]

  if ok = response[:status][:code] == 'OK' rescue false
    response
  elsif defined? response[:status][:description]
    raise PayEx::Error, response[:status][:description]
  else
    raise PayEx::Error, %{invalid response: #{response.inspect}}
  end
end

#invoke_raw!(wsdl, name, body) ⇒ Object



31
32
33
34
35
# File 'lib/payex/api.rb', line 31

def invoke_raw! wsdl, name, body
  Savon.client(wsdl).request(name, body: body) {
    http.headers.delete('SOAPAction')
  }.body
end

#param_error!(message) ⇒ Object

Raises:



98
99
100
# File 'lib/payex/api.rb', line 98

def param_error! message
  raise ParamError, message
end

#parse_param(param, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/payex/api.rb', line 77

def parse_param(param, options)
  unless options.is_a? Hash
    raise ArgumentError, %{expected Hash, got #{options.inspect}}
  end

  if param != nil
    result = param
  elsif options.include? :default
    result = options[:default]
    result = result.call if result.is_a? Proc
  else
    param_error! 'parameter required'
  end

  if options.include?(:format) and not options[:format] === result
    param_error! %{must match #{options[:format].inspect}}
  else
    result
  end
end

#parse_params(params, specs) ⇒ Object



56
57
58
59
60
61
# File 'lib/payex/api.rb', line 56

def parse_params(params, specs)
  stringify_keys(params).tap do |result|
    _parse_params! result, specs
    result.select! { |k, v| v != nil }
  end
end

#sign_params(params, specs) ⇒ Object



47
48
49
# File 'lib/payex/api.rb', line 47

def sign_params(params, specs)
  signed_hash(hashed_params(params, specs).join)
end

#signed_hash(string) ⇒ Object



37
38
39
# File 'lib/payex/api.rb', line 37

def signed_hash(string)
  Digest::MD5.hexdigest(string + PayEx.encryption_key!)
end

#stringify_keys(hash) ⇒ Object



63
64
65
# File 'lib/payex/api.rb', line 63

def stringify_keys(hash)
  Hash[*hash.map { |k, v| [k.to_s, v] }.flatten]
end