class PayCertify::ThreeDS::Client

Constants

API_ENDPOINT

Attributes

api_key[RW]
api_secret[RW]
mode[RW]
response[RW]

Public Class Methods

new(api_key:, api_secret:, mode:) click to toggle source
# File lib/paycertify/three_ds/client.rb, line 12
def initialize(api_key:, api_secret:, mode:)
  self.api_key = api_key
  self.api_secret = api_secret
  self.mode = mode.to_s.to_sym
end

Public Instance Methods

base_url() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 26
def base_url
  @base_url ||= [API_ENDPOINT, path_prefix].join('/')
end
error?() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 52
def error?
  !success?
end
live?() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 18
def live?
  mode.to_sym == :live
end
path_for(path) click to toggle source
# File lib/paycertify/three_ds/client.rb, line 30
def path_for(path)
  base_url + path
end
path_prefix() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 22
def path_prefix
  @path_prefix ||= live?? 'index.php' : 'index_demo.php'
end
post(path:, data:) click to toggle source
# File lib/paycertify/three_ds/client.rb, line 34
def post(path:, data:)
  sorted_data = JSON.generate(data.sort.to_h)

  response = connection.post do |request|
    request.url path_for(path)
    request.headers['Content-Type'] = 'application/json'
    request.headers['x-mpi-api-key'] = api_key
    request.headers['x-mpi-signature'] = signature(path, sorted_data)
    request.body = sorted_data
  end

  respond_with response
end
success?() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 48
def success?
  response.status < 400
end

Private Instance Methods

connection() click to toggle source
# File lib/paycertify/three_ds/client.rb, line 57
def connection
  @connection ||= Faraday.new(url: API_ENDPOINT, ssl: {verify: false}) do |faraday|
    faraday.request :url_encoded
    faraday.request :curl, Logger.new(STDOUT), :warn
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end
end
respond_with(response) click to toggle source
# File lib/paycertify/three_ds/client.rb, line 73
def respond_with(response)
  self.response = response
  JSON.parse(response.body)
end
signature(path, data) click to toggle source
# File lib/paycertify/three_ds/client.rb, line 66
def signature(path, data)
  10.times{puts}
  puts "#{api_key}#{path_for(path)}#{data}#{api_secret}"
  10.times{puts}
  Digest::SHA256.hexdigest "#{api_key}#{path_for(path)}#{data}#{api_secret}"
end