Skip to content

Commit a2d760c

Browse files
authored
Merge pull request #12 from sparkplug/develop
Update client
2 parents 9c962b7 + ba89292 commit a2d760c

36 files changed

Lines changed: 2062 additions & 101 deletions

.coveralls.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
repo_token: bYVvifY6POXhFwOja3SZIX1nUxtWrUxKj

.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ Naming/FileName:
44
Enabled: false
55
Metrics/MethodLength:
66
Max: 25
7+
Metrics/BlockLength:
8+
Max: 35
79
Metrics/ParameterLists:
810
Max: 8
911
Lint/DuplicateMethods:
1012
Enabled: false
1113
Naming/AccessorMethodName:
14+
Enabled: false
15+
Naming/PredicateName:
16+
Enabled: false
17+
Style/AsciiComments:
1218
Enabled: false

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ cache: bundler
55
rvm:
66
- 2.5.3
77
before_install: gem install bundler -v 2.0.2
8+
git:
9+
depth: 25
10+
quiet: true

Gemfile.lock

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
addressable (2.7.0)
10+
public_suffix (>= 2.0.2, < 5.0)
911
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)
1020
diff-lcs (1.3)
21+
docile (1.3.2)
1122
faraday (1.0.0)
1223
multipart-post (>= 1.2, < 3)
24+
hashdiff (1.0.0)
25+
json (2.3.0)
1326
method_source (0.9.2)
1427
multipart-post (2.1.1)
1528
pry (0.12.2)
1629
coderay (~> 1.1.0)
1730
method_source (~> 0.9.0)
31+
public_suffix (4.0.3)
1832
rake (10.5.0)
1933
rspec (3.9.0)
2034
rspec-core (~> 3.9.0)
@@ -29,17 +43,37 @@ GEM
2943
diff-lcs (>= 1.2.0, < 2.0)
3044
rspec-support (~> 3.9.0)
3145
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
3263

3364
PLATFORMS
3465
ruby
3566

3667
DEPENDENCIES
3768
bundler (~> 2.0)
69+
coveralls (~> 0.8.15)
3870
faraday
3971
momoapi-ruby!
4072
pry (~> 0.12)
4173
rake (~> 10.0)
4274
rspec (~> 3.0)
75+
vcr (~> 5.1)
76+
webmock (~> 2.1)
4377

4478
BUNDLED WITH
4579
2.0.2

README.md

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# MTN MoMo API Ruby Gem
22

33
[![Build Status](https://travis-ci.com/sparkplug/momoapi-ruby.svg?branch=master)](https://travis-ci.com/sparkplug/momoapi-ruby)
4-
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/)
56

67
## Usage
78

@@ -26,7 +27,7 @@ Or install it yourself as:
2627
Type this command into your terminal:
2728

2829
```
29-
momoapi --host Your-Provider-Callback-Host --key Your-Ocp-Apim-Subscription-Key
30+
momoapi-ruby --host Your-Provider-Callback-Host --key Your-Ocp-Apim-Subscription-Key
3031
```
3132

3233
A User ID and API Key will be generated:
@@ -37,19 +38,136 @@ API key: Generated API key
3738
```
3839

3940
## Using live credentials
40-
4141
Add the following configurations in an initializer file (for example, `config/initializers/momoapi-ruby.rb` in a Rails app):
4242

4343
```
4444
Momoapi.configure do |config|
4545
config.base_url = 'Your MoMo account base URL'
4646
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+
```
4755
config.collection_primary_key = 'Your Collection Subscription Key'
4856
config.collection_user_id = 'Your Collection User ID'
4957
config.collection_api_secret = 'Your Collection API Key'
50-
end
5158
```
5259

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+
53171
## Contributing
54172

55173
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.

bin/setup

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ IFS=$'\n\t'
44
set -vx
55

66
bundle install
7-
8-
# Do any other automated setup that you need to do here

lib/momoapi-ruby.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'momoapi-ruby/version'
55
require 'momoapi-ruby/cli'
66
require 'momoapi-ruby/collection'
7+
require 'momoapi-ruby/disbursement'
8+
require 'momoapi-ruby/remittance'
79

810
module Momoapi
911
class << self

lib/momoapi-ruby/cli.rb

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# frozen_string_literal: true
22

3-
require 'momoapi-ruby'
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+
47
require 'faraday'
58
require 'json'
9+
require 'securerandom'
10+
11+
require 'momoapi-ruby/config'
12+
require 'momoapi-ruby/errors'
13+
require 'momoapi-ruby'
614

715
module Momoapi
816
class CLI
917
def initialize(option)
10-
@uuid = Faraday.get('https://www.uuidgenerator.net/api/version4').body.chomp
18+
@uuid = SecureRandom.uuid
1119
@host = option[:host]
1220
@key = option[:key]
1321
create_sandbox_user
@@ -22,27 +30,27 @@ def create_sandbox_user
2230
req.headers['Ocp-Apim-Subscription-Key'] = @key
2331
req.body = body.to_json
2432
end
25-
if response.status == 201
26-
generate_api_key
27-
else
28-
# TO DO: Add error handling here
29-
puts response.body
33+
34+
unless response.status == 201
35+
raise Error::APIError.new(response.body, response.status)
3036
end
37+
38+
generate_api_key
3139
end
3240

3341
def generate_api_key
3442
@url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' +
3543
@uuid + '/apikey'
36-
puts @url
3744
response = Faraday.post(@url) do |req|
3845
req.headers['Ocp-Apim-Subscription-Key'] = @key
3946
end
40-
if response.status == 201
41-
puts " User ID: #{@uuid} \n API key: #{response.body}"
42-
else
43-
# TO DO: Add error handling here
44-
puts 'Error creating API key'
47+
48+
unless response.status == 201
49+
raise Error::APIError.new(response.body, response.status)
4550
end
51+
52+
key = JSON.parse(response.body)
53+
puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n"
4654
end
4755
end
4856
end

0 commit comments

Comments
 (0)