class Momoapi::Client

Public Instance Methods

get_auth_token(path, subscription_key) click to toggle source

Create an access token which can then be used to authorize and authenticate towards the other end-points of the API

# File lib/momoapi-ruby/client.rb, line 51
def get_auth_token(path, subscription_key)
  headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
  }
  url = Momoapi.config.base_url
  conn = Faraday.new(url: url)
  conn.headers = headers
  @product = path.split('/')[0]
  get_credentials(@product)
  conn.basic_auth(@username, @password)
  response = conn.post(path)
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    response.body.to_s
  end
end
get_balance(path, subscription_key) click to toggle source

get the balance on an account

# File lib/momoapi-ruby/client.rb, line 93
def get_balance(path, subscription_key)
  prepare_get_request(path, subscription_key)
end
get_credentials(product) click to toggle source
# File lib/momoapi-ruby/client.rb, line 69
def get_credentials(product)
  case product
  when 'collection'
    @username = Momoapi.config.collection_user_id
    @password = Momoapi.config.collection_api_secret
  when 'disbursement'
    @username = Momoapi.config.disbursement_user_id
    @password = Momoapi.config.disbursement_api_secret
  when 'remittance'
    @username = Momoapi.config.remittance_user_id
    @password = Momoapi.config.remittance_api_secret
  end
end
get_transaction_status(path, subscription_key) click to toggle source

retrieve transaction information, for transfer and payments

# File lib/momoapi-ruby/client.rb, line 98
def get_transaction_status(path, subscription_key)
  prepare_get_request(path, subscription_key)
end
handle_error(response_body, response_code) click to toggle source
# File lib/momoapi-ruby/client.rb, line 45
def handle_error(response_body, response_code)
  raise Momoapi::Error.new(response_body, response_code)
end
interpret_response(resp) click to toggle source
# File lib/momoapi-ruby/client.rb, line 33
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
is_user_active(path, subscription_key) click to toggle source

check if an account holder is registered and active in the system

# File lib/momoapi-ruby/client.rb, line 103
def is_user_active(path, subscription_key)
  prepare_get_request(path, subscription_key)
end
prepare_get_request(path, subscription_key) click to toggle source
# File lib/momoapi-ruby/client.rb, line 83
def prepare_get_request(path, subscription_key)
  headers = {
    "X-Target-Environment": Momoapi.config.environment || 'sandbox',
    "Content-Type": 'application/json',
    "Ocp-Apim-Subscription-Key": subscription_key
  }
  send_request('get', path, headers)
end
send_request(method, path, headers, body = {}) click to toggle source
# File lib/momoapi-ruby/client.rb, line 14
def send_request(method, path, headers, body = {})
  begin
    auth_token = get_auth_token['access_token']
    conn = Faraday.new(url: Momoapi.config.base_url)
    conn.headers = headers
    conn.authorization(:Bearer, auth_token)

    case method
    when 'get'
      response = conn.get(path)
    when 'post'
      response = conn.post(path, body.to_json)
    end
  rescue ArgumentError
    raise "Missing configuration key(s) for #{@product.capitalize}s"
  end
  interpret_response(response)
end