Class: Kount::Client
- Inherits:
-
Object
- Object
- Kount::Client
- Defined in:
- lib/kount/client.rb
Overview
This class is where the primary interaction with the merchant integration will take place.
Constant Summary collapse
- RESPONSE_FORMAT =
Tells the RIS server to respond in JSON instead of key/value pairs This cannot be overridden.
'JSON'- DEFAULT_VERSION =
RIS Version. Can be overridden my merchant if required.
'0720'- ENDPOINT_PROD =
Default endpoint for production. Used by the DEFAULT_OPTIONS
'https://risk.kount.net'- ENDPOINT_TEST =
Default endpoint for test. Used by the TEST_DEFAULT_OPTIONS
'https://risk.test.kount.net'- PAYMENTS_FRAUD_API_ENDPOINT_PROD =
Default endpoint for Payments Fraud by Kount 360 production. Used by the DEFAULT_OPTIONS
'https://api.kount.com/commerce/ris'- PAYMENTS_FRAUD_API_ENDPOINT_TEST =
Default endpoint for Payments Fraud by Kount 360 test. Used by the TEST_DEFAULT_OPTIONS
'https://api-sandbox.kount.com/commerce/ris'- PAYMENTS_FRAUD_AUTH_ENDPOINT_PROD =
Default endpoint for Payments Fraud by Kount 360 production. Used by the DEFAULT_OPTIONS
'https://login.kount.com/oauth2/ausdppksgrbyM0abp357/v1/token'- PAYMENTS_FRAUD_AUTH_ENDPOINT_TEST =
Default endpoint for Payments Fraud by Kount 360 test. Used by the TEST_DEFAULT_OPTIONS
'https://login.kount.com/oauth2/ausdppkujzCPQuIrY357/v1/token'- PROD_DEFAULT_OPTIONS =
Default params for production
{ endpoint: ENDPOINT_PROD, pf_api_endpoint: PAYMENTS_FRAUD_API_ENDPOINT_PROD, pf_auth_endpoint: PAYMENTS_FRAUD_AUTH_ENDPOINT_PROD, version: DEFAULT_VERSION, is_test: false, timeout: 10 }
- TEST_DEFAULT_OPTIONS =
Default params for test if is_test is TRUE
{ endpoint: ENDPOINT_TEST, pf_api_endpoint: PAYMENTS_FRAUD_API_ENDPOINT_TEST, pf_auth_endpoint: PAYMENTS_FRAUD_AUTH_ENDPOINT_TEST, version: DEFAULT_VERSION, timeout: 10 }
Instance Method Summary collapse
- #access_token ⇒ Object
-
#endpoint ⇒ Object
RIS Endpoint URL.
-
#get_response(request) ⇒ Hash
Makes the call to the Kount RIS server.
-
#initialize(params = {}) ⇒ Client
constructor
Initialize a client object.
-
#key ⇒ Object
Merchant API for RIS access.
-
#ksalt ⇒ Object
Secret Kount salt for KHASH.
-
#merchant_id ⇒ Object
Kount Merchant ID.
-
#prepare_request_params(request) ⇒ Object
Give the request object what it needs to know to process the params to send to RIS.
- #set_access_token(token) ⇒ Object
- #set_token_expires_at(token) ⇒ Object
-
#test? ⇒ Boolean
Is test or production setting.
-
#timeout ⇒ Object
Timeout settings.
- #token_expires_at ⇒ Object
-
#version ⇒ Object
RIS Interface Version.
Constructor Details
#initialize(params = {}) ⇒ Client
Initialize a client object
Example usage
{:merchant_id => "123456", :key => "trhvihsrihsta7ftadk6edkre7y8..."}
other optional params
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kount/client.rb', line 67 def initialize(params = {}) # Migration mode enabled @migration_mode_enabled = false @access_token = '' @token_expires_at = Time.now - 3600 # set to one hour ago to force refresh on first use @access_token_mutex = Mutex.new @token_expires_at_mutex = Mutex.new = {} migration_mode = params[:migration_mode_enabled] if migration_mode.nil? @migration_mode_enabled = false else @migration_mode_enabled = migration_mode.to_s.downcase == 'true' end if params[:is_test] .merge!(TEST_DEFAULT_OPTIONS) else .merge!(PROD_DEFAULT_OPTIONS) end .merge!(params) end |
Instance Method Details
#access_token ⇒ Object
92 93 94 |
# File 'lib/kount/client.rb', line 92 def access_token @access_token_mutex.synchronize { @access_token } end |
#endpoint ⇒ Object
RIS Endpoint URL
160 161 162 163 164 165 |
# File 'lib/kount/client.rb', line 160 def endpoint if @migration_mode_enabled return [:pf_api_endpoint] end [:endpoint] end |
#get_response(request) ⇒ Hash
Makes the call to the Kount RIS server
rubocop:disable Metrics/AbcSize
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kount/client.rb', line 113 def get_response(request) headers = {} if @migration_mode_enabled if token_expires_at.nil? || Time.now >= token_expires_at refresh_pf_auth_token if access_token.nil? || access_token == '' raise RuntimeError, 'Access token could not be retrieved' end headers = pf_http_headers headers.merge!({ 'Authorization' => "Bearer #{access_token}" }) end else headers = http_headers end payload = URI.encode_www_form(prepare_request_params(request)) response = {} begin resp = http.post(http_path.to_s, payload, headers) response = JSON.parse(resp.body) rescue StandardError => e puts e # RIS errors do not come back as JSON, so just pass them along raw. response end end |
#key ⇒ Object
Merchant API for RIS access
173 174 175 |
# File 'lib/kount/client.rb', line 173 def key [:key] end |
#ksalt ⇒ Object
Secret Kount salt for KHASH
178 179 180 |
# File 'lib/kount/client.rb', line 178 def ksalt [:ksalt] end |
#merchant_id ⇒ Object
Kount Merchant ID
147 148 149 150 151 152 |
# File 'lib/kount/client.rb', line 147 def merchant_id if @migration_mode_enabled return [:pf_client_id] end [:merchant_id] end |
#prepare_request_params(request) ⇒ Object
Give the request object what it needs to know to process the params to send to RIS.
142 143 144 |
# File 'lib/kount/client.rb', line 142 def prepare_request_params(request) request.prepare_params(version, merchant_id, RESPONSE_FORMAT, ksalt) end |
#set_access_token(token) ⇒ Object
96 97 98 |
# File 'lib/kount/client.rb', line 96 def set_access_token(token) @access_token_mutex.synchronize { @access_token = token } end |
#set_token_expires_at(token) ⇒ Object
104 105 106 |
# File 'lib/kount/client.rb', line 104 def set_token_expires_at(token) @token_expires_at_mutex.synchronize { @token_expires_at = token } end |
#test? ⇒ Boolean
Is test or production setting
183 184 185 |
# File 'lib/kount/client.rb', line 183 def test? [:is_test] end |
#timeout ⇒ Object
Timeout settings
168 169 170 |
# File 'lib/kount/client.rb', line 168 def timeout [:timeout] end |
#token_expires_at ⇒ Object
100 101 102 |
# File 'lib/kount/client.rb', line 100 def token_expires_at @token_expires_at_mutex.synchronize { @token_expires_at } end |
#version ⇒ Object
RIS Interface Version
155 156 157 |
# File 'lib/kount/client.rb', line 155 def version [:version] end |