class AmexTokenizationClient

developer.americanexpress.com/products/amex-token-service/resources

Constants

VERSION

Attributes

base_path[R]
client_id[R]
client_secret[R]
encryption_key[R]
encryption_key_id[R]
host[R]
logger[RW]
token_requester_id[R]

Public Class Methods

new(host:, token_requester_id:, client_id:, client_secret:, encryption_key_id:, encryption_key:, logger: Logger.new('/dev/null')) click to toggle source
# File lib/amex_tokenization_client.rb, line 20
def initialize(host:,
               token_requester_id:,
               client_id:, client_secret:,
               encryption_key_id:, encryption_key:,
               logger: Logger.new('/dev/null'))
  @host = host
  @base_path = "/payments/digital/v2/tokens".freeze
  @token_requester_id = token_requester_id
  @client_id, @client_secret = client_id, client_secret
  @encryption_key_id, @encryption_key = encryption_key_id, Base64.decode64(encryption_key)
  @logger = logger
end

Public Instance Methods

headers(authorization) click to toggle source
# File lib/amex_tokenization_client.rb, line 95
def headers(authorization)
  Hash[
    'Content-Type' => 'application/json',
    'Content-Language' => 'en-US',
    'Accept-Language' => 'en',
    'x-amex-token-requester-id' => token_requester_id,
    'x-amex-api-key' => client_id,
    'x-amex-request-id' => SecureRandom.uuid,
    'authorization' => authorization,
  ]
end
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_tokenization_client.rb, line 81
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_tokenization_client.rb, line 87
def hmac_digest(s)
  Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, client_secret, s.to_s))
end
jwe_decrypt(data) click to toggle source
# File lib/amex_tokenization_client.rb, line 67
def jwe_decrypt(data)
  JWE.decrypt(data, encryption_key)
end
metadata(token_ref_id) click to toggle source
# File lib/amex_tokenization_client.rb, line 55
def metadata(token_ref_id)
  JSON.parse send_authorized_request('GET', "#{token_ref_id}/metadata")
end
new_request(method, resource_path, authorization) click to toggle source
# File lib/amex_tokenization_client.rb, line 91
def new_request(method, resource_path, authorization)
  Request.new(method, "https://#{host}/#{resource_path}", headers(authorization), logger: logger)
end
notifications(kargs) click to toggle source
# File lib/amex_tokenization_client.rb, line 47
def notifications(kargs)
  send_authorized_request('POST', 'notifications', notifications_payload(kargs))
end
notifications_payload(kargs) click to toggle source
# File lib/amex_tokenization_client.rb, line 63
def notifications_payload(kargs)
  NotificationsPayload.new(kargs).to_json(encryption_key_id, encryption_key)
end
provisioning(kargs) click to toggle source

@return [Hash] token_ref_id and other values.

# File lib/amex_tokenization_client.rb, line 34
def provisioning(kargs)
  response = JSON.parse send_authorized_request('POST', 'provisioning', provisioning_payload(kargs))
  response.merge! JSON.parse jwe_decrypt response.delete('secure_token_data')
  response
end
provisioning_payload(kargs) click to toggle source
# File lib/amex_tokenization_client.rb, line 59
def provisioning_payload(kargs)
  ProvisioningPayload.new(kargs).to_json(encryption_key_id, encryption_key)
end
provisionings(kargs) click to toggle source

@return [Hash] token_ref_id and other values.

# File lib/amex_tokenization_client.rb, line 41
def provisionings(kargs)
  response = JSON.parse send_authorized_request('POST', 'provisionings', provisioning_payload(kargs))
  response.merge! JSON.parse jwe_decrypt response.delete('secure_token_data')
  response
end
send_authorized_request(method, route, payload = nil) click to toggle source
# File lib/amex_tokenization_client.rb, line 71
def send_authorized_request(method, route, payload = nil)
  resource_path = "#{base_path}/#{route}"
  authorization = hmac_authorization(method, resource_path, payload)
  new_request(method, resource_path, authorization).send(payload)
end
status(token_ref_id) click to toggle source
# File lib/amex_tokenization_client.rb, line 51
def status(token_ref_id)
  JSON.parse send_authorized_request('GET', "#{token_ref_id}/status")
end