momoapi-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 84233d3a948cd641d3664e290b10789c97728009ed2e50de4c8a663581dcb625
4
+ data.tar.gz: '0764955b5e484a2cc4e727c4365101dbc19d021d10582a6e4ab7bc734495f974'
5
+ SHA512:
6
+ metadata.gz: 2c0edeccf9353280687071eb002fb0e45716102214575185f8effd38c9777267df1bb3d4bff3ff3f781f68eded6b1e1fc551f535e330cacd564b16246edf359f
7
+ data.tar.gz: d37405e1e743ba3bf3beb9c8bccf901a6ac3ff61db093ac5db16622696f015fac7b50cd53456113191d3a96da3c5cb05347d2d91351b791c36fb8fca11cd363a
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ repo_token: bYVvifY6POXhFwOja3SZIX1nUxtWrUxKj
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ Documentation:
2
+ Enabled: false
3
+ Naming/FileName:
4
+ Enabled: false
5
+ Metrics/MethodLength:
6
+ Max: 25
7
+ Metrics/BlockLength:
8
+ Max: 35
9
+ Metrics/ParameterLists:
10
+ Max: 8
11
+ Lint/DuplicateMethods:
12
+ Enabled: false
13
+ Naming/AccessorMethodName:
14
+ Enabled: false
15
+ Naming/PredicateName:
16
+ Enabled: false
17
+ Style/AsciiComments:
18
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.2
8
+ git:
9
+ depth: 25
10
+ quiet: true
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at lydiansanyu@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in momoapi-ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ momoapi-ruby (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ coderay (1.1.2)
12
+ coveralls (0.8.23)
13
+ json (>= 1.8, < 3)
14
+ simplecov (~> 0.16.1)
15
+ term-ansicolor (~> 1.3)
16
+ thor (>= 0.19.4, < 2.0)
17
+ tins (~> 1.6)
18
+ crack (0.4.3)
19
+ safe_yaml (~> 1.0.0)
20
+ diff-lcs (1.3)
21
+ docile (1.3.2)
22
+ faraday (1.0.0)
23
+ multipart-post (>= 1.2, < 3)
24
+ hashdiff (1.0.0)
25
+ json (2.3.0)
26
+ method_source (0.9.2)
27
+ multipart-post (2.1.1)
28
+ pry (0.12.2)
29
+ coderay (~> 1.1.0)
30
+ method_source (~> 0.9.0)
31
+ public_suffix (4.0.3)
32
+ rake (10.5.0)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.1)
38
+ rspec-support (~> 3.9.1)
39
+ rspec-expectations (3.9.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.2)
46
+ safe_yaml (1.0.5)
47
+ simplecov (0.16.1)
48
+ docile (~> 1.1)
49
+ json (>= 1.8, < 3)
50
+ simplecov-html (~> 0.10.0)
51
+ simplecov-html (0.10.2)
52
+ sync (0.5.0)
53
+ term-ansicolor (1.7.1)
54
+ tins (~> 1.0)
55
+ thor (1.0.1)
56
+ tins (1.24.1)
57
+ sync
58
+ vcr (5.1.0)
59
+ webmock (2.3.2)
60
+ addressable (>= 2.3.6)
61
+ crack (>= 0.3.2)
62
+ hashdiff
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ bundler (~> 2.0)
69
+ coveralls (~> 0.8.15)
70
+ faraday
71
+ momoapi-ruby!
72
+ pry (~> 0.12)
73
+ rake (~> 10.0)
74
+ rspec (~> 3.0)
75
+ vcr (~> 5.1)
76
+ webmock (~> 2.1)
77
+
78
+ BUNDLED WITH
79
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 LydiaSanyu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,181 @@
1
+ # MTN MoMo API Ruby Gem
2
+
3
+ [![Build Status](https://travis-ci.com/sparkplug/momoapi-ruby.svg?branch=master)](https://travis-ci.com/sparkplug/momoapi-ruby)
4
+ [![Coverage Status](https://coveralls.io/repos/github/sparkplug/momoapi-ruby/badge.svg?branch=master)](https://coveralls.io/github/sparkplug/momoapi-ruby?branch=master)
5
+ [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/momo-api-developers/)
6
+
7
+ ## Usage
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'momoapi-ruby'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install momoapi-ruby
24
+
25
+
26
+ ## Creating a sandbox environment API user
27
+ Type this command into your terminal:
28
+
29
+ ```
30
+ momoapi-ruby --host Your-Provider-Callback-Host --key Your-Ocp-Apim-Subscription-Key
31
+ ```
32
+
33
+ A User ID and API Key will be generated:
34
+
35
+ ```
36
+ User ID: Generated user ID
37
+ API key: Generated API key
38
+ ```
39
+
40
+ ## Using live credentials
41
+ Add the following configurations in an initializer file (for example, `config/initializers/momoapi-ruby.rb` in a Rails app):
42
+
43
+ ```
44
+ Momoapi.configure do |config|
45
+ config.base_url = 'Your MoMo account base URL'
46
+ config.callback_host = 'Your Provider Callback Host'
47
+ end
48
+ ```
49
+
50
+ ## Collections
51
+ The collections client can be created with the following paramaters. Note that the `COLLECTION_USER_ID` and `COLLECTION_API_SECRET` for production are provided on the MTN OVA dashboard.
52
+
53
+ Add the following to your configuration block:
54
+ ```
55
+ config.collection_primary_key = 'Your Collection Subscription Key'
56
+ config.collection_user_id = 'Your Collection User ID'
57
+ config.collection_api_secret = 'Your Collection API Key'
58
+ ```
59
+
60
+ * `collection_primary_key`: Primary Key for the `Collection` product on the developer portal.
61
+ * `collection_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
62
+ * `collection_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.
63
+
64
+ You can create a collection client with the following:
65
+
66
+ ```ruby
67
+ require 'momoapi-ruby'
68
+
69
+ collection = Momoapi::Collection.new
70
+ ```
71
+
72
+ ### Methods
73
+ 1. `request_to_pay`: This operation is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction is executed once the payer has authorized the payment. The transaction will be in status PENDING until it is authorized or declined by the payer or it is timed out by the system. Status of the transaction can be validated by using `get_transaction_status`.
74
+
75
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_id` returned by `request_to_pay`. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error.
76
+
77
+ 3. `get_balance`: Get the balance of the account.
78
+
79
+ ### Sample Code
80
+
81
+ ```ruby
82
+ require 'momoapi-ruby'
83
+
84
+ collection = Momoapi::Collection.new
85
+ collection.request_to_pay(
86
+ mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
87
+ ```
88
+ An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
89
+
90
+ ## Disbursements
91
+ The disbursements client can be created with the following paramaters. The `DISBURSEMENT_USER_ID` and `DISBURSEMENT_API_SECRET` for production are provided on the MTN OVA dashboard.
92
+
93
+ Add the following to your configuration block:
94
+ ```
95
+ config.disbursement_primary_key = 'Your Disbursement Subscription Key'
96
+ config.disbursement_user_id = 'Your Disbursement User ID'
97
+ config.disbursement_api_secret = 'Your Disbursement API Key'
98
+ ```
99
+
100
+ * `disbursement_primary_key`: Primary Key for the `Disbursement` product on the developer portal.
101
+ * `disbursement_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
102
+ * `disbursement_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.
103
+
104
+ You can create a disbursement client with the following:
105
+
106
+ ```ruby
107
+ require 'momoapi-ruby'
108
+
109
+ disbursement = Momoapi::Disbursement.new
110
+ ```
111
+
112
+ ### Methods
113
+ 1. `transfer`: Used to transfer an amount from the owner’s account to a payee account. Status of the transaction can be validated by using the `get_transaction_status` method.
114
+
115
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_id` returned by `transfer`. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error.
116
+
117
+ 3. `get_balance`: Get the balance of the account.
118
+
119
+ ### Sample Code
120
+
121
+ ```ruby
122
+ require 'momoapi-ruby'
123
+
124
+ disbursement = Momoapi::Disbursement.new
125
+ disbursement.transfer(
126
+ mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
127
+ ```
128
+ An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
129
+
130
+ ## Remittances
131
+ The remittances client can be created with the following paramaters. The `REMITTANCES_USER_ID` and `REMITTANCES_API_SECRET` for production are provided on the MTN OVA dashboard.
132
+
133
+ Add the following to your configuration block:
134
+ ```
135
+ config.remittance_primary_key = 'Your Remittance Subscription Key'
136
+ config.remittance_user_id = 'Your Remittance User ID'
137
+ config.remittance_api_secret = 'Your Remittance API Key'
138
+ ```
139
+
140
+ * `remittance_primary_key`: Primary Key for the `Remittance` product on the developer portal.
141
+ * `remittance_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
142
+ * `remittance_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.
143
+
144
+ You can create a remittance client with the following:
145
+
146
+ ```ruby
147
+ require 'momoapi-ruby'
148
+
149
+ remittance = Momoapi::Remittance.new
150
+ ```
151
+
152
+ ### Methods
153
+ 1. `transfer`: Used to transfer an amount from the owner’s account to a payee account. Status of the transaction can be validated by using the `get_transaction_status` method.
154
+
155
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_id` returned by `transfer`. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error.
156
+
157
+ 3. `get_balance`: Get the balance of the account.
158
+
159
+ ### Sample Code
160
+
161
+ ```ruby
162
+ require 'momoapi-ruby'
163
+
164
+ remittance = Momoapi::Remittance.new
165
+ remittance.transfer(
166
+ mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
167
+ ```
168
+ An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
169
+
170
+
171
+ ## Contributing
172
+
173
+ 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.
174
+
175
+ ## License
176
+
177
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
178
+
179
+ ## Code of Conduct
180
+
181
+ 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).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'momoapi-ruby'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/momoapi-ruby ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'momoapi-ruby'
6
+ require 'optparse'
7
+ require_relative '../lib/momoapi-ruby/cli'
8
+
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.banner = 'Usage: momoapi --host host --key key'
12
+
13
+ opts.on('-h', '--host=HOST', String,
14
+ 'Provider callback host') do |v|
15
+ options[:host] = v
16
+ end
17
+
18
+ opts.on('-k', '--key=KEY', String,
19
+ 'Subscription key') do |v|
20
+ options[:key] = v
21
+ end
22
+ end.parse!
23
+
24
+ Momoapi::CLI.new(options)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is an executable file allowing a user to use the command line interface
4
+ # to pass in a callback host url and a subscription key
5
+ # and get back a generated user id and API key
6
+
7
+ require 'faraday'
8
+ require 'json'
9
+ require 'securerandom'
10
+
11
+ require 'momoapi-ruby/config'
12
+ require 'momoapi-ruby/errors'
13
+ require 'momoapi-ruby'
14
+
15
+ module Momoapi
16
+ class CLI
17
+ def initialize(option)
18
+ @uuid = SecureRandom.uuid
19
+ @host = option[:host]
20
+ @key = option[:key]
21
+ create_sandbox_user
22
+ end
23
+
24
+ def create_sandbox_user
25
+ body = { "providerCallbackHost": @host }
26
+ @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser'
27
+ response = Faraday.post(@url) do |req|
28
+ req.headers['Content-Type'] = 'application/json'
29
+ req.headers['X-Reference-Id'] = @uuid
30
+ req.headers['Ocp-Apim-Subscription-Key'] = @key
31
+ req.body = body.to_json
32
+ end
33
+
34
+ unless response.status == 201
35
+ raise Error::APIError.new(response.body, response.status)
36
+ end
37
+
38
+ generate_api_key
39
+ end
40
+
41
+ def generate_api_key
42
+ @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' +
43
+ @uuid + '/apikey'
44
+ response = Faraday.post(@url) do |req|
45
+ req.headers['Ocp-Apim-Subscription-Key'] = @key
46
+ end
47
+
48
+ unless response.status == 201
49
+ raise Error::APIError.new(response.body, response.status)
50
+ end
51
+
52
+ key = JSON.parse(response.body)
53
+ puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Base implementation of the MTN API client
4
+
5
+ # Includes methods common to collections, disbursements and remittances
6
+
7
+ require 'faraday'
8
+
9
+ require 'momoapi-ruby/config'
10
+ require 'momoapi-ruby/errors'
11
+
12
+ module Momoapi
13
+ class Client
14
+ def send_request(method, path, headers, *_body)
15
+ auth_token = get_auth_token['access_token']
16
+ conn = Faraday.new(url: Momoapi.config.base_url)
17
+ conn.headers = headers
18
+ conn.authorization(:Bearer, auth_token)
19
+
20
+ case method
21
+ when 'get'
22
+ response = conn.get(path)
23
+ when 'post'
24
+ response = conn.post(path)
25
+ end
26
+ interpret_response(response)
27
+ end
28
+
29
+ def interpret_response(resp)
30
+ body = resp.body.empty? ? '' : JSON.parse(resp.body)
31
+ response = {
32
+ body: body,
33
+ code: resp.status
34
+ }
35
+ unless resp.status >= 200 && resp.status < 300
36
+ handle_error(response[:body], response[:code])
37
+ end
38
+ response
39
+ end
40
+
41
+ def handle_error(response_body, response_code)
42
+ raise Error::APIError.new(response_body, response_code)
43
+ end
44
+
45
+ # Create an access token which can then be used to
46
+ # authorize and authenticate towards the other end-points of the API
47
+ def get_auth_token(path, subscription_key)
48
+ headers = {
49
+ "Ocp-Apim-Subscription-Key": subscription_key
50
+ }
51
+ username = Momoapi.config.collection_user_id
52
+ password = Momoapi.config.collection_api_secret
53
+ url = Momoapi.config.base_url
54
+ conn = Faraday.new(url: url)
55
+ conn.headers = headers
56
+ conn.basic_auth(username, password)
57
+ response = conn.post(path)
58
+ begin
59
+ JSON.parse(response.body)
60
+ rescue JSON::ParserError
61
+ response.body.to_s
62
+ end
63
+ end
64
+
65
+ def prepare_get_request(path, subscription_key)
66
+ headers = {
67
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
68
+ "Content-Type": 'application/json',
69
+ "Ocp-Apim-Subscription-Key": subscription_key
70
+ }
71
+ send_request('get', path, headers)
72
+ end
73
+
74
+ # get the balance on an account
75
+ def get_balance(path, subscription_key)
76
+ prepare_get_request(path, subscription_key)
77
+ end
78
+
79
+ # retrieve transaction information, for transfer and payments
80
+ def get_transaction_status(path, subscription_key)
81
+ prepare_get_request(path, subscription_key)
82
+ end
83
+
84
+ # check if an account holder is registered and active in the system
85
+ def is_user_active(path, subscription_key)
86
+ prepare_get_request(path, subscription_key)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Implementation of the MTN API collections client
4
+
5
+ require 'securerandom'
6
+
7
+ require 'momoapi-ruby/config'
8
+ require 'momoapi-ruby/client'
9
+
10
+ module Momoapi
11
+ class Collection < Client
12
+ def get_auth_token
13
+ path = 'collection/token/'
14
+ super(path, Momoapi.config.collection_primary_key)
15
+ end
16
+
17
+ def get_balance
18
+ path = '/collection/v1_0/account/balance'
19
+ super(path, Momoapi.config.collection_primary_key)
20
+ end
21
+
22
+ def get_transaction_status(transaction_id)
23
+ path = "/collection/v1_0/requesttopay/#{transaction_id}"
24
+ super(path, Momoapi.config.collection_primary_key)
25
+ end
26
+
27
+ # This method is used to request a payment from a consumer (Payer).
28
+ # The payer will be asked to authorize the payment. The transaction will
29
+ # be executed once the payer has authorized the payment.
30
+ # The requesttopay will be in status PENDING until the transaction
31
+ # is authorized or declined by the payer or it is timed out by the system.
32
+ # The status of the transaction can be validated
33
+ # by using `get_transation_status`
34
+ def request_to_pay(phone_number, amount, external_id,
35
+ payee_note = '', payer_message = '',
36
+ currency = 'EUR', **options)
37
+ uuid = SecureRandom.uuid
38
+ headers = {
39
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
40
+ "Content-Type": 'application/json',
41
+ "X-Reference-Id": uuid,
42
+ "Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
43
+ }
44
+ if options[:callback_url]
45
+ headers['X-Callback-Url'] = options[:callback_url]
46
+ end
47
+ body = {
48
+ "payer": {
49
+ "partyIdType": 'MSISDN',
50
+ "partyId": phone_number
51
+ },
52
+ "payeeNote": payee_note,
53
+ "payerMessage": payer_message,
54
+ "externalId": external_id,
55
+ "currency": currency,
56
+ "amount": amount.to_s
57
+ }
58
+ path = '/collection/v1_0/requesttopay'
59
+ send_request('post', path, headers, body)
60
+ { transaction_reference: uuid }
61
+ end
62
+
63
+ def is_user_active(phone_number)
64
+ path = "/collection/v1_0/accountholder/msisdn/#{phone_number}/active"
65
+ super(path, Momoapi.config.collection_primary_key)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configurations are set up in this file
4
+ # for a user's MTN MoMo API user credentials
5
+
6
+ module Momoapi
7
+ class Config
8
+ attr_accessor :environment, :base_url,
9
+ :callback_host, :collection_primary_key,
10
+ :collection_user_id, :collection_api_secret,
11
+ :disbursement_primary_key, :disbursement_user_id,
12
+ :disbursement_api_secret, :remittance_primary_key,
13
+ :remittance_user_id, :remittance_api_secret
14
+
15
+ def initialize
16
+ @environment = nil
17
+ @base_url = nil
18
+ @callback_host = nil
19
+ @collection_primary_key = nil
20
+ @collection_user_id = nil
21
+ @collection_api_secret = nil
22
+ @disbursement_primary_key = nil
23
+ @disbursement_user_id = nil
24
+ @disbursement_api_secret = nil
25
+ @remittance_primary_key = nil
26
+ @remittance_user_id = nil
27
+ @remittance_api_secret = nil
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Implementation of the MTN API disbursements client
4
+
5
+ require 'momoapi-ruby/config'
6
+ require 'momoapi-ruby/client'
7
+
8
+ module Momoapi
9
+ class Disbursement < Client
10
+ def get_auth_token
11
+ path = 'disbursement/token/'
12
+ super(path, Momoapi.config.disbursement_primary_key)
13
+ end
14
+
15
+ def get_balance
16
+ path = '/disbursement/v1_0/account/balance'
17
+ super(path, Momoapi.config.disbursement_primary_key)
18
+ end
19
+
20
+ def get_transaction_status(transaction_id)
21
+ path = "/disbursement/v1_0/transfer/#{transaction_id}"
22
+ super(path, Momoapi.config.disbursement_primary_key)
23
+ end
24
+
25
+ # The transfer operation is used to transfer an amount from the owner’s
26
+ # account to a payee account.
27
+ # The status of the transaction can be validated
28
+ # by using `get_transation_status`
29
+ def transfer(phone_number, amount, external_id,
30
+ payee_note = '', payer_message = '',
31
+ currency = 'EUR', **options)
32
+ uuid = SecureRandom.uuid
33
+ headers = {
34
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
35
+ "Content-Type": 'application/json',
36
+ "X-Reference-Id": uuid,
37
+ "Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
38
+ }
39
+ if options[:callback_url]
40
+ headers['X-Callback-Url'] = options[:callback_url]
41
+ end
42
+ body = {
43
+ "payer": {
44
+ "partyIdType": 'MSISDN',
45
+ "partyId": phone_number
46
+ },
47
+ "payeeNote": payee_note,
48
+ "payerMessage": payer_message,
49
+ "externalId": external_id,
50
+ "currency": currency,
51
+ "amount": amount.to_s
52
+ }
53
+ path = '/disbursement/v1_0/transfer'
54
+ send_request('post', path, headers, body)
55
+ { transaction_reference: uuid }
56
+ end
57
+
58
+ def is_user_active(phone_number)
59
+ path = "/disbursement/v1_0/accountholder/msisdn/#{phone_number}/active"
60
+ super(path, Momoapi.config.disbursement_primary_key)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Error handling for unsuccessful responses from the MTN Momo API
4
+
5
+ module Error
6
+ class APIError < StandardError
7
+ def initialize(message, code)
8
+ @code = code
9
+ super("Error - code #{code}, message: #{message}")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Implementation of the MTN API remittances client
4
+
5
+ require 'momoapi-ruby/config'
6
+ require 'momoapi-ruby/client'
7
+
8
+ module Momoapi
9
+ class Remittance < Client
10
+ def get_auth_token
11
+ path = 'remittance/token/'
12
+ super(path, Momoapi.config.remittance_primary_key)
13
+ end
14
+
15
+ def get_balance
16
+ path = '/remittance/v1_0/account/balance'
17
+ super(path, Momoapi.config.remittance_primary_key)
18
+ end
19
+
20
+ def get_transaction_status(transaction_id)
21
+ path = "/remittance/v1_0/transfer/#{transaction_id}"
22
+ super(path, Momoapi.config.remittance_primary_key)
23
+ end
24
+
25
+ # The transfer operation is used to transfer an amount from the owner’s
26
+ # account to a payee account.
27
+ # The status of the transaction can be validated
28
+ # by using `get_transation_status`
29
+ def transfer(phone_number, amount, external_id,
30
+ payee_note = '', payer_message = '',
31
+ currency = 'EUR', **options)
32
+ uuid = SecureRandom.uuid
33
+ headers = {
34
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
35
+ "Content-Type": 'application/json',
36
+ "X-Reference-Id": uuid,
37
+ "Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
38
+ }
39
+ if options[:callback_url]
40
+ headers['X-Callback-Url'] = options[:callback_url]
41
+ end
42
+ body = {
43
+ "payer": {
44
+ "partyIdType": 'MSISDN',
45
+ "partyId": phone_number
46
+ },
47
+ "payeeNote": payee_note,
48
+ "payerMessage": payer_message,
49
+ "externalId": external_id,
50
+ "currency": currency,
51
+ "amount": amount.to_s
52
+ }
53
+ path = '/remittance/v1_0/transfer'
54
+ send_request('post', path, headers, body)
55
+ { transaction_reference: uuid }
56
+ end
57
+
58
+ def is_user_active(phone_number)
59
+ path = "/remittance/v1_0/accountholder/msisdn/#{phone_number}/active"
60
+ super(path, Momoapi.config.remittance_primary_key)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Momoapi
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'momoapi-ruby/config'
4
+ require 'momoapi-ruby/version'
5
+ require 'momoapi-ruby/cli'
6
+ require 'momoapi-ruby/collection'
7
+ require 'momoapi-ruby/disbursement'
8
+ require 'momoapi-ruby/remittance'
9
+
10
+ module Momoapi
11
+ class << self
12
+ attr_accessor :config
13
+ end
14
+
15
+ def self.config
16
+ @config ||= Config.new
17
+ end
18
+
19
+ def self.reset
20
+ @config = Config.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(config)
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'momoapi-ruby/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'momoapi-ruby'
9
+ spec.version = Momoapi::VERSION
10
+ spec.authors = ['Lydia Sanyu Naggayi']
11
+ spec.email = ['lydiansanyu@gmail.com']
12
+ spec.summary = 'MTN MoMo gem'
13
+ spec.description = 'MTN MoMo API Client for Ruby'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the
18
+ # RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0")
21
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'bin'
24
+ spec.executables = 'momoapi-ruby'
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 2.0'
28
+ spec.add_development_dependency 'coveralls', '~> 0.8.15'
29
+ spec.add_development_dependency 'faraday'
30
+ spec.add_development_dependency 'pry', '~> 0.12'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'vcr', '~> 5.1'
34
+ spec.add_development_dependency 'webmock', '~> 2.1'
35
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: momoapi-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lydia Sanyu Naggayi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.15
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.15
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.1'
125
+ description: MTN MoMo API Client for Ruby
126
+ email:
127
+ - lydiansanyu@gmail.com
128
+ executables:
129
+ - momoapi-ruby
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".coveralls.yml"
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".rubocop.yml"
137
+ - ".travis.yml"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - bin/console
145
+ - bin/momoapi-ruby
146
+ - bin/setup
147
+ - lib/momoapi-ruby.rb
148
+ - lib/momoapi-ruby/cli.rb
149
+ - lib/momoapi-ruby/client.rb
150
+ - lib/momoapi-ruby/collection.rb
151
+ - lib/momoapi-ruby/config.rb
152
+ - lib/momoapi-ruby/disbursement.rb
153
+ - lib/momoapi-ruby/errors.rb
154
+ - lib/momoapi-ruby/remittance.rb
155
+ - lib/momoapi-ruby/version.rb
156
+ - momoapi-ruby.gemspec
157
+ homepage:
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubygems_version: 3.0.6
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: MTN MoMo gem
180
+ test_files: []