Class: AmexTokenizationClient

Inherits:
Object
  • Object
show all
Defined in:
lib/amex_tokenization_client.rb,
lib/amex_tokenization_client/request.rb,
lib/amex_tokenization_client/version.rb,
lib/amex_tokenization_client/provisioning_payload.rb,
lib/amex_tokenization_client/notifications_payload.rb

Overview

Defined Under Namespace

Classes: NotificationsPayload, ProvisioningPayload, Request

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, token_requester_id:, client_id:, client_secret:, encryption_key_id:, encryption_key:, logger: Logger.new('/dev/null')) ⇒ AmexTokenizationClient

Returns a new instance of AmexTokenizationClient.



20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



14
15
16
# File 'lib/amex_tokenization_client.rb', line 14

def base_path
  @base_path
end

#client_idObject (readonly)

Returns the value of attribute client_id.



16
17
18
# File 'lib/amex_tokenization_client.rb', line 16

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



16
17
18
# File 'lib/amex_tokenization_client.rb', line 16

def client_secret
  @client_secret
end

#encryption_keyObject (readonly)

Returns the value of attribute encryption_key.



17
18
19
# File 'lib/amex_tokenization_client.rb', line 17

def encryption_key
  @encryption_key
end

#encryption_key_idObject (readonly)

Returns the value of attribute encryption_key_id.



17
18
19
# File 'lib/amex_tokenization_client.rb', line 17

def encryption_key_id
  @encryption_key_id
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/amex_tokenization_client.rb', line 13

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/amex_tokenization_client.rb', line 18

def logger
  @logger
end

#token_requester_idObject (readonly)

Returns the value of attribute token_requester_id.



15
16
17
# File 'lib/amex_tokenization_client.rb', line 15

def token_requester_id
  @token_requester_id
end

Instance Method Details

#headers(authorization) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ String

Returns Authorization: MAC id=“gfFb4K8esqZgMpzwF9SXzKLCCbPYV8bR”,ts=“1463772177193”,nonce=“61129a8d-ca24-464b-8891-9251501d86f0”, bodyhash=“YJpz6NdGP0aV6aYaa+6qKCjQt46of+Cj4liBz90G6X8=”, mac=“uzybzLPj3fD8eBZaBzb4E7pZs+l+IWS0w/w2wwsExdo=”.

Parameters:

  • method, (String)

    e.g. ‘POST’

  • resource_path, (String)

    e.g. ‘/payments/digital/v2/tokens/provisioning’

  • JSON (String)

    payload

Returns:

  • (String)

    Authorization: MAC id=“gfFb4K8esqZgMpzwF9SXzKLCCbPYV8bR”,ts=“1463772177193”,nonce=“61129a8d-ca24-464b-8891-9251501d86f0”, bodyhash=“YJpz6NdGP0aV6aYaa+6qKCjQt46of+Cj4liBz90G6X8=”, mac=“uzybzLPj3fD8eBZaBzb4E7pZs+l+IWS0w/w2wwsExdo=”



81
82
83
84
85
# 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) ⇒ Object



87
88
89
# 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) ⇒ Object



67
68
69
# File 'lib/amex_tokenization_client.rb', line 67

def jwe_decrypt(data)
  JWE.decrypt(data, encryption_key)
end

#metadata(token_ref_id) ⇒ Object



55
56
57
# File 'lib/amex_tokenization_client.rb', line 55

def (token_ref_id)
  JSON.parse send_authorized_request('GET', "#{token_ref_id}/metadata")
end

#new_request(method, resource_path, authorization) ⇒ Object



91
92
93
# 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) ⇒ Object



47
48
49
# File 'lib/amex_tokenization_client.rb', line 47

def notifications(kargs)
  send_authorized_request('POST', 'notifications', notifications_payload(kargs))
end

#notifications_payload(kargs) ⇒ Object



63
64
65
# 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) ⇒ Hash

Returns token_ref_id and other values.

Returns:

  • (Hash)

    token_ref_id and other values.



34
35
36
37
38
# 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) ⇒ Object



59
60
61
# 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) ⇒ Hash

Returns token_ref_id and other values.

Returns:

  • (Hash)

    token_ref_id and other values.



41
42
43
44
45
# 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) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



51
52
53
# File 'lib/amex_tokenization_client.rb', line 51

def status(token_ref_id)
  JSON.parse send_authorized_request('GET', "#{token_ref_id}/status")
end