module Firstdata

Constants

VERSION

Attributes

api_url[RW]
gateway_id[RW]
key[RW]
key_id[RW]
password[RW]

Public Class Methods

transact(payload) click to toggle source
# File lib/firstdata.rb, line 26
def self.transact(payload)
  body = payload.merge(ExactID: @gateway_id, Password: @password).to_xml(:root => 'Transaction', :dasherize => false, :skip_instruct => true, :skip_types => true)
  post(set_headers(body), body, api_url)
end

Private Class Methods

commit_post(http, request) click to toggle source
# File lib/firstdata.rb, line 73
def self.commit_post(http, request)
  begin
    response = http.request(request)
    Hash.from_trusted_xml(response.body)
  rescue
    handle_error(response)
  end
end
concat(*args) click to toggle source
# File lib/firstdata.rb, line 41
def self.concat(*args)
  args.join("\n")
end
digest(payload) click to toggle source
# File lib/firstdata.rb, line 33
def self.digest(payload)
  OpenSSL::Digest.new('sha1', payload)
end
gen_hmac(digest, data) click to toggle source
# File lib/firstdata.rb, line 37
def self.gen_hmac(digest, data)
  Base64.strict_encode64(OpenSSL::HMAC.digest(digest, @key, data))
end
handle_error(e) click to toggle source
# File lib/firstdata.rb, line 82
def self.handle_error(e)
  e = e.body if e.body
  { 'Error' => { 'messages' => "#{e.inspect}" } }
end
post(headers, body, url) click to toggle source
# File lib/firstdata.rb, line 64
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) click to toggle source
# File lib/firstdata.rb, line 49
def self.set_headers(payload)
  type = 'application/xml'
  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-sha1' => content_digest.to_s,
      'Authorization' => "GGE4_API #{@key_id}:#{hmac}"
  }
end
urn() click to toggle source
# File lib/firstdata.rb, line 45
def self.urn
  @api_url.split('.com').last
end