Skip to content

Commit eca3999

Browse files
authored
Merge pull request #14 from sparkplug/develop
Fix POST requests not sending body
2 parents a2d760c + b9cdb07 commit eca3999

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

lib/momoapi-ruby/client.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
module 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',

lib/momoapi-ruby/collection.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ def get_transaction_status(transaction_id)
3333
# by using `get_transation_status`
3434
def request_to_pay(phone_number, amount, external_id,
3535
payee_note = '', payer_message = '',
36-
currency = 'EUR', **options)
36+
currency = 'EUR', callback_url = '')
3737
uuid = SecureRandom.uuid
3838
headers = {
3939
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
4040
"Content-Type": 'application/json',
4141
"X-Reference-Id": uuid,
4242
"Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
4343
}
44-
if options[:callback_url]
45-
headers['X-Callback-Url'] = options[:callback_url]
46-
end
44+
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
4745
body = {
4846
"payer": {
4947
"partyIdType": 'MSISDN',

lib/momoapi-ruby/config.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
module Momoapi
77
class Config
8-
attr_accessor :environment, :base_url,
8+
attr_writer :base_url
9+
attr_accessor :environment,
910
:callback_host, :collection_primary_key,
1011
:collection_user_id, :collection_api_secret,
1112
:disbursement_primary_key, :disbursement_user_id,
@@ -26,5 +27,9 @@ def initialize
2627
@remittance_user_id = nil
2728
@remittance_api_secret = nil
2829
end
30+
31+
def base_url
32+
@base_url || 'https://ericssonbasicapi2.azure-api.net'
33+
end
2934
end
3035
end

lib/momoapi-ruby/disbursement.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ def get_transaction_status(transaction_id)
2828
# by using `get_transation_status`
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
31-
currency = 'EUR', **options)
31+
currency = 'EUR', callback_url = '')
3232
uuid = SecureRandom.uuid
3333
headers = {
3434
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
3535
"Content-Type": 'application/json',
3636
"X-Reference-Id": uuid,
3737
"Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
3838
}
39-
if options[:callback_url]
40-
headers['X-Callback-Url'] = options[:callback_url]
41-
end
39+
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
4240
body = {
43-
"payer": {
41+
"payee": {
4442
"partyIdType": 'MSISDN',
4543
"partyId": phone_number
4644
},

lib/momoapi-ruby/errors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Error
66
class APIError < StandardError
77
def initialize(message, code)
88
@code = code
9-
super("Error - code #{code}, message: #{message}")
9+
super("Error code #{code} #{message}")
1010
end
1111
end
1212
end

lib/momoapi-ruby/remittance.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ def get_transaction_status(transaction_id)
2828
# by using `get_transation_status`
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
31-
currency = 'EUR', **options)
31+
currency = 'EUR', callback_url = '')
3232
uuid = SecureRandom.uuid
3333
headers = {
3434
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
3535
"Content-Type": 'application/json',
3636
"X-Reference-Id": uuid,
37-
"Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
37+
"Ocp-Apim-Subscription-Key": Momoapi.config.remittance_primary_key
3838
}
39-
if options[:callback_url]
40-
headers['X-Callback-Url'] = options[:callback_url]
41-
end
39+
headers['X-Callback-Url'] = callback_url unless callback_url.empty?
4240
body = {
43-
"payer": {
41+
"payee": {
4442
"partyIdType": 'MSISDN',
4543
"partyId": phone_number
4644
},

spec/features/collection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
end
1515

1616
describe 'collections', vcr: { record: :new_episodes } do
17-
it 'checks is user is active' do
17+
it 'checks if user is active' do
1818
response = Momoapi::Collection.new.is_user_active('0243656543')
1919
expect(response[:code]).to eq(200)
2020
end

0 commit comments

Comments
 (0)