mpesa_connect 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/access_token.rb +70 -0
- data/lib/cert.cer +38 -0
- data/lib/mpesa_connect.rb +163 -0
- data/lib/mpesa_connect/version.rb +3 -0
- data/lib/pubkey.pem +9 -0
- data/lib/security_credentials.rb +21 -0
- data/mpesa_connect.gemspec +42 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df25e1cf7fb4470050aa2cb2396b856f844798cf
|
4
|
+
data.tar.gz: efbef4a6bc5cc851f975f66a288fcf63ad443169
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0310d675bb63970f1c028114078a9be7db1add163a71191eaf69ab356c855b92bd44af4c47fbbfc3e7f3afe2cea0c8c1efd4ab22459841145658553f9de372f7
|
7
|
+
data.tar.gz: 7a6611473c8391c7a2a2d4d5fb850995371a52bb7136e08ab392e09cc79addab8c8d0dc9ab477be8b20401fd29c646bb0e42d2bd92858572107bc0a0ba376ea5
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 mboyaberry@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 [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 mboya
|
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,43 @@
|
|
1
|
+
# MpesaConnect
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mpesa_connect`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mpesa_connect'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mpesa_connect
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mpesa_connect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the MpesaConnect project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mpesa_connect/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mpesa_connect"
|
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__)
|
data/bin/setup
ADDED
data/lib/access_token.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module MpesaConnect
|
2
|
+
class AccessToken
|
3
|
+
def initialize key, secret
|
4
|
+
@key = key
|
5
|
+
@secret = secret
|
6
|
+
@redis = Redis.new
|
7
|
+
|
8
|
+
load_from_redis
|
9
|
+
end
|
10
|
+
|
11
|
+
def is_valid?
|
12
|
+
has_token? && !token_expired?
|
13
|
+
end
|
14
|
+
|
15
|
+
def token_expired?
|
16
|
+
expire_time = @timestamp.to_i + @expires_in.to_i
|
17
|
+
return expire_time < Time.now.to_i + 58
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_token?
|
21
|
+
return !@token.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def refresh
|
25
|
+
get_new_access_token
|
26
|
+
load_from_redis
|
27
|
+
end
|
28
|
+
|
29
|
+
def load_from_redis
|
30
|
+
data = @redis.get(@key)
|
31
|
+
if (data.nil? || data.empty?)
|
32
|
+
@token = nil
|
33
|
+
@timestamp = nil
|
34
|
+
@expires_in = nil
|
35
|
+
else
|
36
|
+
parsed = JSON.parse(data)
|
37
|
+
@token = parsed['access_token']
|
38
|
+
@timestamp = parsed['time_stamp']
|
39
|
+
@expires_in = parsed['expires_in']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def access_token
|
44
|
+
if is_valid?
|
45
|
+
return @token
|
46
|
+
else
|
47
|
+
refresh
|
48
|
+
return @token
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_new_access_token
|
53
|
+
encode = encode_credentials @key, @secret
|
54
|
+
headers = {
|
55
|
+
"Authorization" => "Basic #{encode}"
|
56
|
+
}
|
57
|
+
url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
|
58
|
+
response = HTTParty.get(url, headers: headers)
|
59
|
+
|
60
|
+
hash = JSON.parse(response.body).merge(Hash['time_stamp',Time.now.to_i])
|
61
|
+
@redis.set @key, hash.to_json
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def encode_credentials key, secret
|
66
|
+
credentials = "#{key}:#{secret}"
|
67
|
+
encoded_credentials = Base64.encode64(credentials).split("\n").join
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/cert.cer
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
2
|
+
MIIGkzCCBXugAwIBAgIKXfBp5gAAAD+hNjANBgkqhkiG9w0BAQsFADBbMRMwEQYK
|
3
|
+
CZImiZPyLGQBGRYDbmV0MRkwFwYKCZImiZPyLGQBGRYJc2FmYXJpY29tMSkwJwYD
|
4
|
+
VQQDEyBTYWZhcmljb20gSW50ZXJuYWwgSXNzdWluZyBDQSAwMjAeFw0xNzA0MjUx
|
5
|
+
NjA3MjRaFw0xODAzMjExMzIwMTNaMIGNMQswCQYDVQQGEwJLRTEQMA4GA1UECBMH
|
6
|
+
TmFpcm9iaTEQMA4GA1UEBxMHTmFpcm9iaTEaMBgGA1UEChMRU2FmYXJpY29tIExp
|
7
|
+
bWl0ZWQxEzARBgNVBAsTClRlY2hub2xvZ3kxKTAnBgNVBAMTIGFwaWdlZS5hcGlj
|
8
|
+
YWxsZXIuc2FmYXJpY29tLmNvLmtlMIIBIjANB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163 @@
|
|
1
|
+
require "mpesa_connect/version"
|
2
|
+
require 'base64'
|
3
|
+
require 'redis'
|
4
|
+
require 'httparty'
|
5
|
+
require 'openssl'
|
6
|
+
require 'access_token'
|
7
|
+
require 'security_credentials'
|
8
|
+
|
9
|
+
module MpesaConnect
|
10
|
+
class Client
|
11
|
+
BASE_URL = "https://sandbox.safaricom.co.ke"
|
12
|
+
BALANCE_URL = "https://sandbox.safaricom.co.ke/mpesa/accountbalance/v1/query"
|
13
|
+
|
14
|
+
def initialize key, secret, security_credentials
|
15
|
+
@key = key
|
16
|
+
@secret = secret
|
17
|
+
@access_token = nil
|
18
|
+
@sec_cred = security_credentials
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_urls timeout=nil, transaction=nil, confirm=nil, validate=nil
|
22
|
+
@timeout = timeout
|
23
|
+
@transaction = transaction
|
24
|
+
@confirm = confirm
|
25
|
+
@validate = validate
|
26
|
+
end
|
27
|
+
|
28
|
+
def account_balance initiator, party_a
|
29
|
+
url = "#{BASE_URL}/mpesa/accountbalance/v1/query"
|
30
|
+
headers = {
|
31
|
+
"Authorization" => "Bearer #{get_token}",
|
32
|
+
"Content-Type" => "application/json"
|
33
|
+
}
|
34
|
+
body = {
|
35
|
+
Initiator: "#{initiator}",
|
36
|
+
SecurityCredential: "#{@sec_cred}",
|
37
|
+
CommandID: "AccountBalance",
|
38
|
+
PartyA: party_a,
|
39
|
+
IdentifierType: "4",
|
40
|
+
Remarks: "Account Balance",
|
41
|
+
QueueTimeOutURL: "#{@timeout}",
|
42
|
+
ResultURL: "#{@transaction}"
|
43
|
+
}.to_json
|
44
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
45
|
+
JSON.parse(response.body)
|
46
|
+
end
|
47
|
+
|
48
|
+
def reversal initiator, transaction_id, amount, receiver_party
|
49
|
+
url = "#{BASE_URL}/mpesa/reversal/v1/request"
|
50
|
+
headers = {
|
51
|
+
"Authorization" => "Bearer #{get_token}",
|
52
|
+
"Content-Type" => "application/json"
|
53
|
+
}
|
54
|
+
body = {
|
55
|
+
Initiator: "#{initiator}",
|
56
|
+
SecurityCredential: "#{@sec_cred}",
|
57
|
+
CommandID:"TransactionReversal",
|
58
|
+
TransactionID: "#{transaction_id}",
|
59
|
+
Amount: "#{amount}",
|
60
|
+
ReceiverParty: "#{receiver_party}",
|
61
|
+
RecieverIdentifierType:"4",
|
62
|
+
ResultURL: "#{@transaction}",
|
63
|
+
QueueTimeOutURL: "#{@timeout}",
|
64
|
+
Remarks: "reverse transaction #{transaction_id}"
|
65
|
+
}.to_json
|
66
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
67
|
+
JSON.parse(response.body)
|
68
|
+
end
|
69
|
+
|
70
|
+
def transaction_status initiator, party_a, transaction_id
|
71
|
+
url = "#{BASE_URL}/mpesa/transactionstatus/v1/query"
|
72
|
+
headers = {
|
73
|
+
"Authorization" => "Bearer #{get_token}",
|
74
|
+
"Content-Type" => "application/json"
|
75
|
+
}
|
76
|
+
body = {
|
77
|
+
Initiator: "#{initiator}",
|
78
|
+
SecurityCredential: "#{@sec_cred}",
|
79
|
+
CommandID: "TransactionStatusQuery",
|
80
|
+
TransactionID: "#{transaction_id}",
|
81
|
+
PartyA: "#{party_a}",
|
82
|
+
IdentifierType: "3",
|
83
|
+
Remarks: "Transaction Status",
|
84
|
+
QueueTimeOutURL: "#{@timeout}",
|
85
|
+
ResultURL: "#{@transaction}"
|
86
|
+
}.to_json
|
87
|
+
|
88
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
89
|
+
JSON.parse(response.body)
|
90
|
+
end
|
91
|
+
|
92
|
+
def c2b_register_url shortcode, response_type
|
93
|
+
url = "#{BASE_URL}/mpesa/c2b/v1/registerurl"
|
94
|
+
headers = {
|
95
|
+
"Authorization" => "Bearer #{get_token}",
|
96
|
+
"Content-Type" => "application/json"
|
97
|
+
}
|
98
|
+
body = {
|
99
|
+
ShortCode: "#{shortcode}",
|
100
|
+
ResponseType: "#{response_type}",
|
101
|
+
ConfirmationURL: "#{@confirm}",
|
102
|
+
ValidationURL: "#{@validate}"
|
103
|
+
}.to_json
|
104
|
+
|
105
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
106
|
+
JSON.parse(response.body)
|
107
|
+
end
|
108
|
+
|
109
|
+
def c2b_transaction shortcode, amount, msisdn, bill_reference=nil
|
110
|
+
url = "#{BASE_URL}/mpesa/c2b/v1/simulate"
|
111
|
+
headers = {
|
112
|
+
"Authorization" => "Bearer #{get_token}",
|
113
|
+
"Content-Type" => "application/json"
|
114
|
+
}
|
115
|
+
body = {
|
116
|
+
ShortCode: "#{shortcode}",
|
117
|
+
CommandID: "CustomerPayBillOnline",
|
118
|
+
Amount: "#{amount}",
|
119
|
+
Msisdn: "#{msisdn}",
|
120
|
+
BillRefNumber: "#{bill_reference}"
|
121
|
+
}.to_json
|
122
|
+
|
123
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
124
|
+
JSON.parse(response.body)
|
125
|
+
end
|
126
|
+
|
127
|
+
def b2c_transaction initiator, amount, party_a, party_b,
|
128
|
+
url = "#{BASE_URL}/mpesa/b2c/v1/paymentrequest"
|
129
|
+
headers = {
|
130
|
+
"Authorization" => "Bearer #{get_token}",
|
131
|
+
"Content-Type" => "application/json"
|
132
|
+
}
|
133
|
+
body = {
|
134
|
+
InitiatorName: "#{initiator}",
|
135
|
+
SecurityCredential: "#{@sec_cred}",
|
136
|
+
CommandID: "SalaryPayment",
|
137
|
+
Amount: "#{amount}",
|
138
|
+
PartyA: "#{party_a}",
|
139
|
+
PartyB: "#{party_b}",
|
140
|
+
Remarks: "Salary Payments",
|
141
|
+
QueueTimeOutURL: "#{@timeout}",
|
142
|
+
ResultURL: "#{@transaction}"
|
143
|
+
}.to_json
|
144
|
+
response = HTTParty.post(url, headers: headers, body: body)
|
145
|
+
JSON.parse(response.body)
|
146
|
+
end
|
147
|
+
|
148
|
+
def security_password security_credentials
|
149
|
+
@sec_cred = security_credentials
|
150
|
+
encrypted_security_password
|
151
|
+
end
|
152
|
+
|
153
|
+
private
|
154
|
+
def get_token
|
155
|
+
AccessToken.new(@key, @secret).access_token
|
156
|
+
end
|
157
|
+
|
158
|
+
def encrypted_security_password
|
159
|
+
password = SecurityCredentials.new(@sec_cred).encrypt_security_cred
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
data/lib/pubkey.pem
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
-----BEGIN PUBLIC KEY-----
|
2
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoknIb5Tm1hxOVdFsOejA
|
3
|
+
s6veAai32Zv442BLuOGkFKUeCUM2s0K8XEsUt6BP25rQGNlTCTEqfdtRrym6bt5k
|
4
|
+
0fTDscf0yMCoYzaxTh1mejg8rPO6bD8MJB0cFWRUeLEyWjMeEPsYVSJFv7T58IdA
|
5
|
+
n7/RhkrpBl1dT7SmIZfNVkIlD35+Cxgab+u7+c7dHh6mWguEEoE3NbV7Xjl60zbD
|
6
|
+
/Buvmu6i9EYz+27jNVPI6pRXHvp+ajIzTSsieD8Ztz1eoC9mphErasAGpMbR1sba
|
7
|
+
9bM6hjw4tyTWnJDz7RdQQmnsW1NfFdYdK0qDRKUX7SG6rQkBqVhndFve4SDFRq6w
|
8
|
+
vQIDAQAB
|
9
|
+
-----END PUBLIC KEY-----
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'pry-nav'
|
3
|
+
|
4
|
+
module MpesaConnect
|
5
|
+
class SecurityCredentials
|
6
|
+
|
7
|
+
def initialize security_cred
|
8
|
+
@security_cred = security_cred
|
9
|
+
end
|
10
|
+
|
11
|
+
def encrypt_security_cred
|
12
|
+
byte_array = @security_cred.bytes.to_a.to_s
|
13
|
+
key_file = "lib/pubkey.pem"
|
14
|
+
public_key = File.read(key_file)
|
15
|
+
ssl = OpenSSL::PKey::RSA.new(public_key)
|
16
|
+
|
17
|
+
encrypted_string = Base64.encode64(ssl.public_encrypt(byte_array)).split("\n").join
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "mpesa_connect/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mpesa_connect"
|
8
|
+
spec.version = MpesaConnect::VERSION
|
9
|
+
spec.authors = ["mboya"]
|
10
|
+
spec.email = ["mboyaberry@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{connect to Mpesa API}
|
13
|
+
spec.description = %q{connect to Mpesa API to perform available transactions}
|
14
|
+
spec.homepage = "https://mboya.github.io/mpesa_connect/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "httparty"
|
34
|
+
spec.add_dependency "redis-rack"
|
35
|
+
spec.add_dependency "redis-namespace"
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
39
|
+
spec.add_development_dependency "pry"
|
40
|
+
spec.add_development_dependency "pry-nav"
|
41
|
+
spec.add_development_dependency "webmock"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mpesa_connect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mboya
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
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: redis-rack
|
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: redis-namespace
|
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: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.15'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.15'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-nav
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: connect to Mpesa API to perform available transactions
|
140
|
+
email:
|
141
|
+
- mboyaberry@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".ruby-version"
|
148
|
+
- ".travis.yml"
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- Gemfile
|
151
|
+
- LICENSE.txt
|
152
|
+
- README.md
|
153
|
+
- Rakefile
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- lib/access_token.rb
|
157
|
+
- lib/cert.cer
|
158
|
+
- lib/mpesa_connect.rb
|
159
|
+
- lib/mpesa_connect/version.rb
|
160
|
+
- lib/pubkey.pem
|
161
|
+
- lib/security_credentials.rb
|
162
|
+
- mpesa_connect.gemspec
|
163
|
+
homepage: https://mboya.github.io/mpesa_connect/
|
164
|
+
licenses:
|
165
|
+
- MIT
|
166
|
+
metadata: {}
|
167
|
+
post_install_message:
|
168
|
+
rdoc_options: []
|
169
|
+
require_paths:
|
170
|
+
- lib
|
171
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
requirements: []
|
182
|
+
rubyforge_project:
|
183
|
+
rubygems_version: 2.6.12
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: connect to Mpesa API
|
187
|
+
test_files: []
|