momoapi-ruby 1.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a1d80680812051443018bc06018832938da0fd7231688c99eefa7eb6c9b5622c
4
+ data.tar.gz: 9a3d90835eec545832380ad7a19a74e80a66b69aaaed0a1cb7c911f429aff8ec
5
+ SHA512:
6
+ metadata.gz: b0e7c62674d5339870a85ecb23fb7f3ca637b54d5587c09f37e7c2de5e4319e119560fddd43a03164d9d334aac199585d0ef667e06176c7da3dcb477f5675a00
7
+ data.tar.gz: 4a5670b723903be7c104ad738d3b05ae40cb9c0723cb66e923cf683418b52d4f31613edbfe6d5f55ae6a3575020a1f11b37b06847049d16790dab678088fc3d1
@@ -0,0 +1 @@
1
+ repo_token: bYVvifY6POXhFwOja3SZIX1nUxtWrUxKj
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ momoapi-ruby (1.0.1)
5
+ faraday (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ coderay (1.1.2)
13
+ coveralls (0.8.23)
14
+ json (>= 1.8, < 3)
15
+ simplecov (~> 0.16.1)
16
+ term-ansicolor (~> 1.3)
17
+ thor (>= 0.19.4, < 2.0)
18
+ tins (~> 1.6)
19
+ crack (0.4.3)
20
+ safe_yaml (~> 1.0.0)
21
+ diff-lcs (1.3)
22
+ docile (1.3.2)
23
+ faraday (1.0.0)
24
+ multipart-post (>= 1.2, < 3)
25
+ hashdiff (1.0.0)
26
+ json (2.3.0)
27
+ method_source (0.9.2)
28
+ multipart-post (2.1.1)
29
+ pry (0.12.2)
30
+ coderay (~> 1.1.0)
31
+ method_source (~> 0.9.0)
32
+ public_suffix (4.0.3)
33
+ rake (13.0.1)
34
+ rspec (3.9.0)
35
+ rspec-core (~> 3.9.0)
36
+ rspec-expectations (~> 3.9.0)
37
+ rspec-mocks (~> 3.9.0)
38
+ rspec-core (3.9.1)
39
+ rspec-support (~> 3.9.1)
40
+ rspec-expectations (3.9.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-mocks (3.9.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-support (3.9.2)
47
+ safe_yaml (1.0.5)
48
+ simplecov (0.16.1)
49
+ docile (~> 1.1)
50
+ json (>= 1.8, < 3)
51
+ simplecov-html (~> 0.10.0)
52
+ simplecov-html (0.10.2)
53
+ sync (0.5.0)
54
+ term-ansicolor (1.7.1)
55
+ tins (~> 1.0)
56
+ thor (1.0.1)
57
+ tins (1.24.1)
58
+ sync
59
+ vcr (5.1.0)
60
+ webmock (2.3.2)
61
+ addressable (>= 2.3.6)
62
+ crack (>= 0.3.2)
63
+ hashdiff
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (~> 2.0)
70
+ coveralls (~> 0.8.15)
71
+ momoapi-ruby!
72
+ pry (~> 0.12)
73
+ rake (~> 13.0)
74
+ rspec (~> 3.0)
75
+ vcr (~> 5.1)
76
+ webmock (~> 2.1)
77
+
78
+ BUNDLED WITH
79
+ 2.0.2
@@ -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.
@@ -0,0 +1,178 @@
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 install
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. The status of the transaction can be validated by using `get_transaction_status`.
74
+
75
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_reference` 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
+
80
+ ## Disbursements
81
+ 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.
82
+
83
+ Add the following to your configuration block:
84
+ ```
85
+ config.disbursement_primary_key = 'Your Disbursement Subscription Key'
86
+ config.disbursement_user_id = 'Your Disbursement User ID'
87
+ config.disbursement_api_secret = 'Your Disbursement API Key'
88
+ ```
89
+
90
+ * `disbursement_primary_key`: Primary Key for the `Disbursement` product on the developer portal.
91
+ * `disbursement_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
92
+ * `disbursement_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.
93
+
94
+ You can create a disbursement client with the following:
95
+
96
+ ```ruby
97
+ require 'momoapi-ruby'
98
+
99
+ disbursement = Momoapi::Disbursement.new
100
+ ```
101
+
102
+ ### Methods
103
+ 1. `transfer`: Used to transfer an amount from the owner’s account to a payee account. The status of the transaction can be validated by using the `get_transaction_status` method.
104
+
105
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_reference` 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.
106
+
107
+ 3. `get_balance`: Get the balance of the account.
108
+
109
+
110
+ ## Remittances
111
+ 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.
112
+
113
+ Add the following to your configuration block:
114
+ ```
115
+ config.remittance_primary_key = 'Your Remittance Subscription Key'
116
+ config.remittance_user_id = 'Your Remittance User ID'
117
+ config.remittance_api_secret = 'Your Remittance API Key'
118
+ ```
119
+
120
+ * `remittance_primary_key`: Primary Key for the `Remittance` product on the developer portal.
121
+ * `remittance_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
122
+ * `remittance_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.
123
+
124
+ You can create a remittance client with the following:
125
+
126
+ ```ruby
127
+ require 'momoapi-ruby'
128
+
129
+ remittance = Momoapi::Remittance.new
130
+ ```
131
+
132
+ ### Methods
133
+ 1. `transfer`: Used to transfer an amount from the owner’s account to a payee account. The status of the transaction can be validated by using the `get_transaction_status` method.
134
+
135
+ 2. `get_transaction_status`: Retrieve transaction information using the `transaction_reference` 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.
136
+
137
+ 3. `get_balance`: Get the balance of the account.
138
+
139
+
140
+ ## Sample Code
141
+
142
+ ```ruby
143
+ require 'momoapi-ruby'
144
+
145
+ collection = Momoapi::Collection.new
146
+
147
+ collection.is_user_active('256772123456')
148
+
149
+ collection.get_balance
150
+
151
+ transaction = collection.request_to_pay(
152
+ phone_number="256772123456", amount=600, external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
153
+
154
+ transaction_ref = transaction[:transaction_reference]
155
+
156
+ collection.get_transaction_status(transaction_ref)
157
+
158
+ ```
159
+
160
+ ### Points to note:
161
+ All methods for Disbursements and Remittances follow the same format as the examples shown above for Collections
162
+
163
+ The 'transfer' method for Disbursements and Remittances follows the same format as 'request_to_pay' above.
164
+
165
+ An extra argument, `callback_url`, can be passed to the 'request_to_pay' and 'transfer' methods, denoting the URL to the server where the callback should be sent.
166
+
167
+
168
+ ## Contributing
169
+
170
+ 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.
171
+
172
+ ## License
173
+
174
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
175
+
176
+ ## Code of Conduct
177
+
178
+ 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).
@@ -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
@@ -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__)
@@ -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)
@@ -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,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,58 @@
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
+ # Create an API user in the sandbox target environment
25
+ def create_sandbox_user
26
+ body = { "providerCallbackHost": @host }
27
+ @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser'
28
+ response = Faraday.post(@url) do |req|
29
+ req.headers['Content-Type'] = 'application/json'
30
+ req.headers['X-Reference-Id'] = @uuid
31
+ req.headers['Ocp-Apim-Subscription-Key'] = @key
32
+ req.body = body.to_json
33
+ end
34
+
35
+ unless response.status == 201
36
+ raise Momoapi::Error.new(response.body, response.status)
37
+ end
38
+
39
+ generate_api_key
40
+ end
41
+
42
+ # Generate an API key in the sandbox target environment
43
+ def generate_api_key
44
+ @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser/' +
45
+ @uuid + '/apikey'
46
+ response = Faraday.post(@url) do |req|
47
+ req.headers['Ocp-Apim-Subscription-Key'] = @key
48
+ end
49
+
50
+ unless response.status == 201
51
+ raise Momoapi::Error.new(response.body, response.status)
52
+ end
53
+
54
+ key = JSON.parse(response.body)
55
+ puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,107 @@
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
+ begin
16
+ auth_token = get_auth_token['access_token']
17
+ conn = Faraday.new(url: Momoapi.config.base_url)
18
+ conn.headers = headers
19
+ conn.authorization(:Bearer, auth_token)
20
+
21
+ case method
22
+ when 'get'
23
+ response = conn.get(path)
24
+ when 'post'
25
+ response = conn.post(path, body.to_json)
26
+ end
27
+ rescue ArgumentError
28
+ raise "Missing configuration key(s) for #{@product.capitalize}s"
29
+ end
30
+ interpret_response(response)
31
+ end
32
+
33
+ def interpret_response(resp)
34
+ body = resp.body.empty? ? '' : JSON.parse(resp.body)
35
+ response = {
36
+ body: body,
37
+ code: resp.status
38
+ }
39
+ unless resp.status >= 200 && resp.status < 300
40
+ handle_error(response[:body], response[:code])
41
+ end
42
+ body
43
+ end
44
+
45
+ def handle_error(response_body, response_code)
46
+ raise Momoapi::Error.new(response_body, response_code)
47
+ end
48
+
49
+ # Create an access token which can then be used to
50
+ # authorize and authenticate towards the other end-points of the API
51
+ def get_auth_token(path, subscription_key)
52
+ headers = {
53
+ "Ocp-Apim-Subscription-Key": subscription_key
54
+ }
55
+ url = Momoapi.config.base_url
56
+ conn = Faraday.new(url: url)
57
+ conn.headers = headers
58
+ @product = path.split('/')[0]
59
+ get_credentials(@product)
60
+ conn.basic_auth(@username, @password)
61
+ response = conn.post(path)
62
+ begin
63
+ JSON.parse(response.body)
64
+ rescue JSON::ParserError
65
+ response.body.to_s
66
+ end
67
+ end
68
+
69
+ def get_credentials(product)
70
+ case product
71
+ when 'collection'
72
+ @username = Momoapi.config.collection_user_id
73
+ @password = Momoapi.config.collection_api_secret
74
+ when 'disbursement'
75
+ @username = Momoapi.config.disbursement_user_id
76
+ @password = Momoapi.config.disbursement_api_secret
77
+ when 'remittance'
78
+ @username = Momoapi.config.remittance_user_id
79
+ @password = Momoapi.config.remittance_api_secret
80
+ end
81
+ end
82
+
83
+ def prepare_get_request(path, subscription_key)
84
+ headers = {
85
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
86
+ "Content-Type": 'application/json',
87
+ "Ocp-Apim-Subscription-Key": subscription_key
88
+ }
89
+ send_request('get', path, headers)
90
+ end
91
+
92
+ # get the balance on an account
93
+ def get_balance(path, subscription_key)
94
+ prepare_get_request(path, subscription_key)
95
+ end
96
+
97
+ # retrieve transaction information, for transfer and payments
98
+ def get_transaction_status(path, subscription_key)
99
+ prepare_get_request(path, subscription_key)
100
+ end
101
+
102
+ # check if an account holder is registered and active in the system
103
+ def is_user_active(path, subscription_key)
104
+ prepare_get_request(path, subscription_key)
105
+ end
106
+ end
107
+ 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
+ require 'momoapi-ruby/validate'
10
+
11
+ module Momoapi
12
+ class Collection < Client
13
+ def get_auth_token
14
+ path = 'collection/token/'
15
+ super(path, Momoapi.config.collection_primary_key)
16
+ end
17
+
18
+ def get_balance
19
+ path = '/collection/v1_0/account/balance'
20
+ super(path, Momoapi.config.collection_primary_key)
21
+ end
22
+
23
+ def get_transaction_status(transaction_id)
24
+ path = "/collection/v1_0/requesttopay/#{transaction_id}"
25
+ super(path, Momoapi.config.collection_primary_key)
26
+ end
27
+
28
+ # This method is used to request a payment from a consumer (Payer).
29
+ # The payer will be asked to authorize the payment. The transaction will
30
+ # be executed once the payer has authorized the payment.
31
+ # The requesttopay will be in status PENDING until the transaction
32
+ # is authorized or declined by the payer or it is timed out by the system.
33
+ # The status of the transaction can be validated
34
+ # by using `get_transation_status`
35
+ def request_to_pay(phone_number, amount, external_id,
36
+ payee_note = '', payer_message = '',
37
+ currency = 'EUR', callback_url = '')
38
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
39
+ uuid = SecureRandom.uuid
40
+ headers = {
41
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
42
+ "Content-Type": 'application/json',
43
+ "X-Reference-Id": uuid,
44
+ "Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
45
+ }
46
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
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,35 @@
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_writer :base_url
9
+ attr_accessor :environment,
10
+ :callback_host, :collection_primary_key,
11
+ :collection_user_id, :collection_api_secret,
12
+ :disbursement_primary_key, :disbursement_user_id,
13
+ :disbursement_api_secret, :remittance_primary_key,
14
+ :remittance_user_id, :remittance_api_secret
15
+
16
+ def initialize
17
+ @environment = nil
18
+ @base_url = nil
19
+ @callback_host = nil
20
+ @collection_primary_key = nil
21
+ @collection_user_id = nil
22
+ @collection_api_secret = nil
23
+ @disbursement_primary_key = nil
24
+ @disbursement_user_id = nil
25
+ @disbursement_api_secret = nil
26
+ @remittance_primary_key = nil
27
+ @remittance_user_id = nil
28
+ @remittance_api_secret = nil
29
+ end
30
+
31
+ def base_url
32
+ @base_url || 'https://ericssonbasicapi2.azure-api.net'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,62 @@
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', callback_url = '')
32
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
33
+ uuid = SecureRandom.uuid
34
+ headers = {
35
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
36
+ "Content-Type": 'application/json',
37
+ "X-Reference-Id": uuid,
38
+ "Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
39
+ }
40
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
41
+ body = {
42
+ "payee": {
43
+ "partyIdType": 'MSISDN',
44
+ "partyId": phone_number
45
+ },
46
+ "payeeNote": payee_note,
47
+ "payerMessage": payer_message,
48
+ "externalId": external_id,
49
+ "currency": currency,
50
+ "amount": amount.to_s
51
+ }
52
+ path = '/disbursement/v1_0/transfer'
53
+ send_request('post', path, headers, body)
54
+ { transaction_reference: uuid }
55
+ end
56
+
57
+ def is_user_active(phone_number)
58
+ path = "/disbursement/v1_0/accountholder/msisdn/#{phone_number}/active"
59
+ super(path, Momoapi.config.disbursement_primary_key)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Error handling for unsuccessful responses from the MTN Momo API
4
+
5
+ module Momoapi
6
+ class Error < StandardError
7
+ def initialize(message, code)
8
+ @code = code
9
+ super("Error code #{code} #{message}")
10
+ end
11
+ end
12
+
13
+ class ValidationError < StandardError
14
+ def initialize(msg = message)
15
+ super(msg)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,62 @@
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', callback_url = '')
32
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
33
+ uuid = SecureRandom.uuid
34
+ headers = {
35
+ "X-Target-Environment": Momoapi.config.environment || 'sandbox',
36
+ "Content-Type": 'application/json',
37
+ "X-Reference-Id": uuid,
38
+ "Ocp-Apim-Subscription-Key": Momoapi.config.remittance_primary_key
39
+ }
40
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
41
+ body = {
42
+ "payee": {
43
+ "partyIdType": 'MSISDN',
44
+ "partyId": phone_number
45
+ },
46
+ "payeeNote": payee_note,
47
+ "payerMessage": payer_message,
48
+ "externalId": external_id,
49
+ "currency": currency,
50
+ "amount": amount.to_s
51
+ }
52
+ path = '/remittance/v1_0/transfer'
53
+ send_request('post', path, headers, body)
54
+ { transaction_reference: uuid }
55
+ end
56
+
57
+ def is_user_active(phone_number)
58
+ path = "/remittance/v1_0/accountholder/msisdn/#{phone_number}/active"
59
+ super(path, Momoapi.config.remittance_primary_key)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Validations for parameters passed into client methods
4
+
5
+ require 'momoapi-ruby/errors'
6
+
7
+ module Momoapi
8
+ class Validate
9
+ def validate(phone_number, amount, currency)
10
+ validate_string?(phone_number, 'Phone number')
11
+ validate_numeric?(amount, 'Amount')
12
+ validate_string?(currency, 'Currency')
13
+ end
14
+
15
+ def validate_numeric?(num, field)
16
+ return true if num.is_a? Numeric
17
+
18
+ raise Momoapi::ValidationError, "#{field} should be a number"
19
+ end
20
+
21
+ def validate_string?(str, field)
22
+ return true if str.is_a? String
23
+
24
+ raise Momoapi::ValidationError, "#{field} should be a string"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Momoapi
4
+ VERSION = '1.0.1'
5
+ end
@@ -0,0 +1,36 @@
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
+ spec.required_ruby_version = '>= 2.3'
27
+
28
+ spec.add_dependency 'faraday', '~> 1.0'
29
+ spec.add_development_dependency 'bundler', '~> 2.0'
30
+ spec.add_development_dependency 'coveralls', '~> 0.8.15'
31
+ spec.add_development_dependency 'pry', '~> 0.12'
32
+ spec.add_development_dependency 'rake', '~> 13.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'vcr', '~> 5.1'
35
+ spec.add_development_dependency 'webmock', '~> 2.1'
36
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: momoapi-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lydia Sanyu Naggayi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.15
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.15
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: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.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/validate.rb
156
+ - lib/momoapi-ruby/version.rb
157
+ - momoapi-ruby.gemspec
158
+ homepage:
159
+ licenses:
160
+ - MIT
161
+ metadata: {}
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '2.3'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubygems_version: 3.0.6
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: MTN MoMo gem
181
+ test_files: []