From e939569c6be76af4255dc2344c45c90c8114dcca Mon Sep 17 00:00:00 2001 From: NLSanyu Date: Tue, 3 Mar 2020 15:55:55 +0300 Subject: [PATCH 1/2] Fix sending of post request body --- lib/momoapi-ruby/client.rb | 4 ++-- lib/momoapi-ruby/collection.rb | 6 ++---- lib/momoapi-ruby/config.rb | 7 ++++++- lib/momoapi-ruby/disbursement.rb | 8 +++----- lib/momoapi-ruby/errors.rb | 2 +- lib/momoapi-ruby/remittance.rb | 10 ++++------ spec/features/collection_spec.rb | 2 +- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/momoapi-ruby/client.rb b/lib/momoapi-ruby/client.rb index 6ecefae..0f4a746 100644 --- a/lib/momoapi-ruby/client.rb +++ b/lib/momoapi-ruby/client.rb @@ -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 @@ -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 diff --git a/lib/momoapi-ruby/collection.rb b/lib/momoapi-ruby/collection.rb index 0240164..279b5a9 100644 --- a/lib/momoapi-ruby/collection.rb +++ b/lib/momoapi-ruby/collection.rb @@ -33,7 +33,7 @@ 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', @@ -41,9 +41,7 @@ def request_to_pay(phone_number, amount, external_id, "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', diff --git a/lib/momoapi-ruby/config.rb b/lib/momoapi-ruby/config.rb index 53c07b8..5670d66 100644 --- a/lib/momoapi-ruby/config.rb +++ b/lib/momoapi-ruby/config.rb @@ -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, @@ -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 diff --git a/lib/momoapi-ruby/disbursement.rb b/lib/momoapi-ruby/disbursement.rb index 0e7ee62..a27e554 100644 --- a/lib/momoapi-ruby/disbursement.rb +++ b/lib/momoapi-ruby/disbursement.rb @@ -28,7 +28,7 @@ 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', @@ -36,11 +36,9 @@ def transfer(phone_number, amount, external_id, "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 }, diff --git a/lib/momoapi-ruby/errors.rb b/lib/momoapi-ruby/errors.rb index f229521..ee8910a 100644 --- a/lib/momoapi-ruby/errors.rb +++ b/lib/momoapi-ruby/errors.rb @@ -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 diff --git a/lib/momoapi-ruby/remittance.rb b/lib/momoapi-ruby/remittance.rb index 2a008ca..11d2cf1 100644 --- a/lib/momoapi-ruby/remittance.rb +++ b/lib/momoapi-ruby/remittance.rb @@ -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 }, diff --git a/spec/features/collection_spec.rb b/spec/features/collection_spec.rb index fc30a6e..6bcfe8e 100644 --- a/spec/features/collection_spec.rb +++ b/spec/features/collection_spec.rb @@ -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 From caf04b88aa9031f8420f19d258a15b7155d9aa66 Mon Sep 17 00:00:00 2001 From: NLSanyu Date: Tue, 3 Mar 2020 18:33:39 +0300 Subject: [PATCH 2/2] Make user credentials specific to product --- lib/momoapi-ruby/client.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/momoapi-ruby/client.rb b/lib/momoapi-ruby/client.rb index 0f4a746..51d0702 100644 --- a/lib/momoapi-ruby/client.rb +++ b/lib/momoapi-ruby/client.rb @@ -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) @@ -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',