Skip to content

Commit

Permalink
Merge pull request #17 from sparkplug/develop
Browse files Browse the repository at this point in the history
Update version to 1.0.0
  • Loading branch information
NLSanyu authored Mar 6, 2020
2 parents eca3999 + 5e8b120 commit dad91db
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
momoapi-ruby (0.1.0)
momoapi-ruby (1.0.0)

GEM
remote: https://rubygems.org/
Expand Down
59 changes: 28 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,12 @@ 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`.
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`.

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

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.
Expand All @@ -110,22 +100,12 @@ 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.
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.

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

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.
Expand All @@ -150,22 +130,39 @@ 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.
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.

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

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

### Sample Code

## 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")
collection = Momoapi::Collection.new

collection.is_user_active('256772123456')

collection.get_balance

transaction = collection.request_to_pay(
phone_number="256772123456", amount=600, external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")

transaction_ref = transaction[:transaction_reference]

collection.get_transaction_status(transaction_ref)

```
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.

### Points to note:
All methods for Disbursements and Remittances follow the same format as the examples shown above for Collections

The 'transfer' method for Disbursements and Remittances follows the same format as 'request_to_pay' above.

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.


## Contributing
Expand Down
10 changes: 6 additions & 4 deletions lib/momoapi-ruby/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def initialize(option)
create_sandbox_user
end

# Create an API user in the sandbox target environment
def create_sandbox_user
body = { "providerCallbackHost": @host }
@url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser'
@url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser'
response = Faraday.post(@url) do |req|
req.headers['Content-Type'] = 'application/json'
req.headers['X-Reference-Id'] = @uuid
Expand All @@ -32,21 +33,22 @@ def create_sandbox_user
end

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

generate_api_key
end

# Generate an API key in the sandbox target environment
def generate_api_key
@url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' +
@url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser/' +
@uuid + '/apikey'
response = Faraday.post(@url) do |req|
req.headers['Ocp-Apim-Subscription-Key'] = @key
end

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

key = JSON.parse(response.body)
Expand Down
30 changes: 17 additions & 13 deletions lib/momoapi-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
module Momoapi
class Client
def send_request(method, path, headers, body = {})
auth_token = get_auth_token['access_token']
conn = Faraday.new(url: Momoapi.config.base_url)
conn.headers = headers
conn.authorization(:Bearer, auth_token)
begin
auth_token = get_auth_token['access_token']
conn = Faraday.new(url: Momoapi.config.base_url)
conn.headers = headers
conn.authorization(:Bearer, auth_token)

case method
when 'get'
response = conn.get(path)
when 'post'
response = conn.post(path, body.to_json)
case method
when 'get'
response = conn.get(path)
when 'post'
response = conn.post(path, body.to_json)
end
rescue ArgumentError
raise "Missing configuration key(s) for #{@product.capitalize}s"
end
interpret_response(response)
end
Expand All @@ -35,11 +39,11 @@ def interpret_response(resp)
unless resp.status >= 200 && resp.status < 300
handle_error(response[:body], response[:code])
end
response
body
end

def handle_error(response_body, response_code)
raise Error::APIError.new(response_body, response_code)
raise Momoapi::Error.new(response_body, response_code)
end

# Create an access token which can then be used to
Expand All @@ -51,8 +55,8 @@ def get_auth_token(path, subscription_key)
url = Momoapi.config.base_url
conn = Faraday.new(url: url)
conn.headers = headers
product = path.split('/')[0]
get_credentials(product)
@product = path.split('/')[0]
get_credentials(@product)
conn.basic_auth(@username, @password)
response = conn.post(path)
begin
Expand Down
2 changes: 2 additions & 0 deletions lib/momoapi-ruby/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require 'momoapi-ruby/config'
require 'momoapi-ruby/client'
require 'momoapi-ruby/validate'

module Momoapi
class Collection < Client
Expand Down Expand Up @@ -34,6 +35,7 @@ def get_transaction_status(transaction_id)
def request_to_pay(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', callback_url = '')
Momoapi::Validate.new.validate(phone_number, amount, currency)
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
Expand Down
1 change: 1 addition & 0 deletions lib/momoapi-ruby/disbursement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
def transfer(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', callback_url = '')
Momoapi::Validate.new.validate(phone_number, amount, currency)
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
Expand Down
10 changes: 8 additions & 2 deletions lib/momoapi-ruby/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

# Error handling for unsuccessful responses from the MTN Momo API

module Error
class APIError < StandardError
module Momoapi
class Error < StandardError
def initialize(message, code)
@code = code
super("Error code #{code} #{message}")
end
end

class ValidationError < StandardError
def initialize(msg = message)
super(msg)
end
end
end
1 change: 1 addition & 0 deletions lib/momoapi-ruby/remittance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
def transfer(phone_number, amount, external_id,
payee_note = '', payer_message = '',
currency = 'EUR', callback_url = '')
Momoapi::Validate.new.validate(phone_number, amount, currency)
uuid = SecureRandom.uuid
headers = {
"X-Target-Environment": Momoapi.config.environment || 'sandbox',
Expand Down
27 changes: 27 additions & 0 deletions lib/momoapi-ruby/validate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# Validations for parameters passed into client methods

require 'momoapi-ruby/errors'

module Momoapi
class Validate
def validate(phone_number, amount, currency)
validate_string?(phone_number, 'Phone number')
validate_numeric?(amount, 'Amount')
validate_string?(currency, 'Currency')
end

def validate_numeric?(num, field)
return true if num.is_a? Numeric

raise Momoapi::ValidationError, "#{field} should be a number"
end

def validate_string?(str, field)
return true if str.is_a? String

raise Momoapi::ValidationError, "#{field} should be a string"
end
end
end
2 changes: 1 addition & 1 deletion lib/momoapi-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Momoapi
VERSION = '0.1.0'
VERSION = '1.0.0'
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dad91db

Please sign in to comment.