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



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

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



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

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



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

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
30
# 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
  parser = Nori.new(convert_tags_to: lambda { |tag| tag.snakecase.to_sym })
  response = parser.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



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

def invoke_raw! wsdl, name, body
  Savon.client(wsdl: wsdl).call(name, soap_action: false, message: body).body
end

#param_error!(message) ⇒ Object

Raises:



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

def param_error! message
  raise ParamError, message
end

#parse_param(param, options) ⇒ Object



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

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



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

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

#parse_transaction_status(status) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/payex/api.rb', line 101

def parse_transaction_status(status)
  case status.to_s
  when '0' then :sale
  when '1' then :initialize
  when '2' then :credit
  when '3' then :authorize
  when '4' then :cancel
  when '5' then :failure
  when '6' then :capture
  else status.to_s
  end
end

#sign_params(params, specs) ⇒ Object



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

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

#signed_hash(string) ⇒ Object



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

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

#stringify_keys(hash) ⇒ Object



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

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