Module: Firstdata

Defined in:
lib/firstdata.rb,
lib/firstdata/version.rb

Constant Summary collapse

VERSION =
"1.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_urlObject

Returns the value of attribute api_url.



21
22
23
# File 'lib/firstdata.rb', line 21

def api_url
  @api_url
end

.gateway_idObject

Returns the value of attribute gateway_id.



21
22
23
# File 'lib/firstdata.rb', line 21

def gateway_id
  @gateway_id
end

.keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/firstdata.rb', line 21

def key
  @key
end

.key_idObject

Returns the value of attribute key_id.



21
22
23
# File 'lib/firstdata.rb', line 21

def key_id
  @key_id
end

.passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/firstdata.rb', line 21

def password
  @password
end

Class Method Details

.commit_post(http, request) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/firstdata.rb', line 71

def self.commit_post(http, request)
  begin
    response = http.request(request)
    JSON.parse response.body
  rescue JSON::ParserError
    handle_error(response)
  end
end

.concat(*args) ⇒ Object



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

def self.concat(*args)
  args.join("\n")
end

.digest(payload) ⇒ Object



31
32
33
# File 'lib/firstdata.rb', line 31

def self.digest(payload)
  OpenSSL::Digest.new('sha256', payload)
end

.gen_hmac(digest, data) ⇒ Object



35
36
37
# File 'lib/firstdata.rb', line 35

def self.gen_hmac(digest, data)
  Base64.strict_encode64(OpenSSL::HMAC.digest(digest, @key, data))
end

.handle_error(e) ⇒ Object



80
81
82
83
# File 'lib/firstdata.rb', line 80

def self.handle_error(e)
  e = e.body if e.body
  { 'Error' => { 'messages' => "#{e.inspect}" } }
end

.post(headers, body, url) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/firstdata.rb', line 62

def self.post(headers, body, url)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = body
  commit_post(http, request)
end

.set_headers(payload) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/firstdata.rb', line 47

def self.set_headers(payload)
  type = 'application/json'
  time = Time.now.utc.iso8601
  content_digest = digest(payload)
  data = concat('POST', type, content_digest.to_s, time, urn)
  hmac = gen_hmac(content_digest, data)
  {
      'Content-Type' => type,
      'Accept' => type,
      'x-gge4-date' => time,
      'x-gge4-content-sha256' => content_digest.to_s,
      'Authorization' => "GGE4_API #{@key_id}:#{hmac}"
  }
end

.transact(payload) ⇒ Object



24
25
26
27
# File 'lib/firstdata.rb', line 24

def self.transact(payload)
  body = payload.merge(gateway_id: @gateway_id, password: @password).to_json
  post(set_headers(body), body, api_url)
end

.urnObject



43
44
45
# File 'lib/firstdata.rb', line 43

def self.urn
  @api_url.split('.com').last
end