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
30 changes: 17 additions & 13 deletions lib/momoapi-ruby/cli.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
18 changes: 9 additions & 9 deletions lib/momoapi-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions lib/momoapi-ruby/collection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'securerandom'

require 'momoapi-ruby/config'
require 'momoapi-ruby/client'

Expand All @@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion lib/momoapi-ruby/errors.rb
Original file line number Diff line number Diff line change
@@ -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