Skip to content

Commit

Permalink
Merge pull request #12 from sparkplug/develop
Browse files Browse the repository at this point in the history
Update client
  • Loading branch information
NLSanyu authored Mar 2, 2020
2 parents 9c962b7 + ba89292 commit a2d760c
Show file tree
Hide file tree
Showing 36 changed files with 2,062 additions and 101 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: bYVvifY6POXhFwOja3SZIX1nUxtWrUxKj
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ Naming/FileName:
Enabled: false
Metrics/MethodLength:
Max: 25
Metrics/BlockLength:
Max: 35
Metrics/ParameterLists:
Max: 8
Lint/DuplicateMethods:
Enabled: false
Naming/AccessorMethodName:
Enabled: false
Naming/PredicateName:
Enabled: false
Style/AsciiComments:
Enabled: false
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ cache: bundler
rvm:
- 2.5.3
before_install: gem install bundler -v 2.0.2
git:
depth: 25
quiet: true
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
coderay (1.1.2)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
term-ansicolor (~> 1.3)
thor (>= 0.19.4, < 2.0)
tins (~> 1.6)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
docile (1.3.2)
faraday (1.0.0)
multipart-post (>= 1.2, < 3)
hashdiff (1.0.0)
json (2.3.0)
method_source (0.9.2)
multipart-post (2.1.1)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
rake (10.5.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
Expand All @@ -29,17 +43,37 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
safe_yaml (1.0.5)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
sync (0.5.0)
term-ansicolor (1.7.1)
tins (~> 1.0)
thor (1.0.1)
tins (1.24.1)
sync
vcr (5.1.0)
webmock (2.3.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
coveralls (~> 0.8.15)
faraday
momoapi-ruby!
pry (~> 0.12)
rake (~> 10.0)
rspec (~> 3.0)
vcr (~> 5.1)
webmock (~> 2.1)

BUNDLED WITH
2.0.2
126 changes: 122 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# MTN MoMo API Ruby Gem

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

[![Coverage Status](https://coveralls.io/repos/github/sparkplug/momoapi-ruby/badge.svg?branch=master)](https://coveralls.io/github/sparkplug/momoapi-ruby?branch=master)
[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/momo-api-developers/)

## Usage

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

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

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

## Using live credentials

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

```
Momoapi.configure do |config|
config.base_url = 'Your MoMo account base URL'
config.callback_host = 'Your Provider Callback Host'
end
```

## Collections
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.

Add the following to your configuration block:
```
config.collection_primary_key = 'Your Collection Subscription Key'
config.collection_user_id = 'Your Collection User ID'
config.collection_api_secret = 'Your Collection API Key'
end
```

* `collection_primary_key`: Primary Key for the `Collection` product on the developer portal.
* `collection_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
* `collection_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.

You can create a collection client with the following:

```ruby
require 'momoapi-ruby'

collection = Momoapi::Collection.new
```

### Methods
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`.

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.

3. `get_balance`: Get the balance of the account.

### Sample Code

```ruby
require 'momoapi-ruby'

collection = Momoapi::Collection.new
collection.request_to_pay(
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
```
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.

## Disbursements
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.

Add the following to your configuration block:
```
config.disbursement_primary_key = 'Your Disbursement Subscription Key'
config.disbursement_user_id = 'Your Disbursement User ID'
config.disbursement_api_secret = 'Your Disbursement API Key'
```

* `disbursement_primary_key`: Primary Key for the `Disbursement` product on the developer portal.
* `disbursement_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
* `disbursement_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.

You can create a disbursement client with the following:

```ruby
require 'momoapi-ruby'

disbursement = Momoapi::Disbursement.new
```

### Methods
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.

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.

3. `get_balance`: Get the balance of the account.

### Sample Code

```ruby
require 'momoapi-ruby'

disbursement = Momoapi::Disbursement.new
disbursement.transfer(
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
```
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.

## Remittances
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.

Add the following to your configuration block:
```
config.remittance_primary_key = 'Your Remittance Subscription Key'
config.remittance_user_id = 'Your Remittance User ID'
config.remittance_api_secret = 'Your Remittance API Key'
```

* `remittance_primary_key`: Primary Key for the `Remittance` product on the developer portal.
* `remittance_user_id`: For sandbox, use the one generated with the `momoapi-ruby` command.
* `remittance_api_secret`: For sandbox, use the one generated with the `momoapi-ruby` command.

You can create a remittance client with the following:

```ruby
require 'momoapi-ruby'

remittance = Momoapi::Remittance.new
```

### Methods
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.

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.

3. `get_balance`: Get the balance of the account.

### Sample Code

```ruby
require 'momoapi-ruby'

remittance = Momoapi::Remittance.new
remittance.transfer(
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
```
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.


## Contributing

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.
Expand Down
2 changes: 0 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
2 changes: 2 additions & 0 deletions lib/momoapi-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'momoapi-ruby/version'
require 'momoapi-ruby/cli'
require 'momoapi-ruby/collection'
require 'momoapi-ruby/disbursement'
require 'momoapi-ruby/remittance'

module Momoapi
class << self
Expand Down
34 changes: 21 additions & 13 deletions lib/momoapi-ruby/cli.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# frozen_string_literal: true

require 'momoapi-ruby'
# This is an executable file allowing a user to use the command line interface
# to pass in a callback host url and a subscription key
# and get back a generated user id and API key

require 'faraday'
require 'json'
require 'securerandom'

require 'momoapi-ruby/config'
require 'momoapi-ruby/errors'
require 'momoapi-ruby'

module Momoapi
class CLI
def initialize(option)
@uuid = Faraday.get('https://www.uuidgenerator.net/api/version4').body.chomp
@uuid = SecureRandom.uuid
@host = option[:host]
@key = option[:key]
create_sandbox_user
Expand All @@ -22,27 +30,27 @@ def create_sandbox_user
req.headers['Ocp-Apim-Subscription-Key'] = @key
req.body = body.to_json
end
if response.status == 201
generate_api_key
else
# TO DO: Add error handling here
puts response.body

unless response.status == 201
raise Error::APIError.new(response.body, response.status)
end

generate_api_key
end

def generate_api_key
@url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' +
@uuid + '/apikey'
puts @url
response = Faraday.post(@url) do |req|
req.headers['Ocp-Apim-Subscription-Key'] = @key
end
if response.status == 201
puts " User ID: #{@uuid} \n API key: #{response.body}"
else
# TO DO: Add error handling here
puts 'Error creating API key'

unless response.status == 201
raise Error::APIError.new(response.body, response.status)
end

key = JSON.parse(response.body)
puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n"
end
end
end
Loading

0 comments on commit a2d760c

Please sign in to comment.