class AmexEnhancedAuthorization::Connection

Attributes

base_path[R]
client_id[R]
client_secret[R]
host[R]
logger[RW]

Public Class Methods

new(host:, client_id:, client_secret:, logger: Logger.new('/dev/null')) click to toggle source
# File lib/amex_enhanced_authorization/connection.rb, line 8
def initialize(host:,
               client_id:, client_secret:,
               logger: Logger.new('/dev/null'))
  @host = host
  @base_path = "/risk/fraud/v2/apiplatform/enhanced_authorizations".freeze
  @client_id, @client_secret = client_id, client_secret
  @logger = logger
end

Public Instance Methods

hmac_authorization(method, resource_path, payload, nonce = SecureRandom.uuid, ts = (Time.now.to_r * 1000).to_i) click to toggle source

@param [String] method, e.g. 'POST' @param [String] resource_path, e.g. '/payments/digital/v2/tokens/provisioning' @param [String] JSON payload @return [String] Authorization: MAC id=“gfFb4K8esqZgMpzwF9SXzKLCCbPYV8bR”,ts=“1463772177193”,nonce=“61129a8d-ca24-464b-8891-9251501d86f0”, bodyhash=“YJpz6NdGP0aV6aYaa+6qKCjQt46of+Cj4liBz90G6X8=”, mac=“uzybzLPj3fD8eBZaBzb4E7pZs+l+IWS0w/w2wwsExdo=”

# File lib/amex_enhanced_authorization/connection.rb, line 33
def hmac_authorization(method, resource_path, payload, nonce = SecureRandom.uuid, ts = (Time.now.to_r * 1000).to_i)
  bodyhash = hmac_digest(payload)
  mac = hmac_digest([ts, nonce, method, resource_path, host, 443, bodyhash, ''].join("\n"))
  %(MAC id="#{client_id}",ts="#{ts}",nonce="#{nonce}",bodyhash="#{bodyhash}",mac="#{mac}")
end
hmac_digest(s) click to toggle source
# File lib/amex_enhanced_authorization/connection.rb, line 39
def hmac_digest(s)
  Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, client_secret, s.to_s))
end
online_purchase(params) click to toggle source
# File lib/amex_enhanced_authorization/connection.rb, line 17
def online_purchase(params)
  payload = OnlinePurchasePayload.new(params).to_json
  JSON.parse send_authorized_request('POST', 'online_purchases', payload)
end
send_authorized_request(method, route, payload = nil) click to toggle source
# File lib/amex_enhanced_authorization/connection.rb, line 22
def send_authorized_request(method, route, payload = nil)
  resource_path = "#{base_path}/#{route}"
  authorization = hmac_authorization(method, resource_path, payload)
  request = Request.new(method, "https://#{host}#{resource_path}", client_id, logger)
  request.send(payload, authorization)
end