paystack 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -2
- data/.travis.yml +16 -0
- data/README.md +58 -22
- data/Rakefile +1 -2
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/lib/paystack.rb +55 -54
- data/lib/paystack/error.rb +11 -12
- data/lib/paystack/modules/api.rb +8 -7
- data/lib/paystack/objects/base.rb +103 -102
- data/lib/paystack/objects/card.rb +105 -105
- data/lib/paystack/objects/customers.rb +37 -37
- data/lib/paystack/objects/plans.rb +39 -41
- data/lib/paystack/objects/subscriptions.rb +40 -0
- data/lib/paystack/objects/transactions.rb +63 -63
- data/lib/paystack/utils/utils.rb +104 -105
- data/lib/paystack/version.rb +1 -1
- data/paystack.gemspec +7 -5
- metadata +11 -10
- data/key.pem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae8e6284abf3217caed4431540242dcc7ec79fac
|
4
|
+
data.tar.gz: ff9e3e54dcf80cd650cf1315ee307a9eac5a84d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa1c3e4e8ac03be75742784b883bad76cb2c9c570c59dfcf6f9371c9017688727cfc1468f8ca86bbe23bc08c9a03732d9b1ceeeb99fa8f2c579bf9b97aa0d73e
|
7
|
+
data.tar.gz: 231967b9e5724e016da55b3c1db6618d6184e9c8415c508963da8286eee2be83de8ac360f27ac1fa12b0946837ac0068482dc745c96fcd04a0462861df0d1088
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--color
|
2
|
-
--require spec_helper
|
1
|
+
--color
|
2
|
+
--require spec_helper
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
sudo: required
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.0.0
|
5
|
+
before_install: gem install bundler
|
6
|
+
cache:
|
7
|
+
directories:
|
8
|
+
- vendor/bundle
|
9
|
+
|
10
|
+
script:
|
11
|
+
- rspec
|
12
|
+
|
13
|
+
deploy:
|
14
|
+
provider: rubygems
|
15
|
+
api_key: "bcc5d3ff98af614e950b21a2df77af20"
|
16
|
+
gem: paystack
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# Paystack
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/IkoroVictor/paystack-ruby.svg?branch=master)](https://travis-ci.org/IkoroVictor/paystack-ruby) [![Gem Version](https://badge.fury.io/rb/paystack.svg)](https://badge.fury.io/rb/paystack)
|
3
4
|
|
4
|
-
A ruby gem for easy integration of [Paystack](https://paystack.co/).
|
5
|
+
A ruby gem for easy integration of [Paystack](https://paystack.co/).
|
5
6
|
|
6
7
|
|
7
8
|
## Installation
|
@@ -43,23 +44,6 @@ It throws a `PaystackBadKeyError` when either of the keys are invalid or cannot
|
|
43
44
|
|
44
45
|
|
45
46
|
|
46
|
-
### Instantiate a Card object
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
card = new PaystackCard(
|
50
|
-
:number => "304402040400400022",
|
51
|
-
:cvc => "888",
|
52
|
-
:expiryMonth => "07",
|
53
|
-
:expiryYear => "19"
|
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
47
|
### Initialize transaction and get Authorization URL
|
64
48
|
|
65
49
|
```ruby
|
@@ -70,7 +54,7 @@ All card values/fields should be String literals.
|
|
70
54
|
:amount => 300000,
|
71
55
|
:email => "xxxxxx@gmail.com",
|
72
56
|
)
|
73
|
-
auth_url =
|
57
|
+
auth_url = result['data']['authorization_url']
|
74
58
|
```
|
75
59
|
NOTE: Amount is in kobo i.e. `100000 = 100000 kobo = 1000 naira`
|
76
60
|
|
@@ -108,7 +92,7 @@ NOTE: Amount is in kobo i.e. `100000 = 100000 kobo = 1000 naira`
|
|
108
92
|
|
109
93
|
```ruby
|
110
94
|
|
111
|
-
transaction_id = "
|
95
|
+
transaction_id = "123456778"
|
112
96
|
transactions = PaystackTransactions.new(paystackObj)
|
113
97
|
result = transactions.get(transaction_id)
|
114
98
|
|
@@ -233,7 +217,7 @@ NOTE: Amount is in kobo i.e. `100000 = 100000 kobo = 1000 naira`
|
|
233
217
|
|
234
218
|
```ruby
|
235
219
|
|
236
|
-
plan_id = ""123456778"
|
220
|
+
plan_id = ""123456778"
|
237
221
|
plans = PaystackPlans.new(paystackObj)
|
238
222
|
result = plans.update(
|
239
223
|
plan_id,
|
@@ -245,8 +229,60 @@ NOTE: Amount is in kobo i.e. `100000 = 100000 kobo = 1000 naira`
|
|
245
229
|
```
|
246
230
|
|
247
231
|
|
232
|
+
## Subscriptions
|
233
|
+
|
234
|
+
### Create new subscription
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
|
238
|
+
subscriptions = PaystackSubscriptions.new(paystackObj)
|
239
|
+
result = subscriptions.create(
|
240
|
+
|
241
|
+
:customer => "customer@email.com",
|
242
|
+
:plan => "123557", #plan id
|
243
|
+
:amount => 30000 #in KOBO
|
244
|
+
)
|
245
|
+
|
246
|
+
```
|
247
|
+
|
248
|
+
### Get subscription detail
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
|
252
|
+
subscription_id = "123456778"
|
253
|
+
subscriptions = PaystackSubscriptions.new(paystackObj)
|
254
|
+
result = subscriptions.get(subscription_id)
|
255
|
+
subscription = result['data']
|
256
|
+
|
257
|
+
```
|
258
|
+
|
259
|
+
### Enable subscription
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
|
263
|
+
subscriptions = PaystackSubscriptions.new(paystackObj)
|
264
|
+
result = subscriptions.enable(
|
265
|
+
:code => "12328833",
|
266
|
+
:token => "EWFWKFJWE" #user email token
|
267
|
+
)
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
### Disable subscription
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
|
275
|
+
subscriptions = PaystackSubscriptions.new(paystackObj)
|
276
|
+
result = subscriptions.disable(
|
277
|
+
:code => "12328833",
|
278
|
+
:token => "EWFWKFJWE" #user email token
|
279
|
+
)
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
|
248
284
|
## Static methods
|
249
|
-
`PaystackTransactions`, `PaystackCustomers` and `
|
285
|
+
`PaystackTransactions`, `PaystackCustomers`, `PaystackPlans` and `PaystackSubscriptions` 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
286
|
|
251
287
|
|
252
288
|
```ruby
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
data/lib/paystack.rb
CHANGED
@@ -1,54 +1,55 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
require 'paystack/error.rb'
|
3
|
-
require 'paystack/modules/api.rb'
|
4
|
-
require 'paystack/utils/utils.rb'
|
5
|
-
require 'paystack/objects/card.rb'
|
6
|
-
require 'paystack/objects/customers.rb'
|
7
|
-
require 'paystack/objects/plans.rb'
|
8
|
-
require 'paystack/objects/
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
1
|
+
require 'rest-client'
|
2
|
+
require 'paystack/error.rb'
|
3
|
+
require 'paystack/modules/api.rb'
|
4
|
+
require 'paystack/utils/utils.rb'
|
5
|
+
require 'paystack/objects/card.rb'
|
6
|
+
require 'paystack/objects/customers.rb'
|
7
|
+
require 'paystack/objects/plans.rb'
|
8
|
+
require 'paystack/objects/subscriptions.rb'
|
9
|
+
require 'paystack/objects/transactions.rb'
|
10
|
+
|
11
|
+
|
12
|
+
class Paystack
|
13
|
+
attr_reader :public_key, :private_key
|
14
|
+
|
15
|
+
def initialize paystack_public_key=nil, paystack_private_key=nil
|
16
|
+
if (paystack_public_key.nil?)
|
17
|
+
@public_key = ENV['PAYSTACK_PUBLIC_KEY']
|
18
|
+
else
|
19
|
+
@public_key = paystack_public_key
|
20
|
+
end
|
21
|
+
|
22
|
+
if (paystack_private_key.nil?)
|
23
|
+
@private_key = ENV['PAYSTACK_PRIVATE_KEY']
|
24
|
+
else
|
25
|
+
@private_key = paystack_private_key
|
26
|
+
end
|
27
|
+
|
28
|
+
unless !@public_key.nil?
|
29
|
+
raise PaystackBadKeyError, "No public key supplied and couldn't find any in environment variables. Make sure to set public key as an environment variable PAYSTACK_PUBLIC_KEY"
|
30
|
+
end
|
31
|
+
unless @public_key[0..2] == 'pk_'
|
32
|
+
raise PaystackBadKeyError, "Invalid public key #{@public_key}"
|
33
|
+
end
|
34
|
+
|
35
|
+
unless !@private_key.nil?
|
36
|
+
raise PaystackBadKeyError, "No private key supplied and couldn't find any in environment variables. Make sure to set private key as an environment variable PAYSTACK_PRIVATE_KEY"
|
37
|
+
end
|
38
|
+
unless @private_key[0..2] == 'sk_'
|
39
|
+
raise PaystackBadKeyError, "Invalid private key #{@private_key}"
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
#TODO delete if not used
|
46
|
+
def setPublicKey public_key
|
47
|
+
@public_key = public_key
|
48
|
+
end
|
49
|
+
|
50
|
+
def setPrivateKey public_key
|
51
|
+
@public_key = public_key
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
data/lib/paystack/error.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
class PaystackBadKeyError < StandardError
|
1
|
+
class PaystackServerError < StandardError
|
2
|
+
attr_reader :response
|
3
|
+
def initialize(response=nil)
|
4
|
+
@response = response
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class PaystackCardError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class PaystackBadKeyError < StandardError
|
13
12
|
end
|
data/lib/paystack/modules/api.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
module API
|
2
|
-
BASE_URL = "https://api.paystack.co"
|
3
|
-
TOKEN_URL ='https://standard.paystack.co/bosco/createmobiletoken'
|
4
|
-
TRANSACTION_PATH = "/transaction"
|
5
|
-
PLAN_PATH = "/plan"
|
6
|
-
CUSTOMER_PATH = "/customer"
|
7
|
-
|
1
|
+
module API
|
2
|
+
BASE_URL = "https://api.paystack.co"
|
3
|
+
TOKEN_URL ='https://standard.paystack.co/bosco/createmobiletoken'
|
4
|
+
TRANSACTION_PATH = "/transaction"
|
5
|
+
PLAN_PATH = "/plan"
|
6
|
+
CUSTOMER_PATH = "/customer"
|
7
|
+
SUBSCRIPTION_PATH = "/subscription"
|
8
|
+
end
|
@@ -1,102 +1,103 @@
|
|
1
|
-
class PaystackBaseObject
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
end
|
1
|
+
class PaystackBaseObject
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
attr_reader :paystack
|
5
|
+
|
6
|
+
def initialize(paystackObj)
|
7
|
+
unless !paystackObj.nil?
|
8
|
+
raise ArgumentError, "Paystack object cannot be nil!!"
|
9
|
+
end
|
10
|
+
@paystack = paystackObj
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
# =>Static methods
|
15
|
+
def self.initGetRequest(paystackObj, url)
|
16
|
+
result = nil
|
17
|
+
begin
|
18
|
+
response = RestClient.get "#{API::BASE_URL}#{url}" , :Authorization => "Bearer #{paystackObj.private_key}", :content_type => :json, :accept => :json
|
19
|
+
unless (response.code == 200 || response.code == 201)
|
20
|
+
raise PaystackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
21
|
+
end
|
22
|
+
result = JSON.parse(response.body)
|
23
|
+
unless(result['status'] != 0 )
|
24
|
+
raise PaystackServerError.new(response), "Server Message: #{result['message']}"
|
25
|
+
end
|
26
|
+
|
27
|
+
rescue JSON::ParserError => jsonerr
|
28
|
+
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
29
|
+
|
30
|
+
rescue PaystackServerError => e
|
31
|
+
Utils.serverErrorHandler(e)
|
32
|
+
end
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.initPostRequest(paystackObj, url, data = {}, json=false )
|
37
|
+
result = nil
|
38
|
+
begin
|
39
|
+
if !json
|
40
|
+
response = RestClient.post "#{API::BASE_URL}#{url}" , data, :authorization => "Bearer #{paystackObj.private_key}"
|
41
|
+
else
|
42
|
+
response = RestClient.post "#{API::BASE_URL}#{url}" , data.to_json, :authorization => "Bearer #{paystackObj.private_key}", :content_type => :json, :accept => :json
|
43
|
+
|
44
|
+
end
|
45
|
+
unless (response.code == 200 || response.code == 201)
|
46
|
+
raise PaystackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
47
|
+
end
|
48
|
+
result = JSON.parse(response.body)
|
49
|
+
unless(result['status'] != 0 )
|
50
|
+
raise PaystackServerError.new(response), "Server Message: #{result['message']}"
|
51
|
+
end
|
52
|
+
|
53
|
+
rescue JSON::ParserError => jsonerr
|
54
|
+
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
55
|
+
|
56
|
+
rescue PaystackServerError => e
|
57
|
+
Utils.serverErrorHandler(e)
|
58
|
+
end
|
59
|
+
return result
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.initPutRequest(paystackObj, url, data = {} )
|
63
|
+
result = nil
|
64
|
+
begin
|
65
|
+
response = RestClient.put "#{API::BASE_URL}#{url}" , data, :Authorization => "Bearer #{paystackObj.private_key}"
|
66
|
+
unless (response.code == 200 || response.code == 201)
|
67
|
+
raise PaystackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
68
|
+
end
|
69
|
+
result = JSON.parse(response.body)
|
70
|
+
unless(result['status'] != 0 )
|
71
|
+
raise PaystackServerError.new(response), "Server Message: #{result['message']}"
|
72
|
+
end
|
73
|
+
|
74
|
+
rescue JSON::ParserError => jsonerr
|
75
|
+
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
76
|
+
|
77
|
+
rescue PaystackServerError => e
|
78
|
+
Utils.serverErrorHandler(e)
|
79
|
+
end
|
80
|
+
return result
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.initDeleteRequest(paystackObj, url)
|
84
|
+
result = nil
|
85
|
+
begin
|
86
|
+
response = RestClient.delete "#{API::BASE_URL}#{url}" , :Authorization => "Bearer #{paystackObj.private_key}"
|
87
|
+
unless (response.code == 200 || response.code == 201)
|
88
|
+
raise PaystackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
|
89
|
+
end
|
90
|
+
result = JSON.parse(response.body)
|
91
|
+
unless(result['status'] != 0 )
|
92
|
+
raise PaystackServerError.new(response), "Server Message: #{result['message']}"
|
93
|
+
end
|
94
|
+
|
95
|
+
rescue JSON::ParserError => jsonerr
|
96
|
+
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
|
97
|
+
|
98
|
+
rescue PaystackServerError => e
|
99
|
+
Utils.serverErrorHandler(e)
|
100
|
+
end
|
101
|
+
return result
|
102
|
+
end
|
103
|
+
end
|