momoapi-ruby 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f12ed418233f72f5340d44b9eacb05a0f9a93329806f081f3ac8a0cf6ffd312
4
- data.tar.gz: 791db9e61c6d73c833ff28025cb83b9e9e5f51583569bfea6b4b6518247f6de4
3
+ metadata.gz: 59f987b2d792f29ab687833b30ff702544e30ed0db2eb39810240f4edf615ad0
4
+ data.tar.gz: b49d2a488248de144535c6289f516b2d3f347b4a69b41fa05700a73e99560f8c
5
5
  SHA512:
6
- metadata.gz: 6c4e5b1e00631015144d31a157927732acc13c284ff4c177f31a1452d0afd55b4107ceaf426cd56f33c4b1abc09040d8a5bf9f245502bdfcd07399ea79f4d848
7
- data.tar.gz: 3869ce98f646bdd3ffdbf0ee64f898ccf5422554f93cb2e92c5032758eb1fea1484094ac545b0ab3c6e32e4010d7e42d846e34313ac9a908984bc97df8c4af80
6
+ metadata.gz: 598c194f973eb9cbcc31689c04239b5118e4d6be2b0ad954661d1b86dc935113cccc50c04bbc828bcae07f387ffc1dab4117b095398568d547d90f43e887541f
7
+ data.tar.gz: 1d9877820849c3c387abdd7e24d72096a45668e2f2ccd59e6919ba4d1a110c1213754df91e07c59b9b105b1ec464d24b686ca7866cffdeb44607863fe9d7b2b5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- momoapi-ruby (0.1.0)
4
+ momoapi-ruby (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -70,22 +70,12 @@ collection = Momoapi::Collection.new
70
70
  ```
71
71
 
72
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`.
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`.
74
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.
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.
76
76
 
77
77
  3. `get_balance`: Get the balance of the account.
78
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
79
 
90
80
  ## Disbursements
91
81
  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
110
100
  ```
111
101
 
112
102
  ### 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.
114
104
 
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.
116
106
 
117
107
  3. `get_balance`: Get the balance of the account.
118
108
 
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
109
 
130
110
  ## Remittances
131
111
  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
150
130
  ```
151
131
 
152
132
  ### 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.
154
134
 
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.
156
136
 
157
137
  3. `get_balance`: Get the balance of the account.
158
138
 
159
- ### Sample Code
139
+
140
+ ## Sample Code
160
141
 
161
142
  ```ruby
162
143
  require 'momoapi-ruby'
163
144
 
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
+
167
158
  ```
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.
169
166
 
170
167
 
171
168
  ## Contributing
@@ -21,9 +21,10 @@ module Momoapi
21
21
  create_sandbox_user
22
22
  end
23
23
 
24
+ # Create an API user in the sandbox target environment
24
25
  def create_sandbox_user
25
26
  body = { "providerCallbackHost": @host }
26
- @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser'
27
+ @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser'
27
28
  response = Faraday.post(@url) do |req|
28
29
  req.headers['Content-Type'] = 'application/json'
29
30
  req.headers['X-Reference-Id'] = @uuid
@@ -32,21 +33,22 @@ module Momoapi
32
33
  end
33
34
 
34
35
  unless response.status == 201
35
- raise Error::APIError.new(response.body, response.status)
36
+ raise Momoapi::Error.new(response.body, response.status)
36
37
  end
37
38
 
38
39
  generate_api_key
39
40
  end
40
41
 
42
+ # Generate an API key in the sandbox target environment
41
43
  def generate_api_key
42
- @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' +
44
+ @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser/' +
43
45
  @uuid + '/apikey'
44
46
  response = Faraday.post(@url) do |req|
45
47
  req.headers['Ocp-Apim-Subscription-Key'] = @key
46
48
  end
47
49
 
48
50
  unless response.status == 201
49
- raise Error::APIError.new(response.body, response.status)
51
+ raise Momoapi::Error.new(response.body, response.status)
50
52
  end
51
53
 
52
54
  key = JSON.parse(response.body)
@@ -11,17 +11,21 @@ require 'momoapi-ruby/errors'
11
11
 
12
12
  module Momoapi
13
13
  class Client
14
- 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)
14
+ def send_request(method, path, headers, body = {})
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)
19
20
 
20
- case method
21
- when 'get'
22
- response = conn.get(path)
23
- when 'post'
24
- response = conn.post(path)
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"
25
29
  end
26
30
  interpret_response(response)
27
31
  end
@@ -35,11 +39,11 @@ module Momoapi
35
39
  unless resp.status >= 200 && resp.status < 300
36
40
  handle_error(response[:body], response[:code])
37
41
  end
38
- response
42
+ body
39
43
  end
40
44
 
41
45
  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)
43
47
  end
44
48
 
45
49
  # Create an access token which can then be used to
@@ -48,12 +52,12 @@ module Momoapi
48
52
  headers = {
49
53
  "Ocp-Apim-Subscription-Key": subscription_key
50
54
  }
51
- username = Momoapi.config.collection_user_id
52
- password = Momoapi.config.collection_api_secret
53
55
  url = Momoapi.config.base_url
54
56
  conn = Faraday.new(url: url)
55
57
  conn.headers = headers
56
- conn.basic_auth(username, password)
58
+ @product = path.split('/')[0]
59
+ get_credentials(@product)
60
+ conn.basic_auth(@username, @password)
57
61
  response = conn.post(path)
58
62
  begin
59
63
  JSON.parse(response.body)
@@ -62,6 +66,20 @@ module Momoapi
62
66
  end
63
67
  end
64
68
 
69
+ def get_credentials(product)
70
+ case product
71
+ when 'collection'
72
+ @username = Momoapi.config.collection_user_id
73
+ @password = Momoapi.config.collection_api_secret
74
+ when 'disbursement'
75
+ @username = Momoapi.config.disbursement_user_id
76
+ @password = Momoapi.config.disbursement_api_secret
77
+ when 'remittance'
78
+ @username = Momoapi.config.remittance_user_id
79
+ @password = Momoapi.config.remittance_api_secret
80
+ end
81
+ end
82
+
65
83
  def prepare_get_request(path, subscription_key)
66
84
  headers = {
67
85
  "X-Target-Environment": Momoapi.config.environment || 'sandbox',
@@ -6,6 +6,7 @@ require 'securerandom'
6
6
 
7
7
  require 'momoapi-ruby/config'
8
8
  require 'momoapi-ruby/client'
9
+ require 'momoapi-ruby/validate'
9
10
 
10
11
  module Momoapi
11
12
  class Collection < Client
@@ -33,7 +34,8 @@ module Momoapi
33
34
  # by using `get_transation_status`
34
35
  def request_to_pay(phone_number, amount, external_id,
35
36
  payee_note = '', payer_message = '',
36
- currency = 'EUR', **options)
37
+ currency = 'EUR', callback_url = '')
38
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
37
39
  uuid = SecureRandom.uuid
38
40
  headers = {
39
41
  "X-Target-Environment": Momoapi.config.environment || 'sandbox',
@@ -41,9 +43,7 @@ module Momoapi
41
43
  "X-Reference-Id": uuid,
42
44
  "Ocp-Apim-Subscription-Key": Momoapi.config.collection_primary_key
43
45
  }
44
- if options[:callback_url]
45
- headers['X-Callback-Url'] = options[:callback_url]
46
- end
46
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
47
47
  body = {
48
48
  "payer": {
49
49
  "partyIdType": 'MSISDN',
@@ -5,7 +5,8 @@
5
5
 
6
6
  module Momoapi
7
7
  class Config
8
- attr_accessor :environment, :base_url,
8
+ attr_writer :base_url
9
+ attr_accessor :environment,
9
10
  :callback_host, :collection_primary_key,
10
11
  :collection_user_id, :collection_api_secret,
11
12
  :disbursement_primary_key, :disbursement_user_id,
@@ -26,5 +27,9 @@ module Momoapi
26
27
  @remittance_user_id = nil
27
28
  @remittance_api_secret = nil
28
29
  end
30
+
31
+ def base_url
32
+ @base_url || 'https://ericssonbasicapi2.azure-api.net'
33
+ end
29
34
  end
30
35
  end
@@ -28,7 +28,8 @@ module Momoapi
28
28
  # by using `get_transation_status`
29
29
  def transfer(phone_number, amount, external_id,
30
30
  payee_note = '', payer_message = '',
31
- currency = 'EUR', **options)
31
+ currency = 'EUR', callback_url = '')
32
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
32
33
  uuid = SecureRandom.uuid
33
34
  headers = {
34
35
  "X-Target-Environment": Momoapi.config.environment || 'sandbox',
@@ -36,11 +37,9 @@ module Momoapi
36
37
  "X-Reference-Id": uuid,
37
38
  "Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
38
39
  }
39
- if options[:callback_url]
40
- headers['X-Callback-Url'] = options[:callback_url]
41
- end
40
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
42
41
  body = {
43
- "payer": {
42
+ "payee": {
44
43
  "partyIdType": 'MSISDN',
45
44
  "partyId": phone_number
46
45
  },
@@ -2,11 +2,17 @@
2
2
 
3
3
  # Error handling for unsuccessful responses from the MTN Momo API
4
4
 
5
- module Error
6
- class APIError < StandardError
5
+ module Momoapi
6
+ class Error < StandardError
7
7
  def initialize(message, code)
8
8
  @code = code
9
- super("Error - code #{code}, message: #{message}")
9
+ super("Error code #{code} #{message}")
10
+ end
11
+ end
12
+
13
+ class ValidationError < StandardError
14
+ def initialize(msg = message)
15
+ super(msg)
10
16
  end
11
17
  end
12
18
  end
@@ -28,19 +28,18 @@ module Momoapi
28
28
  # by using `get_transation_status`
29
29
  def transfer(phone_number, amount, external_id,
30
30
  payee_note = '', payer_message = '',
31
- currency = 'EUR', **options)
31
+ currency = 'EUR', callback_url = '')
32
+ Momoapi::Validate.new.validate(phone_number, amount, currency)
32
33
  uuid = SecureRandom.uuid
33
34
  headers = {
34
35
  "X-Target-Environment": Momoapi.config.environment || 'sandbox',
35
36
  "Content-Type": 'application/json',
36
37
  "X-Reference-Id": uuid,
37
- "Ocp-Apim-Subscription-Key": Momoapi.config.disbursement_primary_key
38
+ "Ocp-Apim-Subscription-Key": Momoapi.config.remittance_primary_key
38
39
  }
39
- if options[:callback_url]
40
- headers['X-Callback-Url'] = options[:callback_url]
41
- end
40
+ headers['X-Callback-Url'] = callback_url unless callback_url.empty?
42
41
  body = {
43
- "payer": {
42
+ "payee": {
44
43
  "partyIdType": 'MSISDN',
45
44
  "partyId": phone_number
46
45
  },
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Momoapi
4
- VERSION = '0.1.1'
4
+ VERSION = '1.0.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momoapi-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lydia Sanyu Naggayi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-04 00:00:00.000000000 Z
11
+ date: 2020-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,6 +152,7 @@ files:
152
152
  - lib/momoapi-ruby/disbursement.rb
153
153
  - lib/momoapi-ruby/errors.rb
154
154
  - lib/momoapi-ruby/remittance.rb
155
+ - lib/momoapi-ruby/validate.rb
155
156
  - lib/momoapi-ruby/version.rb
156
157
  - momoapi-ruby.gemspec
157
158
  homepage: