Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/momoapi-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

module Momoapi
class Client
def send_request(method, path, headers, *_body)
def send_request(method, path, headers, body = {})
auth_token = get_auth_token['access_token']
conn = Faraday.new(url: Momoapi.config.base_url)
conn.headers = headers
Expand All @@ -21,7 +21,7 @@ def send_request(method, path, headers, *_body)
when 'get'
response = conn.get(path)
when 'post'
response = conn.post(path)
response = conn.post(path, body.to_json)
end
interpret_response(response)
end
Expand All @@ -48,12 +48,12 @@ def get_auth_token(path, subscription_key)
headers = {
"Ocp-Apim-Subscription-Key": subscription_key
}
username = Momoapi.config.collection_user_id
password = Momoapi.config.collection_api_secret
url = Momoapi.config.base_url
conn = Faraday.new(url: url)
conn.headers = headers
conn.basic_auth(username, password)
product = path.split('/')[0]
get_credentials(product)
conn.basic_auth(@username, @password)
response = conn.post(path)
begin
JSON.parse(response.body)
Expand All @@ -62,6 +62,20 @@ def get_auth_token(path, subscription_key)
end
end

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

def prepare_get_request(path, subscription_key)
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
Expand Down
6 changes: 2 additions & 4 deletions lib/momoapi-ruby/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ def get_transaction_status(transaction_id)
# by using `get_transation_status`
def request_to_pay(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', **options)
currency = 'EUR', callback_url = '')
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
"Content-Type": 'application/json',
"X-Reference-Id": uuid,
"Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
}
if options[:callback_url]
headers['X-Callback-Url'] = options[:callback_url]
end
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
body = {
"payer": {
"partyIdType": 'MSISDN',
Expand Down
7 changes: 6 additions & 1 deletion lib/momoapi-ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

module Momoapi
class Config
attr_accessor :environment, :base_url,
attr_writer :base_url
attr_accessor :environment,
:callback_host, :collection_primary_key,
:collection_user_id, :collection_api_secret,
:disbursement_primary_key, :disbursement_user_id,
Expand All @@ -26,5 +27,9 @@ def initialize
@remittance_user_id = nil
@remittance_api_secret = nil
end

def base_url
@base_url || 'https://ericssonbasicapi2.azure-api.net'
end
end
end
8 changes: 3 additions & 5 deletions lib/momoapi-ruby/disbursement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ def get_transaction_status(transaction_id)
# by using `get_transation_status`
def transfer(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', **options)
currency = 'EUR', callback_url = '')
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
"Content-Type": 'application/json',
"X-Reference-Id": uuid,
"Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
}
if options[:callback_url]
headers['X-Callback-Url'] = options[:callback_url]
end
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
body = {
"payer": {
"payee": {
"partyIdType": 'MSISDN',
"partyId": phone_number
},
Expand Down
2 changes: 1 addition & 1 deletion lib/momoapi-ruby/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Error
class APIError < StandardError
def initialize(message, code)
@code = code
super("Error - code #{code}, message: #{message}")
super("Error code #{code} #{message}")
end
end
end
10 changes: 4 additions & 6 deletions lib/momoapi-ruby/remittance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ def get_transaction_status(transaction_id)
# by using `get_transation_status`
def transfer(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', **options)
currency = 'EUR', callback_url = '')
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
"Content-Type": 'application/json',
"X-Reference-Id": uuid,
"Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
"Ocp-Apim-Subscription-Key": Momoapi.config.remittance_primary_key
}
if options[:callback_url]
headers['X-Callback-Url'] = options[:callback_url]
end
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
body = {
"payer": {
"payee": {
"partyIdType": 'MSISDN',
"partyId": phone_number
},
Expand Down
2 changes: 1 addition & 1 deletion spec/features/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

describe 'collections', vcr: { record: :new_episodes } do
it 'checks is user is active' do
it 'checks if user is active' do
response = Momoapi::Collection.new.is_user_active('0243656543')
expect(response[:code]).to eq(200)
end
Expand Down