balanced 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/examples/bank_account_debits.rb +56 -0
- data/lib/balanced/error.rb +10 -0
- data/lib/balanced/pager.rb +6 -0
- data/lib/balanced/resources/bank_account.rb +24 -1
- data/lib/balanced/response/balanced_exception_middleware.rb +14 -1
- data/lib/balanced/version.rb +1 -1
- data/spec/balanced/resources/bank_account_spec.rb +98 -32
- metadata +3 -2
data/README.md
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
"""
|
2
|
+
Learn how to authenticate a bank account so you can debit with it.
|
3
|
+
"""
|
4
|
+
cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
|
5
|
+
$:.unshift(cwd + "/lib")
|
6
|
+
require 'balanced'
|
7
|
+
|
8
|
+
host = ENV.fetch('BALANCED_HOST') { nil }
|
9
|
+
options = {}
|
10
|
+
if host
|
11
|
+
options[:scheme] = 'http'
|
12
|
+
options[:host] = host
|
13
|
+
options[:port] = 5000
|
14
|
+
end
|
15
|
+
|
16
|
+
Balanced.configure(nil, options)
|
17
|
+
|
18
|
+
# create a new marketplace
|
19
|
+
api_key = Balanced::ApiKey.new.save
|
20
|
+
Balanced.configure(api_key.secret)
|
21
|
+
marketplace = Balanced::Marketplace.new.save
|
22
|
+
|
23
|
+
# create a bank account
|
24
|
+
bank_account = marketplace.create_bank_account(
|
25
|
+
:account_number => "1234567890",
|
26
|
+
:bank_code => "12",
|
27
|
+
:name => "Jack Q Merchant"
|
28
|
+
)
|
29
|
+
account = marketplace.create_account()
|
30
|
+
account.add_bank_account(bank_account.uri)
|
31
|
+
|
32
|
+
puts "you can't debit until you authenticate"
|
33
|
+
begin
|
34
|
+
account.debit(:amount => 100)
|
35
|
+
rescue Balanced::Error => ex
|
36
|
+
puts "Debit failed, %s" % ex.message
|
37
|
+
end
|
38
|
+
|
39
|
+
# authenticate
|
40
|
+
verification = bank_account.verify
|
41
|
+
|
42
|
+
begin
|
43
|
+
verification.confirm(1,2)
|
44
|
+
rescue Balanced::BankAccountVerificationFailure => ex
|
45
|
+
puts 'Authentication error , %s' % ex.message
|
46
|
+
puts "PROTIP: for TEST bank accounts the valid amount is always 1 and 1"
|
47
|
+
end
|
48
|
+
verification = bank_account.verifications.find(:first).first
|
49
|
+
|
50
|
+
raise "unpossible" if verification.confirm(1, 1).state != 'verified'
|
51
|
+
debit = account.debit(:amount => 100)
|
52
|
+
puts "debited the bank account %s for %d cents" % [
|
53
|
+
debit.source.uri,
|
54
|
+
debit.amount
|
55
|
+
]
|
56
|
+
puts "and there you have it"
|
data/lib/balanced/error.rb
CHANGED
@@ -97,4 +97,14 @@ module Balanced
|
|
97
97
|
|
98
98
|
# Raised when Balanced returns a 504 HTTP status code
|
99
99
|
class GatewayTimeout < Error; end
|
100
|
+
|
101
|
+
|
102
|
+
# custom mapped exceptions
|
103
|
+
class FundingInstrumentVerificationError < Error; end
|
104
|
+
|
105
|
+
# Raised when Balanced has an issue with you verifying a bank account
|
106
|
+
class BankAccountVerificationFailure < FundingInstrumentVerificationError
|
107
|
+
|
108
|
+
end
|
109
|
+
|
100
110
|
end
|
data/lib/balanced/pager.rb
CHANGED
@@ -8,6 +8,12 @@ module Balanced
|
|
8
8
|
include Enumerable
|
9
9
|
|
10
10
|
# A pager for paginating through resource records.
|
11
|
+
#
|
12
|
+
# @param [String] uri the uri of the resource
|
13
|
+
# @param [Hash] options
|
14
|
+
# @option options [Integer] limit
|
15
|
+
# @option options [Integer] offset
|
16
|
+
# @option options [Integer] per an alias for the :limit option
|
11
17
|
def initialize uri, options = {}
|
12
18
|
@uri = uri
|
13
19
|
@options = options
|
@@ -40,7 +40,13 @@ module Balanced
|
|
40
40
|
meta = args[2] || options.fetch(:meta) { nil }
|
41
41
|
description = args[3] || options.fetch(:description) { nil }
|
42
42
|
|
43
|
-
self.account.debit(
|
43
|
+
self.account.debit(
|
44
|
+
:amount => amount,
|
45
|
+
:appears_on_statement_as => appears_on_statement_as,
|
46
|
+
:meta => meta,
|
47
|
+
:description => description,
|
48
|
+
:source_uri => self.uri
|
49
|
+
)
|
44
50
|
end
|
45
51
|
|
46
52
|
# Creates a Credit of funds from your Marketplace's escrow account to this
|
@@ -82,5 +88,22 @@ module Balanced
|
|
82
88
|
save
|
83
89
|
end
|
84
90
|
|
91
|
+
def verify
|
92
|
+
Verification.new(
|
93
|
+
'uri' => self.verifications_uri
|
94
|
+
).save
|
95
|
+
end
|
96
|
+
|
85
97
|
end
|
98
|
+
|
99
|
+
class Verification
|
100
|
+
include Balanced::Resource
|
101
|
+
|
102
|
+
def confirm(amount_1, amount_2)
|
103
|
+
self.amount_1 = amount_1
|
104
|
+
self.amount_2 = amount_2
|
105
|
+
save
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
86
109
|
end
|
@@ -22,9 +22,22 @@ module Faraday
|
|
22
22
|
503 => Balanced::ServiceUnavailable,
|
23
23
|
504 => Balanced::GatewayTimeout,
|
24
24
|
}
|
25
|
+
CATEGORY_CODE_MAP = {
|
26
|
+
'bank-account-authentication-not-pending' =>
|
27
|
+
Balanced::BankAccountVerificationFailure,
|
28
|
+
'bank-account-authentication-failed' =>
|
29
|
+
Balanced::BankAccountVerificationFailure,
|
30
|
+
'bank-account-authentication-already-exists' =>
|
31
|
+
Balanced::BankAccountVerificationFailure,
|
32
|
+
}
|
25
33
|
def on_complete(response)
|
26
34
|
status_code = response[:status].to_i
|
27
|
-
|
35
|
+
if response.key? :body and response[:body] != nil
|
36
|
+
category_code = response[:body]['category_code']
|
37
|
+
else
|
38
|
+
category_code = nil
|
39
|
+
end
|
40
|
+
error_class = CATEGORY_CODE_MAP[category_code] || HTTP_STATUS_CODES[status_code]
|
28
41
|
raise error_class.new(response) if error_class
|
29
42
|
end
|
30
43
|
|
data/lib/balanced/version.rb
CHANGED
@@ -9,9 +9,9 @@ describe Balanced::BankAccount do
|
|
9
9
|
|
10
10
|
@marketplace = Balanced::Marketplace.new.save
|
11
11
|
card = @marketplace.create_card(
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
:card_number => "5105105105105100",
|
13
|
+
:expiration_month => "12",
|
14
|
+
:expiration_year => "2015"
|
15
15
|
)
|
16
16
|
# An initial balance for the marketplace
|
17
17
|
@buyer = @marketplace.create_buyer(
|
@@ -21,9 +21,9 @@ describe Balanced::BankAccount do
|
|
21
21
|
@buyer.debit :amount => 10000000
|
22
22
|
|
23
23
|
@incomplete_bank_account_hash = {
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
:account_number => "0987654321",
|
25
|
+
:bank_code => "321174851",
|
26
|
+
:name => "Timmy T. McTimmerson"
|
27
27
|
}
|
28
28
|
end
|
29
29
|
|
@@ -32,9 +32,9 @@ describe Balanced::BankAccount do
|
|
32
32
|
|
33
33
|
it 'should not create without a type field' do
|
34
34
|
lambda { Balanced::BankAccount.new(
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
:account_number => "0987654321",
|
36
|
+
:bank_code => "321174851",
|
37
|
+
:name => "Timmy T. McTimmerson"
|
38
38
|
).save }.should raise_error(Balanced::BadRequest)
|
39
39
|
end
|
40
40
|
end
|
@@ -44,10 +44,10 @@ describe Balanced::BankAccount do
|
|
44
44
|
|
45
45
|
before do
|
46
46
|
@bank_account = @marketplace.create_bank_account(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
:account_number => "0987654321",
|
48
|
+
:bank_code => "321174851",
|
49
|
+
:name => "Timmy T. McTimmerson",
|
50
|
+
:type => "checking"
|
51
51
|
)
|
52
52
|
end
|
53
53
|
|
@@ -102,48 +102,114 @@ describe Balanced::BankAccount do
|
|
102
102
|
|
103
103
|
before do
|
104
104
|
@credit = @bank_account.credit(
|
105
|
-
|
106
|
-
|
105
|
+
:amount => 50,
|
106
|
+
:description => 'Blah'
|
107
107
|
)
|
108
108
|
end
|
109
109
|
|
110
110
|
describe 'bank_account' do
|
111
111
|
subject { @credit.bank_account }
|
112
|
-
its(:account_number) {should end_with '4321'}
|
113
|
-
its(:routing_number) {should eql '321174851'}
|
112
|
+
its(:account_number) { should end_with '4321' }
|
113
|
+
its(:routing_number) { should eql '321174851' }
|
114
114
|
|
115
115
|
end
|
116
116
|
|
117
|
-
describe 'without an account' do
|
118
|
-
subject { @credit }
|
119
|
-
it { should respond_to :account }
|
120
|
-
its(:account) { should be_nil }
|
121
|
-
end
|
122
|
-
|
123
117
|
describe 'with an account' do
|
124
118
|
use_vcr_cassette
|
125
119
|
|
126
120
|
before do
|
127
121
|
@account = @marketplace.create_account
|
128
122
|
bank_account = @marketplace.create_bank_account(
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
123
|
+
:account_number => "1234567890111",
|
124
|
+
:bank_code => "021000021",
|
125
|
+
:name => "Timmy T. McTimmerson",
|
126
|
+
:type => "checking"
|
133
127
|
)
|
134
128
|
@account.add_bank_account(bank_account.uri)
|
135
129
|
bank_account = bank_account.reload
|
136
130
|
@credit_with_account = bank_account.credit(
|
137
|
-
|
138
|
-
|
131
|
+
:amount => 500,
|
132
|
+
:description => 'Blahblahblah'
|
139
133
|
)
|
140
134
|
end
|
141
135
|
|
142
|
-
|
143
|
-
|
144
|
-
|
136
|
+
subject { @credit_with_account }
|
137
|
+
it { should respond_to :account }
|
138
|
+
it { should be_instance_of Balanced::Credit }
|
145
139
|
end
|
146
140
|
|
147
141
|
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
describe 'verification' do
|
146
|
+
|
147
|
+
describe 'cannot debit when unverified' do
|
148
|
+
use_vcr_cassette
|
149
|
+
|
150
|
+
before do
|
151
|
+
@bank_account = @marketplace.create_bank_account(
|
152
|
+
:account_number => "0987654321",
|
153
|
+
:bank_code => "321174851",
|
154
|
+
:name => "Timmy T. McTimmerson",
|
155
|
+
:type => "checking"
|
156
|
+
)
|
157
|
+
@account = @marketplace.create_account
|
158
|
+
@account.add_bank_account(@bank_account.uri)
|
159
|
+
end
|
160
|
+
|
161
|
+
it do
|
162
|
+
lambda {
|
163
|
+
@account.debit(:amount => 100)
|
164
|
+
}.should raise_error(Balanced::Conflict)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe 'debits when verified' do
|
169
|
+
use_vcr_cassette
|
170
|
+
|
171
|
+
before do
|
172
|
+
@bank_account = @marketplace.create_bank_account(
|
173
|
+
:account_number => "0987654321",
|
174
|
+
:bank_code => "321174851",
|
175
|
+
:name => "Timmy T. McTimmerson",
|
176
|
+
:type => "checking"
|
177
|
+
)
|
178
|
+
@account = @marketplace.create_account
|
179
|
+
@account.add_bank_account(@bank_account.uri)
|
180
|
+
end
|
181
|
+
|
182
|
+
it do
|
183
|
+
|
184
|
+
authentication = @bank_account.verify
|
185
|
+
authentication.confirm(1, 1)
|
186
|
+
|
187
|
+
@account.debit(:amount => 100)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe 'errors when incorrectly verified' do
|
192
|
+
use_vcr_cassette
|
193
|
+
|
194
|
+
before do
|
195
|
+
@bank_account = @marketplace.create_bank_account(
|
196
|
+
:account_number => "0987654321",
|
197
|
+
:bank_code => "321174851",
|
198
|
+
:name => "Timmy T. McTimmerson",
|
199
|
+
:type => "checking"
|
200
|
+
)
|
201
|
+
@account = @marketplace.create_account
|
202
|
+
@account.add_bank_account(@bank_account.uri)
|
203
|
+
end
|
204
|
+
|
205
|
+
it do
|
206
|
+
|
207
|
+
authentication = @bank_account.verify
|
208
|
+
|
209
|
+
lambda {
|
210
|
+
authentication.confirm(1, 2)
|
211
|
+
}.should raise_error(Balanced::BankAccountVerificationFailure)
|
212
|
+
end
|
213
|
+
end
|
148
214
|
end
|
149
215
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: balanced
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- doc/balanced_templates/default/onefile/html/layout.erb
|
75
75
|
- doc/balanced_templates/default/onefile/html/setup.rb
|
76
76
|
- doc/balanced_templates/default/tags/html/index.erb
|
77
|
+
- examples/bank_account_debits.rb
|
77
78
|
- examples/events_and_callbacks.rb
|
78
79
|
- examples/examples.rb
|
79
80
|
- lib/balanced.rb
|