class Kount::Client
This class is where the primary interaction with the merchant integration will take place.
Constants
- DEFAULT_VERSION
-
RIS Version. Can be overridden my merchant if required.
- ENDPOINT_PROD
-
Default endpoint for production. Used by the DEFAULT_OPTIONS
- ENDPOINT_TEST
-
Default endpoint for test. Used by the
TEST_DEFAULT_OPTIONS
- PAYMENTS_FRAUD_API_ENDPOINT_PROD
-
Default endpoint for Payments Fraud by
Kount
360 production. Used by the DEFAULT_OPTIONS - PAYMENTS_FRAUD_API_ENDPOINT_TEST
-
Default endpoint for Payments Fraud by
Kount
360 test. Used by theTEST_DEFAULT_OPTIONS
- PAYMENTS_FRAUD_AUTH_ENDPOINT_PROD
-
Default endpoint for Payments Fraud by
Kount
360 production. Used by the DEFAULT_OPTIONS - PAYMENTS_FRAUD_AUTH_ENDPOINT_TEST
-
Default endpoint for Payments Fraud by
Kount
360 test. Used by theTEST_DEFAULT_OPTIONS
- PROD_DEFAULT_OPTIONS
-
Default params for production
- RESPONSE_FORMAT
-
Tells the RIS server to respond in JSON instead of key/value pairs This cannot be overridden.
- TEST_DEFAULT_OPTIONS
-
Default params for test if is_test is TRUE
Public Class Methods
Source
# File lib/kount/client.rb, line 72 def initialize(params = {}) @options = {} 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] @options.merge!(TEST_DEFAULT_OPTIONS) else @options.merge!(PROD_DEFAULT_OPTIONS) end @options.merge!(params) if @migration_mode_enabled @options[:version] = DEFAULT_VERSION # this is needed for the intg to work correctly end end
Initialize a client object
Example usage
{:merchant_id => "123456", :key => "trhvihsrihsta7ftadk6edkre7y8..."}
@param params [Hash] Hash with merchant_id
, ksalt and key, plus any other optional params
Public Instance Methods
Source
# File lib/kount/client.rb, line 146 def endpoint if @migration_mode_enabled return @options[:pf_api_endpoint] end @options[:endpoint] end
RIS Endpoint URL
Source
# File lib/kount/client.rb, line 99 def get_response(request) headers = {} if @migration_mode_enabled if @token_expires_at.nil? || DateTime.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, 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
Makes the call to the Kount
RIS server
@param request [Kount::Request] Kount
inquiry or update object @return [Hash] RIS response formatted into a native hash rubocop:disable Metrics/AbcSize
Source
# File lib/kount/client.rb, line 159 def key @options[:key] end
Merchant API for RIS access
Source
# File lib/kount/client.rb, line 164 def ksalt @options[:ksalt] end
Secret Kount
salt for KHASH
Source
# File lib/kount/client.rb, line 133 def merchant_id if @migration_mode_enabled return @options[:pf_client_id] end @options[:merchant_id] end
Kount
Merchant ID
Source
# File lib/kount/client.rb, line 128 def prepare_request_params(request) request.prepare_params(version, merchant_id, RESPONSE_FORMAT, ksalt) end
Give the request object what it needs to know to process the params to send to RIS.
Source
# File lib/kount/client.rb, line 169 def test? @options[:is_test] end
Is test or production setting
Source
# File lib/kount/client.rb, line 154 def timeout @options[:timeout] end
Timeout settings
Source
# File lib/kount/client.rb, line 141 def version @options[:version] end
RIS Interface Version
Private Instance Methods
Source
# File lib/kount/client.rb, line 175 def endpoint_uri @endpoint_uri ||= URI(endpoint) end
Source
# File lib/kount/client.rb, line 180 def http if endpoint_uri.host.nil? || endpoint_uri.port.nil? raise ArgumentError, 'Invalid endpoint or port' end net_http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port) if endpoint_uri.scheme == 'https' net_http.use_ssl = true net_http.verify_mode = test? ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options] |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF end net_http.open_timeout = timeout net_http.read_timeout = timeout net_http.ignore_eof = true net_http end
rubocop:disable Metrics/AbcSize
Source
# File lib/kount/client.rb, line 196 def http_headers { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => "SDK-RIS-Ruby/#{Config::SDK_VERSION}", 'X-Kount-Api-Key' => key } end
Source
# File lib/kount/client.rb, line 232 def http_path endpoint_uri.path.empty? ? '/' : endpoint_uri.path end
Source
# File lib/kount/client.rb, line 205 def pf_http_headers { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => "SDK-RIS-Ruby/#{Config::SDK_VERSION}", 'Authorization' => 'Bearer ' } end
Source
# File lib/kount/client.rb, line 215 def refresh_pf_auth_token payload = URI.encode_www_form({ grant_type: 'client_credentials', scope: 'k1_integration_api' }) headers = { Authorization: "Basic #{@options[:pf_api_key]}", 'Content-Type': 'application/x-www-form-urlencoded' } uri = URI(@options[:pf_auth_endpoint]) client = Net::HTTP.new(uri.host, uri.port) client.ignore_eof = true client.use_ssl = true response = client.post(@options[:pf_auth_endpoint], payload, headers) return unless response.code == '200' data = JSON.parse(response.body) expires_in = data['expires_in'].to_i @access_token = data['access_token'] @token_expires_at = DateTime.now.to_time + (expires_in - 60) # less 60 seconds for latency end
rubocop:disable Metrics/AbcSize