versapay 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +51 -0
- data/lib/versapay.rb +8 -1
- data/lib/versapay/transactions.rb +1 -1
- data/lib/versapay/version.rb +1 -1
- data/test/transactions_test.rb +67 -0
- metadata +8 -9
- data/test/transactions.test.rb +0 -49
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# A gem to help with using VersaPay's API
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
Include it in your app's 'Gemfile':
|
6
|
+
|
7
|
+
```Ruby
|
8
|
+
gem 'versapay'
|
9
|
+
```
|
10
|
+
|
11
|
+
In the controller you can make sure that all the actions are called with a valid Web Hook signature:
|
12
|
+
|
13
|
+
```Ruby
|
14
|
+
class VersapayController < ApplicationController
|
15
|
+
check_versapay_signatures "MyKey"
|
16
|
+
```
|
17
|
+
|
18
|
+
There are some Rails view helpers to set up a debit agreement:
|
19
|
+
|
20
|
+
```Ruby
|
21
|
+
# The first you hear of a new debit agreement is when VersaPay sends a callback. So the reference should be
|
22
|
+
# something you can use to identify your user. For example,
|
23
|
+
# key = MyApp::Application.config.secret_token
|
24
|
+
# verifier = ActiveSupport::MessageVerifier.new(key)
|
25
|
+
# @reference = verifier.generate(@user.id)
|
26
|
+
= link_to "Setup VersaPay", debit_agreement_link("Set up an agreement", { :reference => @reference, :pref => "ba" })
|
27
|
+
```
|
28
|
+
|
29
|
+
And of course, you can sent transactions:
|
30
|
+
|
31
|
+
```Ruby
|
32
|
+
Versapay::Transactions.new do |v|
|
33
|
+
begin
|
34
|
+
result = v.create(:amount_in_cents => 12345,
|
35
|
+
:debit_agreement_token => "RWOCDPSM",
|
36
|
+
:transaction_type => "pre_authorized_debit",
|
37
|
+
:email => "foo@example.com",
|
38
|
+
:message => "A crate of widgets",
|
39
|
+
:transaction_reference => "my reference",
|
40
|
+
:fund_token => "VersaPayBalanceFundingSource"
|
41
|
+
)
|
42
|
+
rescue Versapay::VersapayError => e
|
43
|
+
logger.error "Transaction failed"
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Pretty much all the parameters to the functions match the way the VersaPay developers API works.
|
50
|
+
|
51
|
+
See the VersaPay developers API: http://developers.versapay.com
|
data/lib/versapay.rb
CHANGED
@@ -11,6 +11,8 @@ module Versapay
|
|
11
11
|
class VersapayError < RuntimeError; end
|
12
12
|
|
13
13
|
class InvalidInput < VersapayError; end
|
14
|
+
class DuplicateTransaction < InvalidInput; end
|
15
|
+
|
14
16
|
class InvalidWebhookSignature < VersapayError; end
|
15
17
|
class Unprocessable < VersapayError; end
|
16
18
|
class NotFound < VersapayError; end
|
@@ -47,7 +49,12 @@ module Versapay
|
|
47
49
|
when 200,201
|
48
50
|
return JSON.parse(response)
|
49
51
|
when 412
|
50
|
-
|
52
|
+
result = JSON.parse(response)
|
53
|
+
if result.key? "unique_reference"
|
54
|
+
raise Versapay::DuplicateTransaction, response
|
55
|
+
else
|
56
|
+
raise Versapay::InvalidInput, response
|
57
|
+
end
|
51
58
|
when 422
|
52
59
|
raise Versapay::Unprocessable, response
|
53
60
|
when 500
|
data/lib/versapay/version.rb
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TransactionsTest < ActionView::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
Versapay.key = "mykey"
|
7
|
+
Versapay.token = "mytoken"
|
8
|
+
@v = Versapay::Transactions.new
|
9
|
+
FakeWeb.allow_net_connect = false
|
10
|
+
end
|
11
|
+
|
12
|
+
test "submitting a duplicate transaction" do
|
13
|
+
body = '{"unique_reference":"Already created transaction for supplied unique reference"}'
|
14
|
+
FakeWeb.register_uri(:post, "https://mytoken:mykey@demo.versapay.com/api/transactions.json", :body => body, :status => ["412", "Invalid input"])
|
15
|
+
assert_raises Versapay::DuplicateTransaction do
|
16
|
+
@v.create(:amount_in_cents => 12345,
|
17
|
+
:transaction_type => "direct_credit",
|
18
|
+
:message => "Have some cash!",
|
19
|
+
:transaction_reference => "Hello",
|
20
|
+
:unique_reference => 42,
|
21
|
+
:first_name => "Joe",
|
22
|
+
:last_name => "Blow",
|
23
|
+
:institution_number => "004",
|
24
|
+
:branch_number => "96610",
|
25
|
+
:account_number => "12345",
|
26
|
+
:fund_token => "123"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
test "viewing a transaction's details" do
|
33
|
+
body = '{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"Mitch G.","unique_reference":null,"transaction_reference":"myreference","state":"completed","token":"83F2W2K2FJFV","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"11111","amount_in_cents":128105}'
|
34
|
+
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/transactions/83F2W2K2FJFV.json", :body => body)
|
35
|
+
results = @v.view("83F2W2K2FJFV")
|
36
|
+
|
37
|
+
assert_equal Hash, results.class
|
38
|
+
assert_equal "transaction", results["type"]
|
39
|
+
assert_equal "VersaPay Balance", results["from_fund"]
|
40
|
+
end
|
41
|
+
|
42
|
+
test "viewing an invalid transaction's details" do
|
43
|
+
body = "Not found"
|
44
|
+
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/transactions/83F2W2K2FXXX.json", :body => body, :status => ["500", "Not found"])
|
45
|
+
assert_raises Versapay::NotFound do
|
46
|
+
@v.view("83F2W2K2FXXX")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
test "listing transactions when there are none" do
|
51
|
+
body = "[]"
|
52
|
+
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/transactions.json", :body => body)
|
53
|
+
results = @v.list
|
54
|
+
assert_equal Array, results.class
|
55
|
+
assert_equal 0, results.size
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
test "listing sent transactions when there are some" do
|
60
|
+
body = '[{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"Sean Walberg","unique_reference":"2","transaction_reference":"BAhpBw==--31ff18bce30e92ef2d9d4753a2c3c748e1fb8544","state":"completed","token":"8UB8QUMRJA8M","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":100},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"Leia Organa","unique_reference":null,"transaction_reference":"BAhpAdA=--193ef4be953db212508b80802511ef21319c293b","state":"completed","token":"3FJRHSUKP4N2","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":15645},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"J.T. Hutt","unique_reference":null,"transaction_reference":"BAhpAc8=--75cb17f2a52ab3518123ed091343d67104c0a929","state":"completed","token":"2RUPSUBFLH3G","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":131984},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"Leia Organa","unique_reference":null,"transaction_reference":"BAhpAc0=--ab5d0a1b7976cd482f99dc708bee45ecc546d267","state":"completed","token":"5BV48XZ4CEHC","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":86087},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"J.T. Hutt","unique_reference":null,"transaction_reference":"BAhpAcw=--0b2dabdaf965b6edbd4cf91ff2bb41724faa4822","state":"completed","token":"9TUHM13V1K4K","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":131895},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"Leia Organa","unique_reference":null,"transaction_reference":"BAhpAco=--07bb3b7ae35affc7b6210daed6a0747318e4bfd3","state":"completed","token":"21WREVF394XH","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":45492},{"from_account":"wavepayrolltest","type":"transaction","transaction_type":"direct_credit","to_account":"J.T. Hutt","unique_reference":null,"transaction_reference":"BAhpAck=--bc8994974fc1ca3295c455800855e6df7cfb8504","state":"completed","token":"3AMV3SIL5UKF","link_url":null,"message":null,"from_fund":"VersaPay Balance","email":null,"created_by_user":"1kkOYn_uA6KWNjejFka1","amount_in_cents":131808}]'
|
61
|
+
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/transactions.json", :body => body)
|
62
|
+
results = @v.list
|
63
|
+
assert_equal Array, results.class
|
64
|
+
assert_equal 7, results.size
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: versapay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sean Walberg
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
19
|
-
default_executable:
|
18
|
+
date: 2012-03-05 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: rails
|
@@ -119,6 +118,7 @@ files:
|
|
119
118
|
- .gitignore
|
120
119
|
- .rvmrc
|
121
120
|
- Gemfile
|
121
|
+
- README.md
|
122
122
|
- Rakefile
|
123
123
|
- lib/versapay.rb
|
124
124
|
- lib/versapay/debit_agreement.rb
|
@@ -131,10 +131,9 @@ files:
|
|
131
131
|
- test/fund_sources_test.rb
|
132
132
|
- test/helper_test.rb
|
133
133
|
- test/test_helper.rb
|
134
|
-
- test/
|
134
|
+
- test/transactions_test.rb
|
135
135
|
- test/webhook_test.rb
|
136
136
|
- versapay.gemspec
|
137
|
-
has_rdoc: true
|
138
137
|
homepage: ""
|
139
138
|
licenses: []
|
140
139
|
|
@@ -164,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
163
|
requirements: []
|
165
164
|
|
166
165
|
rubyforge_project: versapay
|
167
|
-
rubygems_version: 1.
|
166
|
+
rubygems_version: 1.8.15
|
168
167
|
signing_key:
|
169
168
|
specification_version: 3
|
170
169
|
summary: A gem to use the Versapay API
|
@@ -173,5 +172,5 @@ test_files:
|
|
173
172
|
- test/fund_sources_test.rb
|
174
173
|
- test/helper_test.rb
|
175
174
|
- test/test_helper.rb
|
176
|
-
- test/
|
175
|
+
- test/transactions_test.rb
|
177
176
|
- test/webhook_test.rb
|
data/test/transactions.test.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TransactionsTest < ActionView::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
Versapay.key = "mykey"
|
7
|
-
Versapay.token = "mytoken"
|
8
|
-
@v = Versapay::DebitAgreement.new
|
9
|
-
FakeWeb.register_uri(:any, "https://demo.versapay.com", :body => "response for any HTTP method", :status => ["500", "bad test"])
|
10
|
-
end
|
11
|
-
|
12
|
-
test "viewing a transaction's details" do
|
13
|
-
body = "{\"type\":\"debit_agreement\",\"state\":\"approved\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"83F2W2K2FJFV\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":\"BAhpBg==--7e0a441bda1b04f5b985fa654d357071bb9b32d1\",\"email\":\"jimbob@smallpayroll.ca\",\"name\":\"Jim Bob\"}"
|
14
|
-
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/debit_agreements/83F2W2K2FJFV.json", :body => body)
|
15
|
-
results = @v.view("83F2W2K2FJFV")
|
16
|
-
|
17
|
-
assert_equal Hash, results.class
|
18
|
-
assert_equal "debit_agreement", results["type"]
|
19
|
-
end
|
20
|
-
|
21
|
-
test "viewing an invalid transaction's details" do
|
22
|
-
body = "Not found"
|
23
|
-
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/debit_agreements/83F2W2K2FXXX.json", :body => body, :status => ["500", "Not found"])
|
24
|
-
assert_raises Versapay::NotFound do
|
25
|
-
@v.view("83F2W2K2FXXX")
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
test "listing sent transactions when there are none" do
|
30
|
-
body = "[]"
|
31
|
-
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/debit_agreements/sent.json", :body => body)
|
32
|
-
results = @v.list_sent
|
33
|
-
assert_equal Array, results.class
|
34
|
-
assert_equal 0, results.size
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
test "listing sent transactions when there are some" do
|
39
|
-
body = "[{\"type\":\"debit_agreement\",\"state\":\"approved\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"83F2W2K2FJFV\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":\"BAhpBg==--7e0a441bda1b04f5b985fa654d357071bb9b32d1\",\"email\":\"jimbob@smallpayroll.ca\",\"name\":\"Jim Bob\"},{\"type\":\"debit_agreement\",\"state\":\"approved\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"6DCKWXXF1A51\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":\"BAhpBg==--7e0a441bda1b04f5b985fa654d357071bb9b32d1\",\"email\":\"jimbob@smallpayroll.ca\",\"name\":\"Jim Bob\"},{\"type\":\"debit_agreement\",\"state\":\"revoked\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"7VBJRS7TKLIL\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":\"BAhpBg==--7e0a441bda1b04f5b985fa654d357071bb9b32d1\",\"email\":\"fredsmith@smallpayroll.ca\",\"name\":\"Fred Smith\"},{\"type\":\"debit_agreement\",\"state\":\"revoked\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"6YI3MTVMY918\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":null,\"email\":\"fredsmith@smallpayroll.ca\",\"name\":\"Fred Smith\"},{\"type\":\"debit_agreement\",\"state\":\"revoked\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"1AYAG5T762TJ\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":null,\"email\":\"fredsmith@smallpayroll.ca\",\"name\":\"Fred Smith\"},{\"type\":\"debit_agreement\",\"state\":\"cancelled\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"7IN4ALWCXKJG\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":null,\"email\":\"fredsmith@smallpayroll.ca\",\"name\":\"Fred Smith\"},{\"type\":\"debit_agreement\",\"state\":\"revoked\",\"created_by_account\":\"wavepayrolltest\",\"token\":\"4RQ58W5B4M1W\",\"message\":\"blah\",\"created_by_user\":\"1kkOYn_uA6KWNjejFka1\",\"reference\":null,\"email\":\"fredsmith@smallpayroll.ca\",\"name\":\"Fred Smith\"}]"
|
40
|
-
FakeWeb.register_uri(:get, "https://mytoken:mykey@demo.versapay.com/api/debit_agreements/sent.json", :body => body)
|
41
|
-
results = @v.list_sent
|
42
|
-
assert_equal Array, results.class
|
43
|
-
assert_equal 7, results.size
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
########
|
49
|
-
# When testing a transaction involving a post, ensure the data is in json and the content-type is application/json
|