Class: Momoapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/momoapi-ruby/client.rb

Direct Known Subclasses

Collection, Disbursement, Remittance

Instance Method Summary collapse

Instance Method Details

#get_auth_token(path, subscription_key) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object

get the balance on an account



93
94
95
# File 'lib/momoapi-ruby/client.rb', line 93

def get_balance(path, subscription_key)
  prepare_get_request(path, subscription_key)
end

#get_credentials(product) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object

retrieve transaction information, for transfer and payments



98
99
100
# 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) ⇒ Object

Raises:



45
46
47
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

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



103
104
105
# 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) ⇒ Object



83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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