diff --git a/lib/momoapi-ruby/cli.rb b/lib/momoapi-ruby/cli.rb index 0d58696..5b31b1f 100644 --- a/lib/momoapi-ruby/cli.rb +++ b/lib/momoapi-ruby/cli.rb @@ -1,13 +1,17 @@ # frozen_string_literal: true -require 'momoapi-ruby' require 'faraday' require 'json' +require 'securerandom' + +require 'momoapi-ruby/config' +require 'momoapi-ruby/errors' +require 'momoapi-ruby' module Momoapi class CLI def initialize(option) - @uuid = Faraday.get('https://www.uuidgenerator.net/api/version4').body.chomp + @uuid = SecureRandom.uuid @host = option[:host] @key = option[:key] create_sandbox_user @@ -22,27 +26,27 @@ def create_sandbox_user req.headers['Ocp-Apim-Subscription-Key'] = @key req.body = body.to_json end - if response.status == 201 - generate_api_key - else - # TO DO: Add error handling here - puts response.body + + unless response.status == 201 + raise Error::APIError.new(response.body, response.status) end + + generate_api_key end def generate_api_key @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' + @uuid + '/apikey' - puts @url response = Faraday.post(@url) do |req| req.headers['Ocp-Apim-Subscription-Key'] = @key end - if response.status == 201 - puts " User ID: #{@uuid} \n API key: #{response.body}" - else - # TO DO: Add error handling here - puts 'Error creating API key' + + unless response.status == 201 + raise Error::APIError.new(response.body, response.status) end + + key = JSON.parse(response.body) + puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n" end end end diff --git a/lib/momoapi-ruby/client.rb b/lib/momoapi-ruby/client.rb index a938433..9b7df84 100644 --- a/lib/momoapi-ruby/client.rb +++ b/lib/momoapi-ruby/client.rb @@ -24,18 +24,18 @@ def send_request(method, path, headers, *_body) def interpret_response(resp) response = { - body: JSON.parse(resp.body), + body: resp.body, code: resp.status } - # unless 200 <= resp.status && resp.status < 300 - # handle_error(response[:code], response[:body]) - # end + unless resp.status >= 200 && resp.status < 300 + handle_error(response[:body], response[:status]) + end response end - # def handle_error(response_code, response_body) - # raise APIError(response_code, response_body) - # end + def handle_error(response_body, response_code) + raise Error::APIError.new(response_body, response_code) + end def get_auth_token(path, subscription_key) headers = { @@ -53,7 +53,7 @@ def get_auth_token(path, subscription_key) def get_balance(path, subscription_key) headers = { - "X-Target-Environment": 'sandbox', + "X-Target-Environment": Momoapi.config.environment || 'sandbox', "Content-Type": 'application/json', "Ocp-Apim-Subscription-Key": subscription_key } @@ -62,7 +62,7 @@ def get_balance(path, subscription_key) def get_transaction_status(path, subscription_key) headers = { - "X-Target-Environment": 'sandbox', + "X-Target-Environment": Momoapi.config.environment || 'sandbox', "Content-Type": 'application/json', "Ocp-Apim-Subscription-Key": subscription_key } diff --git a/lib/momoapi-ruby/collection.rb b/lib/momoapi-ruby/collection.rb index 235e7f9..b0e2ef2 100644 --- a/lib/momoapi-ruby/collection.rb +++ b/lib/momoapi-ruby/collection.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'securerandom' + require 'momoapi-ruby/config' require 'momoapi-ruby/client' @@ -22,9 +24,9 @@ def get_transaction_status(transaction_id) def request_to_pay(phone_number, amount, external_id, payee_note = '', payer_message = '', currency = 'EUR') - uuid = Faraday.get('https://www.uuidgenerator.net/api/version4').body.chomp + uuid = SecureRandom.uuid headers = { - "X-Target-Environment": 'sandbox', + "X-Target-Environment": Momoapi.config.environment || 'sandbox', "Content-Type": 'application/json', "X-Reference-Id": uuid, "Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key @@ -42,6 +44,7 @@ def request_to_pay(phone_number, amount, external_id, } path = '/collection/v1_0/requesttopay' send_request('post', path, headers, body) + uuid end end end diff --git a/lib/momoapi-ruby/errors.rb b/lib/momoapi-ruby/errors.rb index 9922db4..15f7c87 100644 --- a/lib/momoapi-ruby/errors.rb +++ b/lib/momoapi-ruby/errors.rb @@ -1,4 +1,10 @@ # frozen_string_literal: true -class APIError < StandardError +module Error + class APIError < StandardError + def initialize(message, code) + @code = code + super(message) + end + end end