paymentsds-mpesa 0.1.0.pre.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eec94343c54d33bb54e2492a22a89d538231c674ba4f4ea3aac71e133cf3cb4b
4
+ data.tar.gz: 33813434c53a1270dd0699ed3c824f8c90cba92b962140a1ad6244b8af04dc35
5
+ SHA512:
6
+ metadata.gz: 778a5b13075fcc4aeb92a3883000cf5dc1a9281d5b1a1cc66770cc27ad3b2c3813a8bc860087dd155b09c7e1987e7118c409ab8cfa6bd8db1ddc448bfa2d4598
7
+ data.tar.gz: 24a25d403a4228c16065c09b1d18c6ded65ea02ee070d1ad79d277bb0de0d3d5206e0be88c51b6490727ff3a33462ff2f9740c932981c432b4a3e4519eec2a96
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /build.shh
10
+ /Gemfile.lock
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
File without changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
File without changes
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paysuite-mpesa.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2020 Edson Michaque, Nélio Macombo
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,261 @@
1
+ # M-Pesa SDK for Ruby
2
+
3
+ M-Pesa SDK for Ruby is an unofficial library aiming to help businesses integrating every [M-Pesa](https://developer.mpesa.vm.co.mz) operations to their Ruby applications.
4
+
5
+ ## Contents
6
+
7
+ - [Features](#features)
8
+ - [Usage](#usage)
9
+ - [Quickstart](#usage/scenario-1)
10
+ - [Receive Money from a Mobile Account](#usage/scenario-1)
11
+ - [Send Money to a Mobile Account](#usage/scenario-2)
12
+ - [Send Money to a Business Account](#usage/scenario-3)
13
+ - [Revert a Transaction](#usage/scenario-4)
14
+ - [Query the Status of a Transaction](#usage/scenario-5)
15
+ - [Examples](#usage/scenario-6)
16
+ - [Prerequisites](#prerequisites)
17
+ - [Installation](#installation)
18
+ - [Using RubyGems](#installation/scenario-1)
19
+ - [Using Bundler](#installation/scenario-2)
20
+ - [Installation Scenario 3](#installation/scenario-3)
21
+ - [Installation Scenario 4](#installation/scenario-4)
22
+ - [Configuration](#configuration)
23
+ - [Configuration Scenario 1](#configuration/scenario-1)
24
+ - [Configuration Scenario 2](#configuration/scenario-2)
25
+ - [Configuration Scenario 3](#configuration/scenario-3)
26
+ - [Configuration Scenario 4](#configuration/scenario-4)
27
+ - [Related Projects](#related-projects)
28
+ - [Dependencies](#related-projects/dependencies)
29
+ - [Friends](#related-projects/friends)
30
+ - [Alternatives](#related-projects/alternatives)
31
+ - [Contributing](#contributing)
32
+ - [Changelog](#changelog)
33
+ - [Authors](#authors)
34
+ - [Credits](#credits)
35
+ - [License](#license)
36
+
37
+ ## Features <a name="features"></a>
38
+
39
+ - Receive money from a mobile account to a business account
40
+ - Send money from a business account to a mobile account
41
+ - Send money from a business account to a another business account
42
+ - Revert a transaction
43
+ - Query the status of a transaction
44
+
45
+ ## Usage <a name="usage"></a>
46
+
47
+ ### Quickstart <a name="#usage/scenario-1"></a>
48
+
49
+ ### Receive Money from a Mobile Account <a name="#usage/scenario-2"></a>
50
+
51
+ ```ruby
52
+ require 'paymentsds/mpesa'
53
+
54
+ client = Paymentsds::MPesa::Client.new do |config|
55
+ config.api_key = '<REPLACE>' # API Key
56
+ config.public_key = '<REPLACE>' # Public Key
57
+ config.service_provider_code = '<REPLACE>' # input_ServiceProviderCode
58
+ end
59
+
60
+ begin
61
+ payment_data = {
62
+ from: '841234567', # input_CustomerMSISDN
63
+ reference: '11114', # input_ThirdPartyReference
64
+ transaction: 'T12344CC', # input_TransactionReference
65
+ amount: '10' # input_Amount
66
+ }
67
+
68
+ result = client.receive(payment_data)
69
+
70
+ if result.success?
71
+ puts result.data
72
+ end
73
+ rescue
74
+ puts 'Operation failed'
75
+ end
76
+ ```
77
+ ### Send Money to a Mobile Account <a name="#usage/scenario-3"></a>
78
+
79
+ ```ruby
80
+ require 'paymentsds/mpesa'
81
+
82
+ client = Paymentsds::MPesa::Client.new do |config|
83
+ config.api_key = '<REPLACE>' # API Key
84
+ config.public_key = '<REPLACE>' # Public Key
85
+ config.service_provider_code = '<REPLACE>' # input_ServiceProviderCode
86
+ end
87
+
88
+ begin
89
+ payment_data = {
90
+ to: '841234567', # input_CustomerMSISDN
91
+ reference: '11114', # input_ThirdPartyReference
92
+ transaction: 'T12344CC', # input_TransactionReference
93
+ amount: '10' # input_Amount
94
+ }
95
+
96
+ result = client.send(payment_data)
97
+
98
+ if result.success?
99
+ puts result.data
100
+ end
101
+ rescue
102
+ puts 'Operation failed'
103
+ end
104
+ ```
105
+
106
+ ### Send Money to a Business Account <a name="#usage/scenario-4"></a>
107
+
108
+ ```ruby
109
+ require 'paymentsds/mpesa'
110
+
111
+ client = Paymentsds::MPesa::Client.new do |config|
112
+ config.api_key = '<REPLACE>' # API Key
113
+ config.public_key = '<REPLACE>' # Public Key
114
+ config.service_provider_code = '<REPLACE>' # input_ServiceProviderCode
115
+ end
116
+
117
+ begin
118
+ payment_data = {
119
+ to: '979797', # input_ReceiverPartyCode
120
+ reference: '11114', # input_ThirdPartyReference
121
+ transaction: 'T12344CC', # input_TransactionReference
122
+ amount: '10' # input_Amount
123
+ }
124
+
125
+ result = client.send(payment_data)
126
+
127
+ if result.success?
128
+ puts result.data
129
+ end
130
+ rescue
131
+ puts 'Operation failed'
132
+ end
133
+ ```
134
+
135
+ ### Revert a Transaction <a name="#usage/scenario-5"></a>
136
+
137
+ ```ruby
138
+ require 'paymentsds/mpesa'
139
+
140
+ client = Paymentsds::MPesa::Client.new do |config|
141
+ config.api_key = '<REPLACE>' # API Key
142
+ config.public_key = '<REPLACE>' # Public Key
143
+ config.service_provider_code = '<REPLACE>' # input_ServiceProviderCode
144
+ config.initiator_identifier = '<REPLACE>' # input_InitiatorIdentifier,
145
+ config.security_identifier = '<REPLACE>' # input_SecurityCredential
146
+ end
147
+
148
+ begin
149
+ reversion_data = {
150
+ reference: '11114', # input_ThirdPartyReference
151
+ transaction: 'T12344CC', # input_TransactionReference
152
+ amount: '10' # input_ReversalAmounts
153
+ }
154
+
155
+ result = client.reversion(payment_data)
156
+
157
+ if result.success?
158
+ # Handle success scenario
159
+ end
160
+ rescue
161
+ # Handle failure scenario
162
+ end
163
+ ```
164
+
165
+ ### Query the Status of a Transaction <a name="#usage/scenario-6"></a>
166
+
167
+ ### Examples <a name="usage/scenario-7"></a>
168
+
169
+ ## Prerequisites <a name="prerequisites"></a>
170
+
171
+ - [Ruby 2.5+](https://www.ruby-lang.org)
172
+ - [RubyGems](https://rubygems.org)
173
+ - [Bundler](https://bundler.io)
174
+
175
+ ## Installation <a name="installation"></a>
176
+
177
+ ### Using RubyGems <a name="installation/scenario-1"></a>
178
+
179
+ ```bash
180
+ gem install paymentsds-mpesa
181
+ ```
182
+
183
+ ### Using Bundler <a name="installation/scenario-2"></a>
184
+
185
+ ```ruby
186
+ #Gemfile
187
+ gem 'paymentsds-mpesa'
188
+ ```
189
+
190
+ ### Installation Scenario 3 <a name="installation/scenario-3"></a>
191
+
192
+ ## Configuration <a name="configuration"></a>
193
+
194
+ ### Configuration Scenario 1 <a name="configuration/scenario-1"></a>
195
+
196
+ ### Configuration Scenario 2 <a name="configuration/scenario-2"></a>
197
+
198
+ ### Configuration Scenario 3 <a name="configuration/scenario-3"></a>
199
+
200
+ ## Related Projects <a name="related-projects"></a>
201
+
202
+ ### Dependencies <a name="related-projects/dependencies"></a>
203
+
204
+ #### Production Dependencies
205
+
206
+ - [Faraday](https://github.com/lostisland/faraday)
207
+ - [OpenSSL](https://github.com/ruby/openssl)
208
+
209
+
210
+ #### Development Dependencies
211
+
212
+ - [RSpec](https://github.com/rspec/rspec)
213
+ - [Rake](https://github.com/ruby/rake)
214
+ - [RuboCop](https://github.com/rubocop-hq/rubocop)
215
+ - [Overcommit](https://github.com/sds/overcommit)
216
+
217
+ ### Friends <a name="related-projects/friends"></a>
218
+
219
+ - [M-Pesa SDK for Javascript](https://github.com/paymentsds/mpesa-js-sdk)
220
+ - [M-Pesa SDK for PHP](https://github.com/paymentsds/mpesa-php-sdk)
221
+ - [M-Pesa SDK for Ruby](https://github.com/paymentsds/mpesa-ruby-sdk)
222
+ - [M-Pesa SDK for Python](https://github.com/paymentsds/mpesa-python-sdk)
223
+
224
+ ### Alternatives <a name="related-projects/alternatives"></a>
225
+
226
+ - [Alternative 1](https://github.com/<username>/<project>)
227
+ - [Alternative 2](https://github.com/<username>/<project>)
228
+ - [Alternative 3](https://github.com/<username>/<project>)
229
+ - [Alternative 4](https://github.com/<username>/<project>)
230
+
231
+
232
+ ### Inspiration
233
+
234
+ - [rosariopfernandes/mpesa-node-api](https://github.com/abdulmueid/mpesa-php-api)
235
+ - [karson/mpesa-php-sdk](https://github.com/karson/mpesa-php-sdk)
236
+ - [codeonweekends/mpesa-php-sdk](https://github.com/codeonweekends/mpesa-php-sdk)
237
+ - [abdulmueid/mpesa-php-api](https://github.com/abdulmueid/mpesa-php-api)
238
+ - [realdm/mpesasdk](https://github.com/realdm/mpesasdk)
239
+
240
+
241
+ ## Contributing <a name="contributing"></a>
242
+
243
+ ## Changelog <a name="changelog"></a>
244
+
245
+ ## Authors <a name="authors"></a>
246
+
247
+ - [Edson Michaque](https://github.com/edsonmichaque)
248
+ - [Nélio Macombo](https://github.com/neliomacombo)
249
+
250
+ ## Credits <a name="credits"></a>
251
+
252
+ ## License <a name="license"></a>
253
+
254
+ Copyright 2020 Anísio Mandlate, Edson Michaque, Elton Laice and Nélio Macombo
255
+
256
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
257
+
258
+ http://www.apache.org/licenses/LICENSE-2.0
259
+
260
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
261
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'paysuite/mpesa'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'mpesa/version'
4
+ require_relative 'mpesa/operation'
5
+ require_relative 'mpesa/constants'
6
+ require_relative 'mpesa/configuration'
7
+ require_relative 'mpesa/service'
8
+ require_relative 'mpesa/client'
9
+
10
+ module Paymentsds
11
+ module MPesa
12
+ class Error < StandardError; end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ class Client
6
+ def initialize
7
+ @service = Service.new
8
+ yield(@service.config) if block_given?
9
+ end
10
+
11
+ def send(intent)
12
+ @service.handle_send(intent)
13
+ end
14
+
15
+ def receive(intent)
16
+ @service.handle_receive(intent)
17
+ end
18
+
19
+ def query(intent)
20
+ @service.handle_query(intent)
21
+ end
22
+
23
+ def revert(intent)
24
+ @service.handle_revert(intent)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'openssl'
5
+
6
+ module Paymentsds
7
+ module MPesa
8
+ class Configuration
9
+ attr_accessor :access_token
10
+ attr_accessor :api_key
11
+ attr_accessor :public_key
12
+ attr_accessor :user_agent
13
+ attr_accessor :verify_ssl
14
+ attr_accessor :timeout
15
+ attr_accessor :debugging
16
+ attr_accessor :environment
17
+ attr_accessor :initiator_identifier
18
+ attr_accessor :security_credential
19
+ attr_accessor :service_provider_code
20
+ attr_accessor :origin
21
+ attr_reader :auth
22
+
23
+ def initialize
24
+ yield(self) if block_given?
25
+ end
26
+
27
+ def generate_access_token
28
+ has_keys = instance_variable_defined?('@api_key') && instance_variable_defined?('@public_key')
29
+ has_access_token = instance_variable_defined?('@access_token')
30
+
31
+ @auth = encrypt(@api_key, @public_key) if has_keys
32
+ @auth = @access_token if has_access_token
33
+ end
34
+
35
+ private
36
+
37
+ def encrypt(api_key, public_key)
38
+ plain_public_key = Base64.decode64 public_key
39
+ key = OpenSSL::PKey::RSA.new plain_public_key
40
+ encrypted_api_key = key.public_encrypt api_key
41
+
42
+ Base64.strict_encode64 encrypted_api_key
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ PATTERNS = {
6
+ PHONE_NUMBER: /^((00|\+)?(258))?8[45][0-9]{7}$/,
7
+ MONEY_AMOUNT: /^[1-9][0-9]*(\.[0-9]+)?$/,
8
+ WORD: /^\w+$/,
9
+ SERVICE_PROVIDER: /^[0-9]{5,6}$/
10
+ }.freeze
11
+
12
+ OPERATIONS = {
13
+ C2B_PAYMENT: {
14
+ method: :post,
15
+ port: '18352',
16
+ path: '/ipg/v1x/c2bPayment/singleStage/',
17
+ mapping: {
18
+ to: 'input_ServiceProviderCode',
19
+ from: 'input_CustomerMSISDN',
20
+ amount: 'input_Amount',
21
+ reference: 'input_ThirdPartyReference',
22
+ transaction: 'input_TransactionReference'
23
+ },
24
+ validation: {
25
+ to: PATTERNS[:SERVICE_PROVIDER],
26
+ from: PATTERNS[:PHONE_NUMBER],
27
+ amount: PATTERNS[:MONEY_AMOUNT],
28
+ reference: PATTERNS[:WORD],
29
+ transaction: PATTERNS[:WORD]
30
+ },
31
+ required: [
32
+ :to,
33
+ :from,
34
+ :amount,
35
+ :reference,
36
+ :transaction
37
+ ],
38
+ optional: [
39
+ :to
40
+ ]
41
+ },
42
+
43
+ B2C_PAYMENT: {
44
+ method: :post,
45
+ port: '18345',
46
+ path: '/ipg/v1x/b2cPayment/',
47
+ mapping: {
48
+ to: 'input_ServiceProviderCode',
49
+ from: 'input_CustomerMSISDN',
50
+ amount: 'input_Amount',
51
+ reference: 'input_ThirdPartyReference',
52
+ transaction: 'input_TransactionReference'
53
+ },
54
+ validation: {
55
+ to: PATTERNS[:SERVICE_PROVIDER],
56
+ from: PATTERNS[:PHONE_NUMBER],
57
+ amount: PATTERNS[:MONEY_AMOUNT],
58
+ reference: PATTERNS[:WORD],
59
+ transaction: PATTERNS[:WORD]
60
+ },
61
+ required: [
62
+ :to,
63
+ :from,
64
+ :amount,
65
+ :reference,
66
+ :transaction
67
+ ],
68
+ optional: [
69
+ :to
70
+ ]
71
+ },
72
+
73
+ B2B_PAYMENT: {
74
+ method: :post,
75
+ port: '18349',
76
+ path: '/ipg/v1x/b2bPayment/',
77
+ mapping: {
78
+ to: 'input_ServiceProviderCode',
79
+ from: 'input_CustomerMSISDN',
80
+ amount: 'input_Amount',
81
+ reference: 'input_ThirdPartyReference',
82
+ transaction: 'input_TransactionReference'
83
+ },
84
+ validation: {
85
+ to: PATTERNS[:SERVICE_PROVIDER],
86
+ from: PATTERNS[:PHONE_NUMBER],
87
+ amount: PATTERNS[:MONEY_AMOUNT],
88
+ reference: PATTERNS[:WORD],
89
+ transaction: PATTERNS[:WORD]
90
+ },
91
+ required: [
92
+ :to,
93
+ :from,
94
+ :amount,
95
+ :reference,
96
+ :transaction
97
+ ],
98
+ optional: [
99
+ :to
100
+ ]
101
+ },
102
+
103
+ REVERSAL: {
104
+ method: :put,
105
+ port: '18354',
106
+ path: '/ipg/v1x/reversal/',
107
+ mapping: {
108
+ to: 'input_ServiceProviderCode',
109
+ from: 'input_CustomerMSISDN',
110
+ amount: 'input_Amount',
111
+ reference: 'input_ThirdPartyReference',
112
+ transaction: 'input_TransactionReference'
113
+ },
114
+ validation: {
115
+ to: PATTERNS[:SERVICE_PROVIDER],
116
+ from: PATTERNS[:PHONE_NUMBER],
117
+ amount: PATTERNS[:MONEY_AMOUNT],
118
+ reference: PATTERNS[:WORD],
119
+ transaction: PATTERNS[:WORD]
120
+ },
121
+ required: [
122
+ :to,
123
+ :from,
124
+ :amount,
125
+ :reference,
126
+ :transaction
127
+ ],
128
+ optional: [
129
+ :to
130
+ ]
131
+ },
132
+
133
+ QUERY_TRANSACTION_STATUS: {
134
+ method: :get,
135
+ port: '18353',
136
+ path: '/ipg/v1x/queryTransactionStatus/',
137
+ mapping: {
138
+ to: 'input_ServiceProviderCode',
139
+ from: 'input_CustomerMSISDN',
140
+ amount: 'input_Amount',
141
+ reference: 'input_ThirdPartyReference',
142
+ transaction: 'input_TransactionReference'
143
+ },
144
+ validation: {
145
+ to: PATTERNS[:SERVICE_PROVIDER],
146
+ from: PATTERNS[:PHONE_NUMBER],
147
+ amount: PATTERNS[:MONEY_AMOUNT],
148
+ reference: PATTERNS[:WORD],
149
+ transaction: PATTERNS[:WORD]
150
+ },
151
+ required: [
152
+ :to,
153
+ :from,
154
+ :amount,
155
+ :reference,
156
+ :transaction
157
+ ],
158
+ optional: [
159
+ :to
160
+ ]
161
+ }
162
+ }.freeze
163
+ end
164
+ end
@@ -0,0 +1,8 @@
1
+ module Paymentsds
2
+ module MPesa
3
+ class ErrorType
4
+ MISSING_PROPERTIES = :MISSING_PROPERTIES
5
+ VALIDATION_ERROR = :VALIDATION_ERROR
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ class Operation
6
+ attr_accessor :name
7
+ attr_accessor :method
8
+ attr_accessor :port
9
+ attr_accessor :path
10
+ attr_accessor :mapping
11
+ attr_accessor :requires
12
+ attr_accessor :validation
13
+ attr_accessor :optional
14
+
15
+ def initialize
16
+ yield(self) if block_given?
17
+ end
18
+
19
+ def valid?
20
+ method_valid = ['get', 'post'].include? @method
21
+ port_valid = @port.to_s.match? /^[0-9]{1,5}$/
22
+ requires_is_array = @requires.is_a? Array
23
+
24
+ method_valid && port_valid && requires_is_array
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ PATTERNS = {
6
+ PHONE_NUMBER: /^((00|\+)?(258))?8[45][0-9]{7}$/,
7
+ MONEY_AMOUNT: /^[1-9][0-9]*(\.[0-9]+)?$/,
8
+ WORD: /^\w+$/,
9
+ SERVICE_PROVIDEER: /^[0-9]{5,6}$/
10
+ }.freeze
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module Paymentsds
2
+ module MPesa
3
+ class Result
4
+ attr_reader :error
5
+ attr_reader :data
6
+
7
+ def initialize(success, error, data)
8
+ @success = success
9
+ @error = error
10
+ @data = data
11
+ end
12
+
13
+ def success?
14
+ @success
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,181 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ class Service
6
+ attr_accessor :config
7
+
8
+ def initialize
9
+ @config = Paymentsds::MPesa::Configuration.new
10
+ end
11
+
12
+ def handle_send(intent)
13
+ operation = detect_operation(intent)
14
+ handle_request(operation, intent)
15
+ end
16
+
17
+ def handle_receive(intent)
18
+ handle_request(:C2B_PAYMENT, intent)
19
+ end
20
+
21
+ def handle_revert(intent)
22
+ handle_request(:REVERSAL, intent)
23
+ end
24
+
25
+ def handle_query(_intent)
26
+ handle_request(:QUERY_TRANSACTION_STATUS, ntent)
27
+ end
28
+
29
+ private
30
+
31
+ def handle_request(opcode, intent)
32
+ data = fill_optional_properties(opcode, intent)
33
+
34
+ missing_properties = detect_missing_properties(opcode, data)
35
+ unless missing_properties.empty?
36
+ return Result.new(false, ErrorType::MISSING_PROPERTIES, missing_properties)
37
+ end
38
+
39
+ errors = detect_errors(opcode, data)
40
+ unless errors.empty?
41
+ return Result.new(false, ErrorType::VALIDATION_ERROR, errors)
42
+ end
43
+
44
+ perform_request(opcode, intent)
45
+ end
46
+
47
+ def detect_operation(intent)
48
+ if intent.key? :to
49
+ case intent[:to]
50
+ when /^((00|\+)?258)?8[45][0-9]{7}$/
51
+ :B2C_PAYMENT
52
+ when /^[0-9]{5,6}$/
53
+ :B2B_PAYMENT
54
+ end
55
+
56
+ # Should raise an exception
57
+ end
58
+
59
+ # Should raise an exception
60
+ end
61
+
62
+ def detect_missing_properties(opcode, intent)
63
+ operation = Paymentsds::MPesa::OPERATIONS[opcode]
64
+
65
+ operation[:required] - intent.keys
66
+ end
67
+
68
+ def detect_errors(opcode, intent)
69
+ operation = Paymentsds::MPesa::OPERATIONS[opcode]
70
+
71
+ errors = []
72
+
73
+ intent.each do |k, _v|
74
+ errors.push(k) unless (intent[k]).match operation.validation[k]
75
+ end
76
+
77
+ errors
78
+ end
79
+
80
+ def fill_optional_properties(opcode, intent)
81
+ case opcode
82
+ when :C2B_PAYMENT
83
+ intent[:to] = @config.service_provider_code if intent.key?(:to) && !@config.service_provider_code.nil?
84
+ when :B2B_PAYMENT
85
+ intent[:to] = @config.service_provider_code if intent.key?(:to) && !@config.service_provider_code.nil?
86
+ when :B2C_PAYMENT
87
+ intent[:from] = @config.service_provider_code if intent.key?(:from) && !@config.service_provider_code.nil?
88
+ when :REVERSAL
89
+ intent[:to] = @config.service_provider_code if intent.key?(:to) && !@config.service_provider_code.nil?
90
+
91
+ if intent.key?(:initiator_identifier) && !@config.initiator_identifier.nil?
92
+ intent[:initiator_identifier] = @config.initiator_identifier
93
+ end
94
+
95
+ if intent.key?(:security_credential) && !@config.security_credential.nil?
96
+ intent[:security_credential] = @config.security_credential
97
+ end
98
+ when :QUERY_TRANSACTION_STATUS
99
+ intent[:to] = @config.service_provider_code if intent.key?(:to) && !@config.service_provider_code.nil?
100
+ end
101
+
102
+ intent
103
+ end
104
+
105
+ def build_request_body(opcode, intent)
106
+ operation = Paymentsds::MPesa::OPERATIONS[opcode]
107
+
108
+ body = {}
109
+ operation[:mapping].each do |k, v|
110
+ body[v] = intent[k]
111
+ end
112
+
113
+ body
114
+ end
115
+
116
+ def build_request_headers
117
+ generate_access_token
118
+
119
+ headers = {
120
+ 'Authorization': "Bearer #{@config.auth}",
121
+ 'Origin': @config.origin,
122
+ 'Content-Type': 'application/json',
123
+ 'User-Agent': @config.user_agent
124
+ }
125
+
126
+ headers
127
+ end
128
+
129
+ def perform_request(opcode, intent)
130
+ generate_access_token
131
+
132
+ request_data = {
133
+ base_url: "#{@config.environment.scheme}://#{@config.environment.domain}:#{operation[:port]}",
134
+ url: operation[:path],
135
+ method: operation[:method],
136
+ path: operation[:path],
137
+ headers: build_request_headers,
138
+ timeout: @config.timeout * 1000,
139
+ body: build_request_body(opcode, intent)
140
+ }
141
+
142
+ http_client = Faraday::Connection.new do |client|
143
+ client.url = "#{request_data[:base_url]}/#{request_data[:path]}"
144
+ client.headers = request_data[:headers]
145
+ client.options.timeout = request_data.timeout
146
+ end
147
+
148
+ case operation[:method]
149
+
150
+ when :get
151
+ response = http_client.get do |req|
152
+ req.params = request_data.body
153
+ end
154
+
155
+ when :post
156
+ response = http_client.post do |req|
157
+ req.body = request_data.body
158
+ end
159
+
160
+ when :put
161
+ response = http_client.put do |req|
162
+ req.body = request_data.body
163
+ end
164
+ end
165
+
166
+ bulid_response(response)
167
+ end
168
+
169
+ def build_response(result)
170
+ if result.status >= 200 && result.status < 300
171
+ response = Response.new(result.success?, nil, result.data)
172
+ end
173
+
174
+ end
175
+
176
+ def generate_access_token
177
+ @config.generate_access_token
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paymentsds
4
+ module MPesa
5
+ VERSION = '0.1.0.pre.alpha.1'
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/paymentsds/mpesa/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'paymentsds-mpesa'
5
+ spec.version = Paymentsds::MPesa::VERSION
6
+ spec.authors = ['Edson Michaque']
7
+ spec.email = ['edson@michaque.com']
8
+
9
+ spec.summary = 'A Ruby library aiming to helps developers integrating their products with M-Pesa Platform'
10
+ spec.description = %q{M-Pesa SDK for Ruby is an unofficial library aiming to help businesses integrating every M-Pesa operations to their Ruby applications.}
11
+ spec.homepage = "https://github.com/paymentsds/mpesa-ruby-sdk"
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+ spec.license = 'Apache-2.0'
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ spec.add_dependency 'faraday', '~> 1.0.1', '>= 1.0.1'
28
+ spec.add_development_dependency 'rspec', '~> 3.9.0', '>= 3.9.0'
29
+ spec.add_development_dependency 'overcommit', '~> 0.55.0', '>= 0.55.0'
30
+ spec.add_development_dependency 'rubocop', '~> 0.88.0', '>= 0.88.0'
31
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paymentsds-mpesa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.alpha.1
5
+ platform: ruby
6
+ authors:
7
+ - Edson Michaque
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.1
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.9.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.9.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 3.9.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.9.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: overcommit
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.55.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.55.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.55.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 0.55.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: rubocop
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.88.0
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.88.0
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.88.0
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.88.0
93
+ description: M-Pesa SDK for Ruby is an unofficial library aiming to help businesses
94
+ integrating every M-Pesa operations to their Ruby applications.
95
+ email:
96
+ - edson@michaque.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - ".gitignore"
102
+ - ".overcommit.yml"
103
+ - ".rspec"
104
+ - ".rubocop.yml"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/paymentsds/mpesa.rb
113
+ - lib/paymentsds/mpesa/client.rb
114
+ - lib/paymentsds/mpesa/configuration.rb
115
+ - lib/paymentsds/mpesa/constants.rb
116
+ - lib/paymentsds/mpesa/error_type.rb
117
+ - lib/paymentsds/mpesa/operation.rb
118
+ - lib/paymentsds/mpesa/patterns.rb
119
+ - lib/paymentsds/mpesa/result.rb
120
+ - lib/paymentsds/mpesa/service.rb
121
+ - lib/paymentsds/mpesa/version.rb
122
+ - paymentsds-mpesa.gemspec
123
+ homepage: https://github.com/paymentsds/mpesa-ruby-sdk
124
+ licenses:
125
+ - Apache-2.0
126
+ metadata:
127
+ homepage_uri: https://github.com/paymentsds/mpesa-ruby-sdk
128
+ source_code_uri: https://github.com/paymentsds/mpesa-ruby-sdk
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 2.3.0
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">"
141
+ - !ruby/object:Gem::Version
142
+ version: 1.3.1
143
+ requirements: []
144
+ rubygems_version: 3.1.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: A Ruby library aiming to helps developers integrating their products with
148
+ M-Pesa Platform
149
+ test_files: []