peperusha 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d2464129e7e2ff1fbfb3502faf96418b0b1cb04f63d2f05ebeab85d17a467acf
4
+ data.tar.gz: 9133eb59666008ac409a295c766578e8143dd0af462ea58961cbe7c08c216902
5
+ SHA512:
6
+ metadata.gz: 896a80adf95215065157bc4f5a0fdc9a7db5cf9b53eee5483752b0538e2c878ef28e4220c9e324274868e91fa7d829790c7b628de573f5f90cb4e084967c48cd
7
+ data.tar.gz: 503ac21f3d9723aaf9fde23f09bfdb94b721949319a8571a23449f30fed2f4063555f05aeac444585dca541e40a15c01f4e561da829cf06cab4190e917a843ab
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,10 @@
1
+ ## 1.0.0 / 2020-09-14
2
+
3
+ * [ENHANCEMENT] Add logger for Mpesa requests and responses
4
+ * [FEATURE] Add mpesa authentication mechanism
5
+ * [BUGFIX] Fix faraday response json parsing
6
+ * [BUGFIX] Add activesupport for missing delegates signature
7
+
8
+ ## 1.0.0 / 2020-09-13
9
+
10
+ * Initial release!
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kerandisylvance@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in peperusha.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ peperusha (0.1.0)
5
+ activesupport
6
+ faraday
7
+ interactor
8
+ json
9
+ logger
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (6.0.3.3)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ zeitwerk (~> 2.2, >= 2.2.2)
20
+ concurrent-ruby (1.1.7)
21
+ diff-lcs (1.4.4)
22
+ faraday (1.0.1)
23
+ multipart-post (>= 1.2, < 3)
24
+ i18n (1.8.5)
25
+ concurrent-ruby (~> 1.0)
26
+ interactor (3.1.2)
27
+ json (2.3.1)
28
+ logger (1.4.2)
29
+ minitest (5.14.2)
30
+ multipart-post (2.1.1)
31
+ rake (12.3.3)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.2)
37
+ rspec-support (~> 3.9.3)
38
+ rspec-expectations (3.9.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.3)
45
+ thread_safe (0.3.6)
46
+ tzinfo (1.2.7)
47
+ thread_safe (~> 0.1)
48
+ zeitwerk (2.4.0)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ peperusha!
55
+ rake (~> 12.0)
56
+ rspec (~> 3.0)
57
+
58
+ BUNDLED WITH
59
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sylvance Mbaka
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.
@@ -0,0 +1,175 @@
1
+ # Peperusha
2
+
3
+ Mpesa Client with better error handling and edge case protection.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'peperusha'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install peperusha
20
+
21
+ ## Usage
22
+ ### Intro
23
+ `N/B`: All the result below will respond to these;
24
+ ```ruby
25
+ result.success? # either true or false
26
+ result.errors # an array of strings describing the errors, default is []
27
+ result.body # {body: body} if success otherwise {}
28
+ ```
29
+
30
+ To use `peperusha` first get the client token:
31
+ ```ruby
32
+ result = Peperusha::Authenticate.call(
33
+ consumer_key: consumer_key,
34
+ consumer_secret: consumer_secret
35
+ )
36
+ result.body # returns the response body
37
+ result.body['access_token'] # the token to be used henceforth
38
+ result.body['expires_in'] # the time, in seconds, the token takes to expire
39
+ ```
40
+
41
+ ### Register urls
42
+ To ensure that customer to business calls work, you will have to register the callback urls:
43
+ ```ruby
44
+ result = Peperusha::C2bRegisterUrls.call(
45
+ token: token,
46
+ business_number: business_number,
47
+ confirmation_url: confirmation_url,
48
+ validation_url: validation_url
49
+ )
50
+ result.body # returns the response body
51
+ ```
52
+
53
+ ### Payments
54
+ We can find the test credentials for these here `https://developer.safaricom.co.ke/test_credentials`
55
+
56
+ For customer to business send money via paybill do:
57
+ ```ruby
58
+ result = Peperusha::CustomerPayViaPaybill.call(
59
+ token: token,
60
+ account_number: account_number,
61
+ amount: amount,
62
+ customer_number: customer_number,
63
+ business_paybill_number: business_paybill_number
64
+ )
65
+ result.body # returns the response body
66
+ ```
67
+
68
+ For customer to business send money via till do:
69
+ ```ruby
70
+ result = Peperusha::CustomerPayViaTill.call(
71
+ token: token,
72
+ amount: amount,
73
+ customer_number: customer_number,
74
+ business_till_number: business_till_number
75
+ )
76
+ result.body # returns the response body
77
+ ```
78
+
79
+ For business to customer send money do:
80
+ ```ruby
81
+ result = Peperusha::BusinessToCustomer.call(
82
+ amount: amount, # e.g 70
83
+ business_number: business_number, # e.g 123454
84
+ customer_number: customer_number, # e.g 254790123456
85
+ initiator_name: initiator_name, # e.g John_Doe
86
+ remarks: remarks,
87
+ result_url: result_url, # url where to send notification upon processing of the payment request.
88
+ security_credential: security_credential, # e.g 32SzVdmCvjpmQfw3X2RK8UAv7xuhh304dXxFC5+3lslkk2TDJY/Lh6ESVwtqMxJzF7qA==
89
+ timeout_url: timeout_url, # url where to send notification incase the payment request is timed out while awaiting processing in the queue.
90
+ token: token # the access token
91
+ )
92
+ result.body # returns the response body
93
+ ```
94
+
95
+ For business till to another business till number do:
96
+ ```ruby
97
+ result = Peperusha::BusinessToTill.call(
98
+ amount: amount, # e.g 70
99
+ business_number1: business_number1, # e.g 123321
100
+ business_number2: business_number2, # e.g 123456 should be a till number receiving the funds
101
+ initiator_name: initiator_name, # e.g Business 1
102
+ remarks: remarks,
103
+ result_url: result_url, # url where to send notification upon processing of the payment request.
104
+ security_credential: security_credential, # e.g 32SzVdmCvjpmQfw3X2RK8UAv7xuhh304dXxFC5+3lslkk2TDJY/Lh6ESVwtqMxJzF7qA==
105
+ timeout_url: timeout_url, # url where to send notification incase the payment request is timed out while awaiting processing in the queue.
106
+ token: token # the access token
107
+ )
108
+ result.body # returns the response body
109
+ ```
110
+
111
+ For business paybill to another business paybill number do:
112
+ ```ruby
113
+ result = Peperusha::BusinessToPaybill.call(
114
+ account_reference: account_reference, # e.g 123 the account value under the paybill
115
+ amount: amount, # e.g 70
116
+ business_number1: business_number1, # e.g 123321
117
+ business_number2: business_number2, # e.g 123456 should be a paybill number receiving the funds
118
+ initiator_name: initiator_name, # e.g Business 1
119
+ remarks: remarks,
120
+ result_url: result_url, # url where to send notification upon processing of the payment request.
121
+ security_credential: security_credential, # e.g 32SzVdmCvjpmQfw3X2RK8UAv7xuhh304dXxFC5+3lslkk2TDJY/Lh6ESVwtqMxJzF7qA==
122
+ timeout_url: timeout_url, # url where to send notification incase the payment request is timed out while awaiting processing in the queue.
123
+ token: token # the access token
124
+ )
125
+ result.body # returns the response body
126
+ ```
127
+
128
+ ### Inquiry
129
+ To check the status of a B2C and C2B APIs transactions:
130
+ ```ruby
131
+ result = Peperusha::TransactionInquiry.call(
132
+ token: token,
133
+ initiator_name: initiator_name,
134
+ receiving_number: receiving_number,
135
+ remarks: remarks,
136
+ result_url: result_url,
137
+ security_credential: security_credential,
138
+ timeout_url: timeout_url,
139
+ transaction_id: transaction_id
140
+ )
141
+ ```
142
+
143
+ ### Reversal
144
+ To reverse a B2C APIs transactions:
145
+ ```ruby
146
+ result = Peperusha::TransactionReversal.call(
147
+ token: token,
148
+ initiator_name: initiator_name,
149
+ receiving_number: receiving_number,
150
+ remarks: remarks,
151
+ result_url: result_url,
152
+ security_credential: security_credential,
153
+ timeout_url: timeout_url,
154
+ transaction_id: transaction_id,
155
+ )
156
+ ```
157
+
158
+ ## Development
159
+
160
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
161
+
162
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
163
+
164
+ ## Contributing
165
+
166
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sylvance/peperusha. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sylvance/peperusha/blob/master/CODE_OF_CONDUCT.md). Other issues contact `peperusha.kakitu@gmail.com`.
167
+
168
+
169
+ ## License
170
+
171
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
172
+
173
+ ## Code of Conduct
174
+
175
+ Everyone interacting in the Peperusha project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sylvance/peperusha/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "peperusha"
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,7 @@
1
+ require 'peperusha/version'
2
+ require 'peperusha/authenticate'
3
+
4
+ module Peperusha
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class Authenticate
9
+ include Interactor
10
+
11
+ delegate :consumer_key, :consumer_secret, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ response = Peperusha::Client.invoke_token_request(consumer_key, consumer_secret)
20
+
21
+ if response.status == 200
22
+ data = JSON.parse(response.body)
23
+ context.body = data
24
+ else
25
+ client_errors = Peperusha::Client.build_errors_collection(response)
26
+ context.fail!(errors: client_errors)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def check_if_params_missing
33
+ errors = []
34
+ errors << 'consumer_key.missing' if consumer_key.nil?
35
+ errors << 'consumer_secret.missing' if consumer_secret.nil?
36
+ errors
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,60 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class BusinessToCustomer
9
+ include Interactor
10
+
11
+ delegate :amount, :business_number, :customer_number, :initiator_name, :remarks, :result_url, :security_credential, :timeout_url, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/b2c/v1/paymentrequest'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'token.missing' if token.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'business_number.missing' if business_number.nil?
38
+ errors << 'customer_number.missing' if customer_number.nil?
39
+ errors << 'security_credential.missing' if security_credential.nil?
40
+ errors
41
+ end
42
+
43
+ def attributes
44
+ result_url = nil if result_url.nil?
45
+ timeout_url = nil if timeout_url.nil?
46
+ {
47
+ 'Amount': amount,
48
+ 'CommandID': 'BusinessPayment',
49
+ 'InitiatorName': initiator_name,
50
+ 'Occasion': 'Generated by peperusha',
51
+ 'PartyA': business_number,
52
+ 'PartyB': customer_number,
53
+ 'Remarks': remarks,
54
+ 'ResultURL': result_url,
55
+ 'SecurityCredential': security_credential,
56
+ 'QueueTimeOutURL': timeout_url
57
+ }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,64 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class BusinessToPaybill
9
+ include Interactor
10
+
11
+ delegate :account_reference, :amount, :business_number1, :business_number2, :initiator_name, :remarks, :result_url, :security_credential, :timeout_url, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/b2b/v1/paymentrequest'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'token.missing' if token.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'business_number1.missing' if business_number1.nil?
38
+ errors << 'business_number2.missing' if business_number2.nil?
39
+ errors << 'account_reference.missing' if account_reference.nil?
40
+ errors << 'security_credential.missing' if security_credential.nil?
41
+ errors
42
+ end
43
+
44
+ def attributes
45
+ result_url = nil if result_url.nil?
46
+ timeout_url = nil if timeout_url.nil?
47
+ {
48
+ 'AccountReference': account_reference,
49
+ 'Amount': amount,
50
+ 'CommandID': 'BusinessPayBill',
51
+ 'Initiator': initiator_name,
52
+ 'Occasion': 'Generated by peperusha',
53
+ 'PartyA': business_number1,
54
+ 'PartyB': business_number2,
55
+ 'RecieverIdentifierType': '4',
56
+ 'Remarks': remarks,
57
+ 'ResultURL': result_url,
58
+ 'SecurityCredential': security_credential,
59
+ 'SenderIdentifierType': '4',
60
+ 'QueueTimeOutURL': timeout_url
61
+ }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class BusinessToTill
9
+ include Interactor
10
+
11
+ delegate :amount, :business_number1, :business_number2, :initiator_name, :remarks, :result_url, :security_credential, :timeout_url, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/b2b/v1/paymentrequest'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'token.missing' if token.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'business_number1.missing' if business_number1.nil?
38
+ errors << 'business_number2.missing' if business_number2.nil?
39
+ errors << 'security_credential.missing' if security_credential.nil?
40
+ errors
41
+ end
42
+
43
+ def attributes
44
+ result_url = nil if result_url.nil?
45
+ timeout_url = nil if timeout_url.nil?
46
+ {
47
+ 'Amount': amount,
48
+ 'CommandID': 'BusinessBuyGoods',
49
+ 'Initiator': initiator_name,
50
+ 'Occasion': 'Generated by peperusha',
51
+ 'PartyA': business_number1,
52
+ 'PartyB': business_number2,
53
+ 'RecieverIdentifierType': '2',
54
+ 'Remarks': remarks,
55
+ 'ResultURL': result_url,
56
+ 'SecurityCredential': security_credential,
57
+ 'SenderIdentifierType': '2',
58
+ 'QueueTimeOutURL': timeout_url
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,49 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class C2bRegisterUrls
9
+ include Interactor
10
+
11
+ delegate :business_number, :confirmation_url, :token, :validation_url, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/c2b/v1/registerurl'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'confirmation_url.missing' if confirmation_url.nil?
36
+ errors << 'business_number.missing' if business_number.nil?
37
+ errors
38
+ end
39
+
40
+ def attributes
41
+ {
42
+ 'ConfirmationURL': confirmation_url,
43
+ 'ResponseType': 'Canceled',
44
+ 'ShortCode': business_number,
45
+ 'ValidationURL': validation_url
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,54 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ require_relative('./logs.rb')
5
+
6
+ module Peperusha
7
+ class Client
8
+ def self.invoke_token_request(consumer_key, consumer_secret)
9
+ conn = Faraday.new(url: 'https://sandbox.safaricom.co.ke') do |build|
10
+ build.request :url_encoded
11
+ build.use Peperusha::Logs
12
+ build.adapter Faraday.default_adapter
13
+ end
14
+
15
+ conn.basic_auth(consumer_key, consumer_secret)
16
+
17
+ conn.get('oauth/v1/generate') do |req|
18
+ req.params['grant_type'] = 'client_credentials'
19
+ end
20
+ end
21
+
22
+ def self.invoke_post_request(token, domain_url, body)
23
+ base_url = 'https://sandbox.safaricom.co.ke'
24
+ headers = {
25
+ 'Accept' => 'application/json',
26
+ 'Authorization' => "Bearer #{token}",
27
+ 'Content-Type' => 'application/json'
28
+ }
29
+
30
+ conn = Faraday.new(url: base_url, headers: headers) do |build|
31
+ build.request :url_encoded
32
+ build.use Peperusha::Logs
33
+ build.adapter Faraday.default_adapter
34
+ end
35
+
36
+ conn.post(domain_url) do |req|
37
+ req.body = body.to_json
38
+ end
39
+ end
40
+
41
+ def self.build_errors_collection(response)
42
+ data = JSON.parse(response.body)
43
+ [
44
+ {
45
+ status_code: response.status,
46
+ error: {
47
+ code: data['errorCode'],
48
+ errorMessage: data['errorMessage']
49
+ }
50
+ }
51
+ ]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class CustomerPayViaPaybill
9
+ include Interactor
10
+
11
+ delegate :account_number, :amount, :business_paybill_number, :customer_number, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/c2b/v1/simulate'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'account_number.missing' if account_number.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'business_paybill_number.missing' if business_paybill_number.nil?
38
+ errors << 'customer_number.missing' if customer_number.nil?
39
+ errors
40
+ end
41
+
42
+ def attributes
43
+ {
44
+ 'Amount': amount,
45
+ 'BillRefNumber': account_number,
46
+ 'CommandID': 'CustomerPayBillOnline',
47
+ 'Msisdn': customer_number,
48
+ 'ShortCode': business_paybill_number
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class CustomerPayViaTill
9
+ include Interactor
10
+
11
+ delegate :amount, :business_till_number, :customer_number, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/c2b/v1/simulate'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'amount.missing' if amount.nil?
36
+ errors << 'business_till_number.missing' if business_till_number.nil?
37
+ errors << 'customer_number.missing' if customer_number.nil?
38
+ errors
39
+ end
40
+
41
+ def attributes
42
+ {
43
+ 'Amount': amount,
44
+ 'CommandID': 'CustomerBuyGoodsOnline:',
45
+ 'Msisdn': customer_number,
46
+ 'ShortCode': business_till_number
47
+ }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ require 'logger'
2
+
3
+ module Peperusha
4
+ class Logs
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ @logger = Logger.new(STDOUT)
9
+ end
10
+
11
+ def call(env)
12
+ on_request('request', env)
13
+ @app.call(env).on_complete do
14
+ on_response('response', env)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def on_request(phase, env)
21
+ @logger.info("#{phase} : #{env.method} - #{env.url}") if env.method and env.url
22
+ end
23
+
24
+ def on_response(phase, env)
25
+ @logger.info("#{phase} : #{env.body}") if env.body
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class TransactionInquiry
9
+ include Interactor
10
+
11
+ delegate :initiator_name, :receiving_number, :remarks, :result_url, :security_credential, :timeout_url, :transaction_id, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = 'mpesa/transactionstatus/v1/query'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'token.missing' if token.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'receiving_number.missing' if receiving_number.nil?
38
+ errors << 'transaction_id.missing' if transaction_id.nil?
39
+ errors << 'account_reference.missing' if account_reference.nil?
40
+ errors << 'security_credential.missing' if security_credential.nil?
41
+ errors
42
+ end
43
+
44
+ def attributes
45
+ result_url = nil if result_url.nil?
46
+ timeout_url = nil if timeout_url.nil?
47
+ {
48
+ 'CommandID': 'TransactionStatusQuery',
49
+ 'IdentifierType': '1',
50
+ 'Initiator': initiator_name,
51
+ 'Occasion': 'Generated by peperusha',
52
+ 'PartyA': receiving_number,
53
+ 'Remarks': remarks,
54
+ 'ResultURL': result_url,
55
+ 'SecurityCredential': security_credential,
56
+ 'TransactionID': transaction_id,
57
+ 'QueueTimeOutURL': timeout_url
58
+ }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_support'
2
+ require 'interactor'
3
+ require 'json'
4
+
5
+ require_relative('./client.rb')
6
+
7
+ module Peperusha
8
+ class TransactionReversal
9
+ include Interactor
10
+
11
+ delegate :initiator_name, :remarks, :result_url, :security_credential, :timeout_url, :transaction_id, :token, to: :context
12
+
13
+ before do
14
+ missing_params_errors = check_if_params_missing
15
+ context.fail!(errors: missing_params_errors) if missing_params_errors.size > 0
16
+ end
17
+
18
+ def call
19
+ path = '/mpesa/reversal/v1/request'
20
+ response = Peperusha::Client.invoke_post_request(token, path, attributes)
21
+
22
+ if response.status == 200
23
+ data = JSON.parse(response.body)
24
+ context.body = data
25
+ else
26
+ client_errors = Peperusha::Client.build_errors_collection(response)
27
+ context.fail!(errors: client_errors)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def check_if_params_missing
34
+ errors = []
35
+ errors << 'token.missing' if token.nil?
36
+ errors << 'amount.missing' if amount.nil?
37
+ errors << 'receiving_number.missing' if receiving_number.nil?
38
+ errors << 'transaction_id.missing' if transaction_id.nil?
39
+ errors << 'account_reference.missing' if account_reference.nil?
40
+ errors << 'security_credential.missing' if security_credential.nil?
41
+ errors
42
+ end
43
+
44
+ def attributes
45
+ result_url = nil if result_url.nil?
46
+ timeout_url = nil if timeout_url.nil?
47
+ {
48
+ 'CommandID': 'TransactionReversal',
49
+ 'IdentifierType': '1',
50
+ 'Initiator': initiator_name,
51
+ 'Occasion': 'Generated by peperusha',
52
+ 'PartyA': receiving_number,
53
+ 'RecieverIdentifierType': '1',
54
+ 'Remarks': remarks,
55
+ 'ResultURL': result_url,
56
+ 'SecurityCredential': security_credential,
57
+ 'TransactionID': transaction_id,
58
+ 'QueueTimeOutURL': timeout_url
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Peperusha
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'lib/peperusha/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "peperusha"
5
+ spec.version = Peperusha::VERSION
6
+ spec.authors = ["Sylvance Kerandi"]
7
+ spec.email = ["peperusha.kakitu@gmail.com"]
8
+
9
+ spec.summary = %q{Mpesa Client with better error handling and edge case protection.}
10
+ spec.description = %q{Peperusha use the power of ruby interactors to bring in better error handling and edge case scenarios for easier debugging.}
11
+ spec.homepage = "https://github.com/sylvance/peperusha"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/sylvance/peperusha"
19
+ spec.metadata["changelog_uri"] = "https://github.com/sylvance/peperusha"
20
+
21
+ # dependencies
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency "faraday"
24
+ spec.add_dependency "interactor"
25
+ spec.add_dependency "json"
26
+ spec.add_dependency "logger"
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peperusha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sylvance Kerandi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: interactor
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: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Peperusha use the power of ruby interactors to bring in better error
84
+ handling and edge case scenarios for easier debugging.
85
+ email:
86
+ - peperusha.kakitu@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/peperusha.rb
104
+ - lib/peperusha/authenticate.rb
105
+ - lib/peperusha/business_to_customer.rb
106
+ - lib/peperusha/business_to_paybill.rb
107
+ - lib/peperusha/business_to_till.rb
108
+ - lib/peperusha/c2b_register_urls.rb
109
+ - lib/peperusha/client.rb
110
+ - lib/peperusha/customer_pay_via_paybill.rb
111
+ - lib/peperusha/customer_pay_via_till.rb
112
+ - lib/peperusha/logs.rb
113
+ - lib/peperusha/transaction_inquiry.rb
114
+ - lib/peperusha/transaction_reversal.rb
115
+ - lib/peperusha/version.rb
116
+ - peperusha.gemspec
117
+ homepage: https://github.com/sylvance/peperusha
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ homepage_uri: https://github.com/sylvance/peperusha
122
+ source_code_uri: https://github.com/sylvance/peperusha
123
+ changelog_uri: https://github.com/sylvance/peperusha
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.3.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.1.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Mpesa Client with better error handling and edge case protection.
143
+ test_files: []