paystack 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/README.md +213 -14
- data/lib/paystack.rb +1 -31
- data/lib/paystack/objects/base.rb +4 -4
- data/lib/paystack/objects/card.rb +1 -1
- data/lib/paystack/objects/transactions.rb +0 -7
- data/lib/paystack/utils/utils.rb +1 -1
- data/lib/paystack/version.rb +1 -1
- metadata +2 -4
- data/lib/paystack/modules/crypto.rb +0 -11
- data/lib/paystack/modules/tokenmanager.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bfffac1071ccab650bec0e73af7b5caf8ff6f57
|
4
|
+
data.tar.gz: 5c8125e3f70f8321fc66c5d5adeb758c8cad6368
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d2e673cb05a9bd987450e392138af759619b9e855b56c419d9672bb9855ac5b1dc56af7ff99c877c4ced46c45b38021d2da81ed7ad1c124a00e125e2ebbdd71
|
7
|
+
data.tar.gz: de3ec225ab575e393e03739a916c1b6ed5d32d1d2cebfb9f24f4f30dfba489c0f798ca8004c6604c469befcd8ed5c5630e233b681e7d25da135578103491ad1a
|
data/README.md
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
# Paystack
|
2
2
|
|
3
3
|
|
4
|
-
A ruby gem for easy integration of Paystack
|
4
|
+
A ruby gem for easy integration of [Paystack](https://paystack.co/).
|
5
5
|
|
6
|
-
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/paystack`. To experiment with that code, run `bin/console` for an interactive prompt.
|
7
|
-
|
8
|
-
TODO: Delete this and the text above, and describe your gem
|
9
6
|
|
10
7
|
## Installation
|
11
8
|
|
@@ -23,25 +20,30 @@ Or install it yourself as:
|
|
23
20
|
|
24
21
|
$ gem install paystack
|
25
22
|
|
26
|
-
## Usage
|
23
|
+
## Basic Usage
|
27
24
|
|
28
|
-
### 1. Instantiate Paystack Object
|
29
25
|
|
30
|
-
paystack = Paystack.new(public_key, secret_key)
|
31
26
|
|
32
|
-
|
27
|
+
### Instantiate Paystack Object
|
33
28
|
|
34
29
|
```ruby
|
35
30
|
|
36
|
-
|
31
|
+
paystackObj = Paystack.new(public_key, secret_key)
|
37
32
|
|
38
33
|
```
|
39
34
|
|
40
|
-
|
35
|
+
A secure way is to set your public and private keys as environmental variables `PAYSTACK_PUBLIC_KEY` and `PAYSTACK_PRIVATE_KEY` respectively. Then you instantiate without parameters
|
36
|
+
|
37
|
+
```ruby
|
41
38
|
|
39
|
+
paystackObj = Paystack.new
|
42
40
|
|
41
|
+
```
|
42
|
+
It throws a `PaystackBadKeyError` when either of the keys are invalid or cannot be found as environment variables.
|
43
43
|
|
44
|
-
|
44
|
+
|
45
|
+
|
46
|
+
### Instantiate a Card object
|
45
47
|
|
46
48
|
```ruby
|
47
49
|
card = new PaystackCard(
|
@@ -50,16 +52,213 @@ Methods available in the Paystack class include
|
|
50
52
|
:expiryMonth => "07",
|
51
53
|
:expiryYear => "19"
|
52
54
|
)
|
55
|
+
|
56
|
+
isvalid = card.isValidCard
|
57
|
+
```
|
58
|
+
The `isValidCard` method determines validity of the card i.e. Expiry status, Luhn checksum validity etc.
|
59
|
+
All card values/fields should be String literals.
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
### Initialize transaction and get Authorization URL
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
67
|
+
transactions = PaystackTransactions.new(paystackObj)
|
68
|
+
result = transactions.initializeTransaction(
|
69
|
+
:reference => "blablablabla-YOUR-UNIQUE-REFERENCE-HERE",
|
70
|
+
:amount => 300000,
|
71
|
+
:email => "xxxxxx@gmail.com",
|
72
|
+
)
|
73
|
+
auth_url = results['data']['authorization_url']
|
74
|
+
```
|
75
|
+
NOTE: Amount is in kobo i.e. `100000 = 100000 kobo = 1000 naira`
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
### Charge using Authorization code for returning customers
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
|
83
|
+
result = transactions.chargeAuthorization(
|
84
|
+
"WwdkojpoAJo", # Authorization code
|
85
|
+
"xxxxxx@gmail.com", # Customer email
|
86
|
+
2000000, # Amount
|
87
|
+
:reference => "blablablabla-YOUR-UNIQUE-REFERENCE-HERE"
|
88
|
+
)
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
## Transactions
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
### List transactions
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
|
101
|
+
page_number = 1
|
102
|
+
transactions = PaystackTransactions.new(paystackObj)
|
103
|
+
result = transactions.list(page_number) #Optional `page_number` parameter
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
### Get a transaction
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
|
111
|
+
transaction_id = ""123456778""
|
112
|
+
transactions = PaystackTransactions.new(paystackObj)
|
113
|
+
result = transactions.get(transaction_id)
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
### Verify a transaction
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
|
121
|
+
transaction_reference = "blablablabla-YOUR-VALID-UNIQUE-REFERENCE-HERE"
|
122
|
+
transactions = PaystackTransactions.new(paystackObj)
|
123
|
+
result = transactions.verify(transaction_reference)
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
|
128
|
+
### Get transaction totals
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
|
132
|
+
transactions = PaystackTransactions.new(paystackObj)
|
133
|
+
result = transactions.totals()
|
134
|
+
|
53
135
|
```
|
54
136
|
|
55
137
|
|
138
|
+
## Customers
|
56
139
|
|
57
140
|
|
58
|
-
|
141
|
+
### List Customers
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
|
145
|
+
page_number = 1
|
146
|
+
customers = PaystackCustomers.new(paystackObj)
|
147
|
+
result = customers.list(page_number) #Optional `page_number` parameter, 50 items per page
|
148
|
+
customers_list = result['data']
|
149
|
+
|
150
|
+
```
|
151
|
+
|
152
|
+
### Get a customer
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
|
156
|
+
customer_id = "123456778"
|
157
|
+
customers = PaystackCustomers.new(paystackObj)
|
158
|
+
result = customers.get(customer_id)
|
159
|
+
customer = result['data']
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
### Create new customer
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
|
167
|
+
customers = PaystackCustomers.new(paystackObj)
|
168
|
+
result = customers.create(
|
169
|
+
:first_name => "Victor",
|
170
|
+
:last_name => "Ikoro",
|
171
|
+
:phone => "+234707666669"
|
172
|
+
:email => "xxxxx@gmail.com"
|
173
|
+
)
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
### Update customer details
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
|
181
|
+
customer_id = "123456778"
|
182
|
+
customers = PaystackCustomers.new(paystackObj)
|
183
|
+
# Updating last name and email of customer
|
184
|
+
result = customers.update(
|
185
|
+
customer_id,
|
186
|
+
:last_name => "Ikorodu",
|
187
|
+
:email => "xxxxx-modified@gmail.com"
|
188
|
+
)
|
189
|
+
|
190
|
+
```
|
191
|
+
|
192
|
+
## Plans
|
193
|
+
|
194
|
+
### List Plans
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
|
198
|
+
page_number = 1
|
199
|
+
plans = PaystackPlans.new(paystackObj)
|
200
|
+
result = plans.list(page_number) #Optional `page_number` parameter, 50 items per page
|
201
|
+
plans_list = result['data']
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
### Get plan detail
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
|
209
|
+
plan_id = "123456778"
|
210
|
+
plans = PaystackPlans.new(paystackObj)
|
211
|
+
result = plans.get(plan_id)
|
212
|
+
plan = result['data']
|
213
|
+
|
214
|
+
```
|
215
|
+
|
216
|
+
### Create new plan
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
|
220
|
+
plans = PaystackPlans.new(paystackObj)
|
221
|
+
result = plans.create(
|
222
|
+
|
223
|
+
:name => "Test Plan",
|
224
|
+
:description => "Dev Test Plan",
|
225
|
+
:amount => 30000, #in KOBO
|
226
|
+
:interval => "monthly", #monthly, yearly, quarterly, weekly etc
|
227
|
+
:currency => "NGN"
|
228
|
+
)
|
229
|
+
|
230
|
+
```
|
231
|
+
|
232
|
+
### Update plan details
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
|
236
|
+
plan_id = ""123456778""
|
237
|
+
plans = PaystackPlans.new(paystackObj)
|
238
|
+
result = plans.update(
|
239
|
+
plan_id,
|
240
|
+
:name => "Test Plan Updated",
|
241
|
+
:amount => 500000, #in KOBO
|
242
|
+
:interval => "weekly"
|
243
|
+
)
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
|
248
|
+
## Static methods
|
249
|
+
`PaystackTransactions`, `PaystackCustomers` and `PaystackPlans` methods can be called statically, You just need to pass the paystack object as the first parameter e.g. `verify` method in `PaystackTransactions` can be called like this
|
250
|
+
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
|
254
|
+
transaction_reference = "blablablabla-YOUR-VALID-UNIQUE-REFERENCE-HERE"
|
255
|
+
result = PaystackTransactions.verify(paystackObj, transaction_reference)
|
256
|
+
puts result['message']
|
257
|
+
|
258
|
+
```
|
259
|
+
|
59
260
|
|
60
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
261
|
|
62
|
-
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).
|
63
262
|
|
64
263
|
## Contributing
|
65
264
|
|
data/lib/paystack.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'rest-client'
|
2
|
-
require 'paystack/modules/tokenmanager.rb'
|
3
2
|
require 'paystack/error.rb'
|
3
|
+
require 'paystack/modules/api.rb'
|
4
4
|
require 'paystack/utils/utils.rb'
|
5
|
-
|
6
5
|
require 'paystack/objects/card.rb'
|
7
6
|
require 'paystack/objects/customers.rb'
|
8
7
|
require 'paystack/objects/plans.rb'
|
@@ -43,9 +42,6 @@ class Paystack
|
|
43
42
|
|
44
43
|
end
|
45
44
|
|
46
|
-
def getToken card
|
47
|
-
return TokenManager.create(card, @public_key)
|
48
|
-
end
|
49
45
|
|
50
46
|
def setPublicKey public_key
|
51
47
|
@public_key = public_key
|
@@ -55,30 +51,4 @@ class Paystack
|
|
55
51
|
@public_key = public_key
|
56
52
|
end
|
57
53
|
|
58
|
-
# def chargeToken(token, amount,args = {})
|
59
|
-
# token = token;
|
60
|
-
# amount = amount
|
61
|
-
# email = args[:email]
|
62
|
-
# reference = args[:reference]
|
63
|
-
# result = nil;
|
64
|
-
#
|
65
|
-
# begin
|
66
|
-
# response = RestClient.post "#{API::BASE_URL}#{API::TRANSACTION_PATH}/charge_token", {:token => token, :amount => amount, :email => email, :reference => reference}.to_json, :Authorization => "Bearer #{@private_key}", :content_type => :json, :accept => :json
|
67
|
-
# unless (response.code == 200 || response.code == 201)
|
68
|
-
# raise PaystackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
69
|
-
# end
|
70
|
-
# result = JSON.parse(response.body)
|
71
|
-
# unless(result['status'] != 0 )
|
72
|
-
# raise PaystackServerError.new(response), "Server Message: #{result['message']}"
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# rescue JSON::ParserError => jsonerr
|
76
|
-
# raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
77
|
-
#
|
78
|
-
# rescue PaystackServerError => e
|
79
|
-
# Utils.serverErrorHandler(e)
|
80
|
-
# end
|
81
|
-
# return result
|
82
|
-
# end
|
83
|
-
|
84
54
|
end
|
@@ -26,7 +26,7 @@ class PaystackBaseObject
|
|
26
26
|
rescue JSON::ParserError => jsonerr
|
27
27
|
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
28
28
|
|
29
|
-
rescue
|
29
|
+
rescue PaystackServerError => e
|
30
30
|
Utils.serverErrorHandler(e)
|
31
31
|
end
|
32
32
|
return result
|
@@ -52,7 +52,7 @@ class PaystackBaseObject
|
|
52
52
|
rescue JSON::ParserError => jsonerr
|
53
53
|
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
54
54
|
|
55
|
-
rescue
|
55
|
+
rescue PaystackServerError => e
|
56
56
|
Utils.serverErrorHandler(e)
|
57
57
|
end
|
58
58
|
return result
|
@@ -73,7 +73,7 @@ class PaystackBaseObject
|
|
73
73
|
rescue JSON::ParserError => jsonerr
|
74
74
|
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
75
75
|
|
76
|
-
rescue
|
76
|
+
rescue PaystackServerError => e
|
77
77
|
Utils.serverErrorHandler(e)
|
78
78
|
end
|
79
79
|
return result
|
@@ -94,7 +94,7 @@ class PaystackBaseObject
|
|
94
94
|
rescue JSON::ParserError => jsonerr
|
95
95
|
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
96
96
|
|
97
|
-
rescue
|
97
|
+
rescue PaystackServerError => e
|
98
98
|
Utils.serverErrorHandler(e)
|
99
99
|
end
|
100
100
|
return result
|
@@ -71,7 +71,7 @@ class PaystackCard
|
|
71
71
|
end
|
72
72
|
formatted_number = @number.gsub(/\s+|-/) {|s| '' }.strip
|
73
73
|
|
74
|
-
if(Utils.isEmpty(formatted_number) || !Utils.isWholePositiveNumber(formatted_number) || !Utils.
|
74
|
+
if(Utils.isEmpty(formatted_number) || !Utils.isWholePositiveNumber(formatted_number) || !Utils.isLuhnValidNumber(formatted_number))
|
75
75
|
|
76
76
|
return false
|
77
77
|
end
|
@@ -22,9 +22,6 @@ class PaystackTransactions < PaystackBaseObject
|
|
22
22
|
return PaystackTransactions.totals(@paystack, page)
|
23
23
|
end
|
24
24
|
|
25
|
-
def chargeToken(token, amount,args = {})
|
26
|
-
return PaystackTransactions.chargeToken(@paystack,token, amount, args)
|
27
|
-
end
|
28
25
|
|
29
26
|
def chargeAuthorization(authorization_code, email, amount,args = {})
|
30
27
|
return PaystackTransactions.chargeAuthorization(@paystack,authorization_code,email, amount, args)
|
@@ -56,10 +53,6 @@ class PaystackTransactions < PaystackBaseObject
|
|
56
53
|
initGetRequest(paystackObj, "#{API::TRANSACTION_PATH}/totals?page=#{page}")
|
57
54
|
end
|
58
55
|
|
59
|
-
def PaystackTransactions.chargeToken(paystackObj,token, amount,args = {})
|
60
|
-
hash = {:token => token, :amount => amount}.merge(args)
|
61
|
-
initPostRequest(paystackObj,"#{API::TRANSACTION_PATH}/charge_token", hash, true)
|
62
|
-
end
|
63
56
|
def PaystackTransactions.chargeAuthorization(paystackObj,authorization_code,email, amount,args = {})
|
64
57
|
hash = {:authorization_code => authorization_code, :amount => amount, :email => email}.merge(args)
|
65
58
|
initPostRequest(paystackObj,"#{API::TRANSACTION_PATH}/charge_authorization", hash, true)
|
data/lib/paystack/utils/utils.rb
CHANGED
data/lib/paystack/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paystack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,8 +86,6 @@ files:
|
|
86
86
|
- lib/paystack.rb
|
87
87
|
- lib/paystack/error.rb
|
88
88
|
- lib/paystack/modules/api.rb
|
89
|
-
- lib/paystack/modules/crypto.rb
|
90
|
-
- lib/paystack/modules/tokenmanager.rb
|
91
89
|
- lib/paystack/objects/base.rb
|
92
90
|
- lib/paystack/objects/card.rb
|
93
91
|
- lib/paystack/objects/customers.rb
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
require 'paystack/modules/crypto.rb'
|
3
|
-
require 'paystack/modules/api.rb'
|
4
|
-
require 'paystack/objects/card.rb'
|
5
|
-
|
6
|
-
module TokenManager
|
7
|
-
|
8
|
-
CONCATENATOR = '*'
|
9
|
-
RSA_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIsL+RHqfkBiKGn/D1y1QnNrMkKzxWP\n2wkeSokw2OJrCI+d6YGJPrHHx+nmb/Qn885/R01Gw6d7M824qofmCvkCAwEAAQ==\n-----END PUBLIC KEY-----\n"
|
10
|
-
|
11
|
-
def TokenManager.create(card, publishableKey)
|
12
|
-
if(card == nil)
|
13
|
-
raise PayStackCardError, "Card cannot be null"
|
14
|
-
elsif !card.isValidCard()
|
15
|
-
raise PayStackCardError, "Invalid card details"
|
16
|
-
end
|
17
|
-
|
18
|
-
card_str = concatCardFields(card)
|
19
|
-
enc_card_str = Crypto.encrypt_string(card_str, RSA_PUBLIC_KEY)
|
20
|
-
return createServerToken enc_card_str, publishableKey
|
21
|
-
end
|
22
|
-
|
23
|
-
def TokenManager.concatCardFields(card)
|
24
|
-
raise PayStackCardError, 'Invalid Card' unless (!card.nil? && card.instance_of?(PaystackCard))
|
25
|
-
|
26
|
-
number = Utils.nullifyString(card.number)
|
27
|
-
cvc = card.cvc
|
28
|
-
expiryMonth = card.expiryMonth
|
29
|
-
expiryYear = card.expiryYear
|
30
|
-
|
31
|
-
raise PayStackCardError, 'Card number cannot be empty or null' unless (!number.nil?)
|
32
|
-
|
33
|
-
return "#{number}#{CONCATENATOR}#{cvc}#{CONCATENATOR}#{expiryMonth}#{CONCATENATOR}#{expiryYear}"
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def TokenManager.createServerToken(encrypted_card, publishableKey)
|
38
|
-
token = nil;
|
39
|
-
begin
|
40
|
-
response = RestClient.post "#{API::TOKEN_URL}", :clientdata => encrypted_card, :publishablekey => publishableKey
|
41
|
-
unless (response.code == 200 || response.code == 201)
|
42
|
-
raise PayStackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
43
|
-
end
|
44
|
-
result = JSON.parse(response.body)
|
45
|
-
unless(result['status'] != '0' )
|
46
|
-
raise PayStackServerError.new(response), "Server Message: #{result['message']}"
|
47
|
-
end
|
48
|
-
token = {:token => result['token'], :last4 => result['last4']}
|
49
|
-
rescue JSON::ParserError => jsonerr
|
50
|
-
raise PayStackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
51
|
-
|
52
|
-
rescue PayStackServerError => e
|
53
|
-
puts e.response.code
|
54
|
-
Utils.serverErrorHandler(e)
|
55
|
-
end
|
56
|
-
return token
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|