-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sparkplug/develop
Update master - add client and collections
- Loading branch information
Showing
23 changed files
with
389 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Documentation: | ||
Enabled: false | ||
Naming/FileName: | ||
Enabled: false | ||
Metrics/MethodLength: | ||
Max: 25 | ||
Metrics/ParameterLists: | ||
Max: 8 | ||
Lint/DuplicateMethods: | ||
Enabled: false | ||
Naming/AccessorMethodName: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
source "https://rubygems.org" | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in momoapi-ruby.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
momoapi-ruby (0.1.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
coderay (1.1.2) | ||
diff-lcs (1.3) | ||
faraday (1.0.0) | ||
multipart-post (>= 1.2, < 3) | ||
method_source (0.9.2) | ||
multipart-post (2.1.1) | ||
pry (0.12.2) | ||
coderay (~> 1.1.0) | ||
method_source (~> 0.9.0) | ||
rake (10.5.0) | ||
rspec (3.9.0) | ||
rspec-core (~> 3.9.0) | ||
rspec-expectations (~> 3.9.0) | ||
rspec-mocks (~> 3.9.0) | ||
rspec-core (3.9.1) | ||
rspec-support (~> 3.9.1) | ||
rspec-expectations (3.9.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.9.0) | ||
rspec-mocks (3.9.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.9.0) | ||
rspec-support (3.9.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
bundler (~> 2.0) | ||
faraday | ||
momoapi-ruby! | ||
pry (~> 0.12) | ||
rake (~> 10.0) | ||
rspec (~> 3.0) | ||
|
||
BUNDLED WITH | ||
2.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/setup' | ||
require 'momoapi-ruby' | ||
require 'optparse' | ||
require_relative '../lib/momoapi-ruby/cli' | ||
|
||
options = {} | ||
OptionParser.new do |opts| | ||
opts.banner = 'Usage: momoapi --host host --key key' | ||
|
||
opts.on('-h', '--host=HOST', String, | ||
'Provider callback host') do |v| | ||
options[:host] = v | ||
end | ||
|
||
opts.on('-k', '--key=KEY', String, | ||
'Subscription key') do |v| | ||
options[:key] = v | ||
end | ||
end.parse! | ||
|
||
Momoapi::CLI.new(options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'momoapi-ruby/config' | ||
require 'momoapi-ruby/version' | ||
require 'momoapi-ruby/cli' | ||
require 'momoapi-ruby/collection' | ||
|
||
module Momoapi | ||
class << self | ||
attr_accessor :config | ||
end | ||
|
||
def self.config | ||
@config ||= Config.new | ||
end | ||
|
||
def self.reset | ||
@config = Config.new | ||
end | ||
|
||
def self.configure | ||
yield(config) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'momoapi-ruby' | ||
require 'faraday' | ||
require 'json' | ||
|
||
module Momoapi | ||
class CLI | ||
def initialize(option) | ||
@uuid = Faraday.get('https://www.uuidgenerator.net/api/version4').body.chomp | ||
@host = option[:host] | ||
@key = option[:key] | ||
create_sandbox_user | ||
end | ||
|
||
def create_sandbox_user | ||
body = { "providerCallbackHost": @host } | ||
@url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser' | ||
response = Faraday.post(@url) do |req| | ||
req.headers['Content-Type'] = 'application/json' | ||
req.headers['X-Reference-Id'] = @uuid | ||
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 | ||
end | ||
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' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'momoapi-ruby/config' | ||
require 'momoapi-ruby/request' | ||
|
||
module Momoapi | ||
class Client | ||
def initialize; end | ||
|
||
def get_auth_token(path, subscription_key) | ||
headers = { | ||
"Ocp-Apim-Subscription-Key": subscription_key | ||
} | ||
body = {} | ||
r = Request.new('post', path, headers, body) | ||
r.send_request | ||
end | ||
|
||
def get_balance(path, subscription_key) | ||
headers = { | ||
"X-Target-Environment": 'sandbox', | ||
"Content-Type": 'application/json', | ||
"Ocp-Apim-Subscription-Key": subscription_key | ||
} | ||
body = {} | ||
r = Request.new('get', path, headers, body) | ||
r.send_request | ||
end | ||
|
||
def get_transaction_status(_transaction_id, path, subscription_key) | ||
headers = { | ||
"X-Target-Environment": 'sandbox', | ||
"Content-Type": 'application/json', | ||
"Ocp-Apim-Subscription-Key": subscription_key | ||
} | ||
body = {} | ||
Request.new('get', path, headers, body).send_request | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'momoapi-ruby/config' | ||
require 'momoapi-ruby/client' | ||
|
||
module Momoapi | ||
class Collection < Client | ||
def get_auth_token | ||
path = 'collection/token/' | ||
super(path, Momoapi.config.collection_primary_key) | ||
end | ||
|
||
def get_balance | ||
path = '/collection/v1_0/account/balance/' | ||
super(path, Momoapi.config.collection_primary_key) | ||
end | ||
|
||
def get_transaction_status(transaction_id) | ||
path = "/collection/v1_0/requesttopay/#{transaction_id}" | ||
super(path, Momoapi.config.collection_primary_key) | ||
end | ||
|
||
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 | ||
headers = { | ||
"X-Target-Environment": 'sandbox', | ||
"Content-Type": 'application/json', | ||
"X-Reference-Id": uuid, | ||
"Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key | ||
} | ||
body = { | ||
"payer": { | ||
"partyIdType": 'MSISDN', | ||
"partyId": phone_number | ||
}, | ||
"payeeNote": payee_note, | ||
"payerMessage": payer_message, | ||
"externalId": external_id, | ||
"currency": currency, | ||
"amount": amount.to_s | ||
} | ||
path = '/collection/v1_0/requesttopay/' | ||
r = Request.new('post', path, headers, body) | ||
r.send_request | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Momoapi | ||
class Config | ||
attr_accessor :environment, :base_url, | ||
:callback_host, :collection_primary_key, | ||
:collection_user_id, :collection_api_secret | ||
|
||
def initialize | ||
@environment = nil | ||
@base_url = nil | ||
@callback_host = nil | ||
@collection_primary_key = nil | ||
@collection_user_id = nil | ||
@collection_api_secret = nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class APIError | ||
end |
Oops, something went wrong.