class Payhere::Client

Public Instance Methods

create_connection() click to toggle source

set authorization and authentication

# File lib/payhere-sdk/client.rb, line 50
def create_connection
  url = "https://api.payhere.africa" if Payhere.config.environment.eql?"production"
  url = "https://api-sandbox.payhere.africa/api" if Payhere.config.environment.eql?"sandbox"
  
  headers = {
    "Content-Type": 'application/json'
  }

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

  get_credentials
  conn.basic_auth(@username, @password)

  conn
end
get_credentials() click to toggle source
# File lib/payhere-sdk/client.rb, line 67
def get_credentials
  @username = Payhere.config.username
  @password = Payhere.config.password
end
get_transaction_status(path) click to toggle source

retrieve transaction information

# File lib/payhere-sdk/client.rb, line 73
def get_transaction_status(path)
  send_request('get', path)
end
handle_error(response_body, response_code) click to toggle source
# File lib/payhere-sdk/client.rb, line 45
def handle_error(response_body, response_code)
  raise Payhere::Error.new(response_body, response_code)
end
interpret_response(resp) click to toggle source
# File lib/payhere-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(method, path, body = {}) click to toggle source
# File lib/payhere-sdk/client.rb, line 14
def send_request(method, path, body = {})
  begin
    conn = create_connection
    relative_path = "/api/#{Payhere.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