class Paylense::Client

Public Instance Methods

create_connection(service) click to toggle source

set authorization and authentication

# File lib/paylense-sdk/client.rb, line 50
def create_connection(service)
  url = "https://#{service}.paylense.com" if Payhere.config.environment.eql? 'production'
  url = "https://sandbox#{service}.paylense.com" if Payhere.config.environment.eql? 'sandbox'

  headers = {
    "Content-Type": 'application/json',
    "Accept": 'application/json'
  }

  conn = Faraday.new(url: url)
  conn.headers = headers

  get_credentials
  conn.authorization @access_token
  conn.headers['Secret-Key'] = @secret_key

  conn
end
get_credentials() click to toggle source
# File lib/paylense-sdk/client.rb, line 69
def get_credentials
  @secret_key = Paylense.config.secret_key
  @access_token = Paylense.config.access_token
end
get_transaction_status(path) click to toggle source

retrieve transaction information

# File lib/paylense-sdk/client.rb, line 75
def get_transaction_status(path)
  send_request('get', path)
end
handle_error(response_body, response_code) click to toggle source
# File lib/paylense-sdk/client.rb, line 45
def handle_error(response_body, response_code)
  raise Paylense::Error.new(response_body, response_code)
end
interpret_response(resp) click to toggle source
# File lib/paylense-sdk/client.rb, line 31
def interpret_response(resp)
  body = resp.body.empty? ? '' : JSON.parse(resp.body)

  response = {
    body: body,
    code: resp.status
  }
  unless resp.status >= 200 && resp.status < 300
    handle_error(response[:body], response[:code])
  end

  body
end
send_request(service, method, path, body = {}) click to toggle source
# File lib/paylense-sdk/client.rb, line 14
def send_request(service, method, path, body = {})
  begin
    conn = create_connection(service)
    relative_path = "/api/#{Paylense.config.version}#{path}"

    case method
      when 'get'
        response = conn.get(relative_path)
      when 'post'
        response = conn.post(relative_path, body.to_json)
    end
  rescue ArgumentError
    raise 'Missing configuration key(s)'
  end
  interpret_response(response)
end