1111
1212module Momoapi
1313 class Client
14- def send_request ( method , path , headers , * _body )
14+ def send_request ( method , path , headers , body = { } )
1515 auth_token = get_auth_token [ 'access_token' ]
1616 conn = Faraday . new ( url : Momoapi . config . base_url )
1717 conn . headers = headers
@@ -21,7 +21,7 @@ def send_request(method, path, headers, *_body)
2121 when 'get'
2222 response = conn . get ( path )
2323 when 'post'
24- response = conn . post ( path )
24+ response = conn . post ( path , body . to_json )
2525 end
2626 interpret_response ( response )
2727 end
@@ -48,12 +48,12 @@ def get_auth_token(path, subscription_key)
4848 headers = {
4949 "Ocp-Apim-Subscription-Key" : subscription_key
5050 }
51- username = Momoapi . config . collection_user_id
52- password = Momoapi . config . collection_api_secret
5351 url = Momoapi . config . base_url
5452 conn = Faraday . new ( url : url )
5553 conn . headers = headers
56- conn . basic_auth ( username , password )
54+ product = path . split ( '/' ) [ 0 ]
55+ get_credentials ( product )
56+ conn . basic_auth ( @username , @password )
5757 response = conn . post ( path )
5858 begin
5959 JSON . parse ( response . body )
@@ -62,6 +62,20 @@ def get_auth_token(path, subscription_key)
6262 end
6363 end
6464
65+ def get_credentials ( product )
66+ case product
67+ when 'collection'
68+ @username = Momoapi . config . collection_user_id
69+ @password = Momoapi . config . collection_api_secret
70+ when 'disbursement'
71+ @username = Momoapi . config . disbursement_user_id
72+ @password = Momoapi . config . disbursement_api_secret
73+ when 'remittance'
74+ @username = Momoapi . config . remittance_user_id
75+ @password = Momoapi . config . remittance_api_secret
76+ end
77+ end
78+
6579 def prepare_get_request ( path , subscription_key )
6680 headers = {
6781 "X-Target-Environment" : Momoapi . config . environment || 'sandbox' ,
0 commit comments