Skip to content

Commit ba89292

Browse files
authored
Merge pull request #10 from sparkplug/extend-functionality
Extend functionality
2 parents 54e46c0 + e176cfe commit ba89292

19 files changed

Lines changed: 550 additions & 32 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ Naming/FileName:
55
Metrics/MethodLength:
66
Max: 25
77
Metrics/BlockLength:
8-
Max: 30
8+
Max: 35
99
Metrics/ParameterLists:
1010
Max: 8
1111
Lint/DuplicateMethods:
1212
Enabled: false
1313
Naming/AccessorMethodName:
14+
Enabled: false
15+
Naming/PredicateName:
16+
Enabled: false
17+
Style/AsciiComments:
1418
Enabled: false

Gemfile.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@ GEM
99
addressable (2.7.0)
1010
public_suffix (>= 2.0.2, < 5.0)
1111
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)
1218
crack (0.4.3)
1319
safe_yaml (~> 1.0.0)
1420
diff-lcs (1.3)
21+
docile (1.3.2)
1522
faraday (1.0.0)
1623
multipart-post (>= 1.2, < 3)
1724
hashdiff (1.0.0)
25+
json (2.3.0)
1826
method_source (0.9.2)
1927
multipart-post (2.1.1)
2028
pry (0.12.2)
@@ -36,6 +44,17 @@ GEM
3644
rspec-support (~> 3.9.0)
3745
rspec-support (3.9.2)
3846
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
3958
vcr (5.1.0)
4059
webmock (2.3.2)
4160
addressable (>= 2.3.6)
@@ -47,6 +66,7 @@ PLATFORMS
4766

4867
DEPENDENCIES
4968
bundler (~> 2.0)
69+
coveralls (~> 0.8.15)
5070
faraday
5171
momoapi-ruby!
5272
pry (~> 0.12)

README.md

Lines changed: 93 additions & 6 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

@@ -43,14 +44,18 @@ Add the following configurations in an initializer file (for example, `config/in
4344
Momoapi.configure do |config|
4445
config.base_url = 'Your MoMo account base URL'
4546
config.callback_host = 'Your Provider Callback Host'
46-
config.collection_primary_key = 'Your Collection Subscription Key'
47-
config.collection_user_id = 'Your Collection User ID'
48-
config.collection_api_secret = 'Your Collection API Key'
4947
end
5048
```
5149

5250
## Collections
53-
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;
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+
```
5459

5560
* `collection_primary_key`: Primary Key for the `Collection` product on the developer portal.
5661
* `collection_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
@@ -77,9 +82,91 @@ collection = Momoapi::Collection.new
7782
require 'momoapi-ruby'
7883

7984
collection = Momoapi::Collection.new
80-
collection.requestToPay(
85+
collection.request_to_pay(
8186
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
8287
```
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+
83170

84171
## Contributing
85172

lib/momoapi-ruby/cli.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# frozen_string_literal: true
22

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+
37
require 'faraday'
48
require 'json'
59
require 'securerandom'

lib/momoapi-ruby/client.rb

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

3+
# Base implementation of the MTN API client
4+
5+
# Includes methods common to collections, disbursements and remittances
6+
37
require 'faraday'
48

59
require 'momoapi-ruby/config'
@@ -23,8 +27,9 @@ def send_request(method, path, headers, *_body)
2327
end
2428

2529
def interpret_response(resp)
30+
body = resp.body.empty? ? '' : JSON.parse(resp.body)
2631
response = {
27-
body: resp.body,
32+
body: body,
2833
code: resp.status
2934
}
3035
unless resp.status >= 200 && resp.status < 300
@@ -37,6 +42,8 @@ def handle_error(response_body, response_code)
3742
raise Error::APIError.new(response_body, response_code)
3843
end
3944

45+
# Create an access token which can then be used to
46+
# authorize and authenticate towards the other end-points of the API
4047
def get_auth_token(path, subscription_key)
4148
headers = {
4249
"Ocp-Apim-Subscription-Key": subscription_key
@@ -55,7 +62,7 @@ def get_auth_token(path, subscription_key)
5562
end
5663
end
5764

58-
def get_balance(path, subscription_key)
65+
def prepare_get_request(path, subscription_key)
5966
headers = {
6067
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
6168
"Content-Type": 'application/json',
@@ -64,13 +71,19 @@ def get_balance(path, subscription_key)
6471
send_request('get', path, headers)
6572
end
6673

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
6780
def get_transaction_status(path, subscription_key)
68-
headers = {
69-
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
70-
"Content-Type": 'application/json',
71-
"Ocp-Apim-Subscription-Key": subscription_key
72-
}
73-
send_request('get', path, headers)
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)
7487
end
7588
end
7689
end

lib/momoapi-ruby/collection.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
# Implementation of the MTN API collections client
4+
35
require 'securerandom'
46

57
require 'momoapi-ruby/config'
@@ -22,15 +24,26 @@ def get_transaction_status(transaction_id)
2224
super(path, Momoapi.config.collection_primary_key)
2325
end
2426

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`
2534
def request_to_pay(phone_number, amount, external_id,
26-
payee_note = '', payer_message = '', currency = 'EUR')
35+
payee_note = '', payer_message = '',
36+
currency = 'EUR', **options)
2737
uuid = SecureRandom.uuid
2838
headers = {
2939
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
3040
"Content-Type": 'application/json',
3141
"X-Reference-Id": uuid,
3242
"Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
3343
}
44+
if options[:callback_url]
45+
headers['X-Callback-Url'] = options[:callback_url]
46+
end
3447
body = {
3548
"payer": {
3649
"partyIdType": 'MSISDN',
@@ -46,5 +59,10 @@ def request_to_pay(phone_number, amount, external_id,
4659
send_request('post', path, headers, body)
4760
{ transaction_reference: uuid }
4861
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
4967
end
5068
end

lib/momoapi-ruby/config.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
# Configurations are set up in this file
4+
# for a user's MTN MoMo API user credentials
5+
36
module Momoapi
47
class Config
58
attr_accessor :environment, :base_url,

lib/momoapi-ruby/disbursement.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
# Implementation of the MTN API disbursements client
4+
35
require 'momoapi-ruby/config'
46
require 'momoapi-ruby/client'
57

@@ -20,15 +22,23 @@ def get_transaction_status(transaction_id)
2022
super(path, Momoapi.config.disbursement_primary_key)
2123
end
2224

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`
2329
def transfer(phone_number, amount, external_id,
24-
payee_note = '', payer_message = '', currency = 'EUR')
30+
payee_note = '', payer_message = '',
31+
currency = 'EUR', **options)
2532
uuid = SecureRandom.uuid
2633
headers = {
2734
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
2835
"Content-Type": 'application/json',
2936
"X-Reference-Id": uuid,
3037
"Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
3138
}
39+
if options[:callback_url]
40+
headers['X-Callback-Url'] = options[:callback_url]
41+
end
3242
body = {
3343
"payer": {
3444
"partyIdType": 'MSISDN',
@@ -44,5 +54,10 @@ def transfer(phone_number, amount, external_id,
4454
send_request('post', path, headers, body)
4555
{ transaction_reference: uuid }
4656
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
4762
end
4863
end

lib/momoapi-ruby/errors.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
# Error handling for unsuccessful responses from the MTN Momo API
4+
35
module Error
46
class APIError < StandardError
57
def initialize(message, code)

0 commit comments

Comments
 (0)