Skip to content

Commit dad91db

Browse files
authored
Merge pull request #17 from sparkplug/develop
Update version to 1.0.0
2 parents eca3999 + 5e8b120 commit dad91db

File tree

17 files changed

+403
-67
lines changed

17 files changed

+403
-67
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
momoapi-ruby (0.1.0)
4+
momoapi-ruby (1.0.0)
55

66
GEM
77
remote: https://rubygems.org/

README.md

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,12 @@ collection = Momoapi::Collection.new
7070
```
7171

7272
### 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`.
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`.
7474

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

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

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

9080
## Disbursements
9181
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.
@@ -110,22 +100,12 @@ disbursement = Momoapi::Disbursement.new
110100
```
111101

112102
### 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.
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.
114104

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

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

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

130110
## Remittances
131111
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.
@@ -150,22 +130,39 @@ remittance = Momoapi::Remittance.new
150130
```
151131

152132
### 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.
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.
154134

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

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

159-
### Sample Code
139+
140+
## Sample Code
160141

161142
```ruby
162143
require 'momoapi-ruby'
163144

164-
remittance = Momoapi::Remittance.new
165-
remittance.transfer(
166-
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
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+
167158
```
168-
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
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.
169166

170167

171168
## Contributing

lib/momoapi-ruby/cli.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def initialize(option)
2121
create_sandbox_user
2222
end
2323

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

3435
unless response.status == 201
35-
raise Error::APIError.new(response.body, response.status)
36+
raise Momoapi::Error.new(response.body, response.status)
3637
end
3738

3839
generate_api_key
3940
end
4041

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

4850
unless response.status == 201
49-
raise Error::APIError.new(response.body, response.status)
51+
raise Momoapi::Error.new(response.body, response.status)
5052
end
5153

5254
key = JSON.parse(response.body)

lib/momoapi-ruby/client.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
module Momoapi
1313
class Client
1414
def send_request(method, path, headers, body = {})
15-
auth_token = get_auth_token['access_token']
16-
conn = Faraday.new(url: Momoapi.config.base_url)
17-
conn.headers = headers
18-
conn.authorization(:Bearer, auth_token)
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)
1920

20-
case method
21-
when 'get'
22-
response = conn.get(path)
23-
when 'post'
24-
response = conn.post(path, body.to_json)
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"
2529
end
2630
interpret_response(response)
2731
end
@@ -35,11 +39,11 @@ def interpret_response(resp)
3539
unless resp.status >= 200 && resp.status < 300
3640
handle_error(response[:body], response[:code])
3741
end
38-
response
42+
body
3943
end
4044

4145
def handle_error(response_body, response_code)
42-
raise Error::APIError.new(response_body, response_code)
46+
raise Momoapi::Error.new(response_body, response_code)
4347
end
4448

4549
# Create an access token which can then be used to
@@ -51,8 +55,8 @@ def get_auth_token(path, subscription_key)
5155
url = Momoapi.config.base_url
5256
conn = Faraday.new(url: url)
5357
conn.headers = headers
54-
product = path.split('/')[0]
55-
get_credentials(product)
58+
@product = path.split('/')[0]
59+
get_credentials(@product)
5660
conn.basic_auth(@username, @password)
5761
response = conn.post(path)
5862
begin

lib/momoapi-ruby/collection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
require 'momoapi-ruby/config'
88
require 'momoapi-ruby/client'
9+
require 'momoapi-ruby/validate'
910

1011
module Momoapi
1112
class Collection < Client
@@ -34,6 +35,7 @@ def get_transaction_status(transaction_id)
3435
def request_to_pay(phone_number, amount, external_id,
3536
payee_note = '', payer_message = '',
3637
currency = 'EUR', callback_url = '')
38+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3739
uuid = SecureRandom.uuid
3840
headers = {
3941
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/disbursement.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
3131
currency = 'EUR', callback_url = '')
32+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3233
uuid = SecureRandom.uuid
3334
headers = {
3435
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/errors.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
# Error handling for unsuccessful responses from the MTN Momo API
44

5-
module Error
6-
class APIError < StandardError
5+
module Momoapi
6+
class Error < StandardError
77
def initialize(message, code)
88
@code = code
99
super("Error code #{code} #{message}")
1010
end
1111
end
12+
13+
class ValidationError < StandardError
14+
def initialize(msg = message)
15+
super(msg)
16+
end
17+
end
1218
end

lib/momoapi-ruby/remittance.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
3131
currency = 'EUR', callback_url = '')
32+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3233
uuid = SecureRandom.uuid
3334
headers = {
3435
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/validate.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/momoapi-ruby/version.rb

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

33
module Momoapi
4-
VERSION = '0.1.0'
4+
VERSION = '1.0.0'
55
end

0 commit comments

Comments
 (0)