Skip to content

Commit

Permalink
Merge pull request #3 from sparkplug/develop
Browse files Browse the repository at this point in the history
Update master - add client and collections
  • Loading branch information
NLSanyu authored Feb 18, 2020
2 parents e184d3c + e07465a commit 9c962b7
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 53 deletions.
12 changes: 12 additions & 0 deletions .rubocop.yml
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
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at lydia.sanyu@tezzasolutions.com. All
reported by contacting the project team at lydiansanyu@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
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
45 changes: 45 additions & 0 deletions Gemfile.lock
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
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[![Build Status](https://travis-ci.com/sparkplug/momoapi-ruby.svg?branch=master)](https://travis-ci.com/sparkplug/momoapi-ruby)


## Usage

## Installation

Add this line to your application's Gemfile:
Expand All @@ -18,24 +21,43 @@ Or install it yourself as:

$ gem install momoapi-ruby

## Usage

TODO: Write usage instructions here
## Creating a sandbox environment API user
Type this command into your terminal:

## Development
```
momoapi --host Your-Provider-Callback-Host --key Your-Ocp-Apim-Subscription-Key
```

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
A User ID and API Key will be generated:

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
```
User ID: Generated user ID
API key: Generated API key
```

## Using live credentials

Add the following configurations in an initializer file (for example, `config/initializers/momoapi-ruby.rb` in a Rails app):

```
Momoapi.configure do |config|
config.base_url = 'Your MoMo account base URL'
config.callback_host = 'Your Provider Callback Host'
config.collection_primary_key = 'Your Collection Subscription Key'
config.collection_user_id = 'Your Collection User ID'
config.collection_api_secret = 'Your Collection API Key'
end
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/momoapi-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
Bug reports and pull requests are welcome on GitHub at https://github.com/sparkplug/momoapi-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the Momoapi::Ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/momoapi-ruby/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the Momoapi::Ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sparkplug/momoapi-ruby/blob/master/CODE_OF_CONDUCT.md).
8 changes: 5 additions & 3 deletions Rakefile
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
7 changes: 4 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "momoapi/ruby"
require 'bundler/setup'
require 'momoapi-ruby'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +11,5 @@ require "momoapi/ruby"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
24 changes: 24 additions & 0 deletions bin/momoapi-ruby
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)
24 changes: 24 additions & 0 deletions lib/momoapi-ruby.rb
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
48 changes: 48 additions & 0 deletions lib/momoapi-ruby/cli.rb
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
40 changes: 40 additions & 0 deletions lib/momoapi-ruby/client.rb
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
48 changes: 48 additions & 0 deletions lib/momoapi-ruby/collection.rb
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
18 changes: 18 additions & 0 deletions lib/momoapi-ruby/config.rb
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
4 changes: 4 additions & 0 deletions lib/momoapi-ruby/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class APIError
end
Loading

0 comments on commit 9c962b7

Please sign in to comment.