stanbic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 42c701e53003988ca6ea2d2b47c750a6251c354c1efc253f70efb7f77ea17e91
4
+ data.tar.gz: 69bde2a14bb9bcb17bf33dcbf90fdc89ec34dd94429e42d3e46874f846e53221
5
+ SHA512:
6
+ metadata.gz: 9bcf63bc1a3477e686388701e8640db293455e15229f925b5bc291ce1423005beea22af20fdafedb8f073893a8af83262047d0f656bc282381bee348d9765a9d
7
+ data.tar.gz: 732ee56d9f7c9e06b2c298d46a577802441326b0a19f756019c5b8e308144281132e5876bf6b73bea70e79607dab47011e86f322403ee45ee249ee0c2dd2e399
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-11-24
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in stanbic.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "pry"
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stanbic (0.1.0)
5
+ faraday (~> 1.7)
6
+ faraday_middleware (~> 1.2)
7
+ json
8
+ oauth2 (~> 2.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ coderay (1.1.3)
15
+ faraday (1.10.2)
16
+ faraday-em_http (~> 1.0)
17
+ faraday-em_synchrony (~> 1.0)
18
+ faraday-excon (~> 1.1)
19
+ faraday-httpclient (~> 1.0)
20
+ faraday-multipart (~> 1.0)
21
+ faraday-net_http (~> 1.0)
22
+ faraday-net_http_persistent (~> 1.0)
23
+ faraday-patron (~> 1.0)
24
+ faraday-rack (~> 1.0)
25
+ faraday-retry (~> 1.0)
26
+ ruby2_keywords (>= 0.0.4)
27
+ faraday-em_http (1.0.0)
28
+ faraday-em_synchrony (1.0.0)
29
+ faraday-excon (1.1.0)
30
+ faraday-httpclient (1.0.1)
31
+ faraday-multipart (1.0.4)
32
+ multipart-post (~> 2)
33
+ faraday-net_http (1.0.1)
34
+ faraday-net_http_persistent (1.2.0)
35
+ faraday-patron (1.0.0)
36
+ faraday-rack (1.0.0)
37
+ faraday-retry (1.0.3)
38
+ faraday_middleware (1.2.0)
39
+ faraday (~> 1.0)
40
+ hashie (5.0.0)
41
+ json (2.6.2)
42
+ jwt (2.5.0)
43
+ method_source (1.0.0)
44
+ minitest (5.16.3)
45
+ multi_xml (0.6.0)
46
+ multipart-post (2.2.3)
47
+ oauth2 (2.0.9)
48
+ faraday (>= 0.17.3, < 3.0)
49
+ jwt (>= 1.0, < 3.0)
50
+ multi_xml (~> 0.5)
51
+ rack (>= 1.2, < 4)
52
+ snaky_hash (~> 2.0)
53
+ version_gem (~> 1.1)
54
+ parallel (1.22.1)
55
+ parser (3.1.2.1)
56
+ ast (~> 2.4.1)
57
+ pry (0.14.1)
58
+ coderay (~> 1.1)
59
+ method_source (~> 1.0)
60
+ rack (2.2.4)
61
+ rainbow (3.1.1)
62
+ rake (13.0.6)
63
+ regexp_parser (2.6.1)
64
+ rexml (3.2.5)
65
+ rubocop (1.39.0)
66
+ json (~> 2.3)
67
+ parallel (~> 1.10)
68
+ parser (>= 3.1.2.1)
69
+ rainbow (>= 2.2.2, < 4.0)
70
+ regexp_parser (>= 1.8, < 3.0)
71
+ rexml (>= 3.2.5, < 4.0)
72
+ rubocop-ast (>= 1.23.0, < 2.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 1.4.0, < 3.0)
75
+ rubocop-ast (1.23.0)
76
+ parser (>= 3.1.1.0)
77
+ ruby-progressbar (1.11.0)
78
+ ruby2_keywords (0.0.5)
79
+ snaky_hash (2.0.1)
80
+ hashie
81
+ version_gem (~> 1.1, >= 1.1.1)
82
+ unicode-display_width (2.3.0)
83
+ version_gem (1.1.1)
84
+
85
+ PLATFORMS
86
+ x86_64-linux
87
+
88
+ DEPENDENCIES
89
+ minitest (~> 5.0)
90
+ pry
91
+ rake (~> 13.0)
92
+ rubocop (~> 1.21)
93
+ stanbic!
94
+
95
+ BUNDLED WITH
96
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ochieng Otieno
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Stanbic
2
+
3
+ Stanbic Payment APIs ruby sdk
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ bundle add stanbic
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ gem install stanbic
14
+
15
+ ## Usage
16
+
17
+ ### Initialize Stanbic Client
18
+
19
+ ```ruby
20
+ require "stanbic"
21
+ @client = Stanbic::Client.new(api_key: ENV['STANBIC_API_KEY'], api_secret: ENV['STANBIC_API_SECRET'])
22
+ ```
23
+
24
+ Make API calls using the @client object
25
+
26
+ ### Payments to Stanbic Accounts
27
+
28
+ ```ruby
29
+ send_to_stanbic = @client.stanbic_payments(to_account, amount)
30
+ ```
31
+
32
+ - `to_account`: stanbic recipient account number`REQUIRED`
33
+ - `amount`: amount to transact `REQUIRED`
34
+
35
+ ### Send Money to Mobile Money APIs
36
+
37
+ ```ruby
38
+ mobile_transfer = @client.mobile_payments(sender, receipient, amount, provider)
39
+ ```
40
+
41
+ - `sender`: sender mobile number`REQUIRED`
42
+ - `receipient`: receipient mobile number `REQUIRED`
43
+ - `amount`: amount to transact `REQUIRED`
44
+ - `provider`: mobile provider `REQUIRED`
45
+
46
+ - providers :
47
+ - MPESA
48
+ - T-KASH
49
+ - AIRTEL MONEY
50
+
51
+ ### STK Push - M-Pesa Checkout
52
+
53
+ ```ruby
54
+ mpesa_checkout = @client.mpesa_checkout(mobile_number, amount, bill_account_ref)
55
+ ```
56
+
57
+ - `mobile_number`: customer being charged mobile number`REQUIRED`
58
+ - `amount`: amount being deducted from M-Pesa `REQUIRED`
59
+ - `amount`: Stanbic account recieveing the funds `REQUIRED`
60
+
61
+ ### Inter-Bank Transfers API via Pesalink
62
+
63
+ > **Note**
64
+ > To be implemented
65
+
66
+ ```ruby
67
+ inter_bank_transfer = @client.inter_bank_transfer
68
+ ```
69
+
70
+ - `to_account`: stanbic recipient account number`REQUIRED`
71
+ - `amount`: amount to transact `REQUIRED`
72
+
73
+ ## Development
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/ochiengotieno304/stanbic>.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,12 @@
1
+ module Stanbic
2
+ module ApiExceptions
3
+ APIExceptionError = Class.new(StandardError)
4
+ BadRequestError = Class.new(APIExceptionError)
5
+ UnauthorizedError = Class.new(APIExceptionError)
6
+ ForbiddenError = Class.new(APIExceptionError)
7
+ ApiRequestsQuotaReachedError = Class.new(APIExceptionError)
8
+ NotFoundError = Class.new(APIExceptionError)
9
+ UnprocessableEntityError = Class.new(APIExceptionError)
10
+ ApiError = Class.new(APIExceptionError)
11
+ end
12
+ end
@@ -0,0 +1,201 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "json"
4
+
5
+ module Stanbic
6
+ class Client
7
+ HTTP_OK_CODE = 200
8
+ BASE_URL = "https://api.connect.stanbicbank.co.ke/api/sandbox/".freeze
9
+ attr_reader :adapter, :api_key, :api_secret, :currency_code
10
+
11
+ def initialize(api_key:, api_secret:, currency_code: "KES", adapter: Faraday.default_adapter)
12
+ @api_key = api_key
13
+ @api_secret = api_secret
14
+ @adapter = adapter
15
+ @currency_code = currency_code
16
+ end
17
+
18
+ def stanbic_payments(account_to, amount)
19
+ dbs_reference_id = Random.rand(1_000_000_000_000)
20
+ end_to_end_id = Random.rand(1_000_000_000)
21
+ post_body = {
22
+ "originatorAccount": {
23
+ "identification": {
24
+ "mobileNumber": ""
25
+ }
26
+ },
27
+
28
+ "requestedExecutionDate": Time.now.strftime("%Y-%m-%d"),
29
+
30
+ "dbsReferenceId": dbs_reference_id.to_s,
31
+
32
+ "txnNarrative": "TRANSACTION NARRATIVE",
33
+
34
+ "callBackUrl": "http://clientdomain.com/omnichannel/esbCallback",
35
+
36
+ "transferTransactionInformation": {
37
+ "instructedAmount": {
38
+ "amount": amount.to_s,
39
+ "currencyCode": currency_code
40
+ },
41
+
42
+ "counterparty": {
43
+ "name": "J. Sparrow",
44
+ "postalAddress": {
45
+ "addressLine1": "Some street",
46
+ "addressLine2": "99",
47
+ "postCode": "1100 ZZ",
48
+ "town": "Amsterdam",
49
+ "country": "NL"
50
+ }
51
+ },
52
+
53
+ "counterpartyAccount": {
54
+ "identification": {
55
+ "identification": account_to.to_s
56
+ }
57
+ },
58
+
59
+ "remittanceInformation": {
60
+ "type": "UNSTRUCTURED",
61
+ "content": "SALARY"
62
+ },
63
+
64
+ "endToEndIdentification": end_to_end_id.to_s
65
+ }
66
+ }
67
+
68
+ request(http_method: :post, endpoint: "stanbic-payments", params: post_body)
69
+ end
70
+
71
+ def mobile_transfer(sender, receipient, amount, provider)
72
+ # PROVIDERS = {
73
+ # MPESA,
74
+ # T-KASH,
75
+ # AIRTET MONEY
76
+ # }
77
+ dbs_reference_id = Random.rand(1_000_000_000_000)
78
+ end_to_end_id = Random.rand(1_000_000_000)
79
+ post_body = {
80
+ "originatorAccount": {
81
+ "identification": {
82
+ "mobileNumber": sender.to_s
83
+ }
84
+ },
85
+ "requestedExecutionDate": Time.now.strftime("%Y-%m-%d"),
86
+ "dbsReferenceId": dbs_reference_id.to_s,
87
+ "txnNarrative": "TRANSACTION NARRATIVE",
88
+ "callBackUrl": "http://client_domain.com/omnichannel/esbCallback",
89
+ "transferTransactionInformation": {
90
+ "instructedAmount": {
91
+ "amount": amount.to_s,
92
+ "currencyCode": currency_code
93
+ },
94
+ "mobileMoneyMno": {
95
+ "name": provider.to_s
96
+ },
97
+ "counterparty": {
98
+ "name": "J. Sparrow",
99
+ "mobileNumber": receipient.to_s,
100
+ "postalAddress": {
101
+ "addressLine1": "Some street",
102
+ "addressLine2": "99",
103
+ "postCode": "1100 ZZ",
104
+ "town": "Amsterdam",
105
+ "country": "NL"
106
+ }
107
+ },
108
+ "remittanceInformation": {
109
+ "type": "UNSTRUCTURED",
110
+ "content": "SALARY"
111
+ },
112
+ "endToEndIdentification": end_to_end_id.to_s
113
+ }
114
+ }
115
+ request(http_method: :post, endpoint: "mobile-payments", params: post_body)
116
+ end
117
+
118
+ def inter_bank_transfer
119
+ request(http_method: :post, endpoint: "pesalink-payments", params: options)
120
+ end
121
+
122
+ def mpesa_checkout(mobile_number, amount, bill_account_ref)
123
+ dbs_reference_id = ([*("A".."Z"), *("0".."9")] - %w[0 1 I O]).sample(13).join
124
+ post_body = {
125
+ "dbsReferenceId": dbs_reference_id.to_s,
126
+ "billAccountRef": bill_account_ref.to_s,
127
+ "amount": amount.to_s,
128
+ "mobileNumber": mobile_number.to_s
129
+ }
130
+ request(http_method: :post, endpoint: "mpesa-checkout", params: post_body)
131
+ end
132
+
133
+ def inspect
134
+ "#<Stanbic::Client>"
135
+ end
136
+
137
+ private
138
+
139
+ def get_token(client_id, client_secret)
140
+ data = {
141
+ scope: "payments",
142
+ grant_type: "client_credentials",
143
+ client_id: client_id,
144
+ client_secret: client_secret
145
+ }
146
+
147
+ url = "https://api.connect.stanbicbank.co.ke/api/sandbox/auth/oauth2/token"
148
+ response = Faraday.post(url) do |req|
149
+ req.headers["Content-Type"] = "application/x-www-form-urlencoded"
150
+ req.body = URI.encode_www_form(data)
151
+ end
152
+
153
+ parsed_response = JSON.parse(response.body)
154
+
155
+ OpenStruct.new(parsed_response).access_token
156
+ end
157
+
158
+ def connection
159
+ token = get_token(api_key, api_secret)
160
+
161
+ @connection ||= Faraday.new do |conn|
162
+ conn.url_prefix = BASE_URL
163
+ conn.request :json
164
+ conn.headers["Authorization"] = "Bearer #{token}"
165
+ conn.response :json, content_type: "application/json"
166
+ conn.adapter adapter
167
+ end
168
+ end
169
+
170
+ def request(http_method:, endpoint:, params: {})
171
+ response = connection.public_send(http_method, endpoint, params)
172
+ parsed_response = OpenStruct.new(response.body)
173
+ # response.body
174
+
175
+ # return parsed_response.httpCode if parsed_response.httpCode == response_successful?
176
+
177
+ # raise error_class, "Code: #{response.status}, response: #{response.body}"
178
+ end
179
+
180
+ # def error_class
181
+ # case response.status
182
+ # when HTTP_BAD_REQUEST_CODE
183
+ # BadRequestError
184
+ # when HTTP_UNAUTHORIZED_CODE
185
+ # UnauthorisedError
186
+ # when HTTP_FORBIDDEN_CODE
187
+ # ForbiddenError
188
+ # when HTTP_NOT_FOUND_CODE
189
+ # NotFoundError
190
+ # when HTTP_UNPROCESSABLE_ENTITY_CODE
191
+ # UnprocessableEntityError
192
+ # else
193
+ # APIError
194
+ # end
195
+ # end
196
+
197
+ def response_successful?
198
+ HTTP_OK_CODE
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,3 @@
1
+ module Stanbic
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,10 @@
1
+ module Stanbic
2
+ module HttpStatusCodes
3
+ HTTP_OK_CODE = 200
4
+ HTTP_BAD_REQUEST_CODE = 400
5
+ HTTP_UNAUTHORIZED_CODE = 401
6
+ HTTP_FORBIDDEN_CODE = 403
7
+ HTTP_NOT_FOUND_CODE = 404
8
+ HTTP_UNPROCESSABLE_ENTITY_CODE = 429
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stanbic
4
+ VERSION = "0.1.0"
5
+ end
data/lib/stanbic.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "stanbic/version"
4
+
5
+ module Stanbic
6
+ autoload :Client, "stanbic/client"
7
+ autoload :Error, "stanbic/error"
8
+ autoload :HttpsStatusCodes, "stanbic/http_status_codes"
9
+ autoload :ApiExceptions, "stanbic/api_exceptions"
10
+ end
data/sig/stanbic.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Stanbic
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stanbic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ochieng Otieno
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-04 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.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oauth2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Ruby SDK for Stanbic Kilele APIs
70
+ email:
71
+ - ochiengotieno71@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rubocop.yml"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/stanbic.rb
84
+ - lib/stanbic/api_exceptions.rb
85
+ - lib/stanbic/client.rb
86
+ - lib/stanbic/error.rb
87
+ - lib/stanbic/http_status_codes.rb
88
+ - lib/stanbic/version.rb
89
+ - sig/stanbic.rbs
90
+ homepage: https://github.com/ochiengotieno304/stanbic-api-ruby-wrapper
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ homepage_uri: https://github.com/ochiengotieno304/stanbic-api-ruby-wrapper
95
+ source_code_uri: https://github.com/ochiengotieno304/stanbic-api-ruby-wrapper
96
+ changelog_uri: https://github.com/ochiengotieno304/stanbic-api-ruby-wrapper/blob/main/CHANGELOG.md
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 2.6.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.3.7
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Ruby SDK for Stanbic Kilele APIs
116
+ test_files: []