balanced 0.3.4 → 0.3.5
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.
- data/examples/examples.rb +39 -22
- data/lib/balanced.rb +5 -0
- data/lib/balanced/pager.rb +152 -0
- data/lib/balanced/resources/account.rb +25 -21
- data/lib/balanced/resources/api_key.rb +1 -1
- data/lib/balanced/resources/bank_account.rb +21 -4
- data/lib/balanced/resources/card.rb +20 -3
- data/lib/balanced/resources/credit.rb +1 -1
- data/lib/balanced/resources/debit.rb +6 -2
- data/lib/balanced/resources/hold.rb +8 -2
- data/lib/balanced/resources/marketplace.rb +16 -7
- data/lib/balanced/resources/merchant.rb +1 -5
- data/lib/balanced/resources/refund.rb +1 -1
- data/lib/balanced/resources/resource.rb +43 -9
- data/lib/balanced/resources/transaction.rb +1 -1
- data/lib/balanced/utils.rb +7 -0
- data/lib/balanced/version.rb +1 -1
- data/spec/balanced/resources/account_spec.rb +118 -6
- data/spec/spec_helper.rb +1 -0
- metadata +3 -2
data/examples/examples.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
$:.unshift("/Users/mahmoud/code/poundpay/ruby/balanced-ruby/lib")
|
2
2
|
require 'balanced'
|
3
|
+
|
3
4
|
begin
|
4
5
|
Balanced::Card
|
5
6
|
rescue NameError
|
@@ -19,12 +20,17 @@ Balanced.configure(nil, options)
|
|
19
20
|
puts "create our new api key"
|
20
21
|
api_key = Balanced::ApiKey.new.save
|
21
22
|
puts "Our secret is: ", api_key.secret
|
23
|
+
secret = api_key.secret
|
22
24
|
|
23
|
-
puts "configure with our secret #{
|
24
|
-
Balanced.configure(
|
25
|
+
puts "configure with our secret #{secret}"
|
26
|
+
Balanced.configure(secret)
|
25
27
|
|
26
28
|
puts "create our marketplace"
|
27
|
-
|
29
|
+
begin
|
30
|
+
marketplace = Balanced::Marketplace.new.save
|
31
|
+
rescue Balanced::Conflict => ex
|
32
|
+
marketplace = Balanced::Marketplace.my_marketplace
|
33
|
+
end
|
28
34
|
|
29
35
|
raise "Merchant.me should not be nil" if Balanced::Merchant.me.nil?
|
30
36
|
puts "what's my merchant?, easy: Merchant.me: ", Balanced::Merchant.me
|
@@ -34,11 +40,12 @@ raise "Marketplace.my_marketplace should not be nil" if Balanced::Marketplace.my
|
|
34
40
|
puts "what's my marketplace?, easy: Marketplace.my_marketplace: ", Balanced::Marketplace.my_marketplace
|
35
41
|
|
36
42
|
puts "My marketplace's name is: #{marketplace.name}"
|
37
|
-
|
38
|
-
|
43
|
+
random_name = (0...10).map{ ('a'..'z').to_a[rand(26)] }.join
|
44
|
+
puts "Changing it to #{random_name}"
|
45
|
+
marketplace.name = random_name
|
39
46
|
marketplace.save
|
40
47
|
puts "My marketplace name is now: #{Balanced::Marketplace.my_marketplace.name}"
|
41
|
-
raise "Marketplace name is NOT
|
48
|
+
raise "Marketplace name is NOT #{random_name}!" if Balanced::Marketplace.my_marketplace.name != random_name
|
42
49
|
|
43
50
|
puts "cool! let's create a new card."
|
44
51
|
card = Balanced::Card.new(
|
@@ -49,17 +56,20 @@ card = Balanced::Card.new(
|
|
49
56
|
puts "Our card uri: #{card.uri}"
|
50
57
|
|
51
58
|
puts "create our **buyer** account"
|
52
|
-
buyer = marketplace.create_buyer(
|
59
|
+
buyer = marketplace.create_buyer(
|
60
|
+
:email_address => "buyer@example.org",
|
61
|
+
:card_uri => card.uri
|
62
|
+
)
|
53
63
|
puts "our buyer account: #{buyer.uri}"
|
54
64
|
|
55
65
|
puts "hold some amount of funds on the buyer, lets say 15$"
|
56
|
-
the_hold = buyer.hold(1500)
|
66
|
+
the_hold = buyer.hold(:amount => 1500)
|
57
67
|
|
58
|
-
puts "the hold has a fee of
|
59
|
-
raise "The hold's fee is incorrect" if the_hold.fee !=
|
68
|
+
puts "the hold has a fee of 30c, here's the fee #{the_hold.fee}"
|
69
|
+
raise "The hold's fee is incorrect" if the_hold.fee != 30
|
60
70
|
|
61
71
|
puts "ok, no more holds! lets just capture it (for the full amount)"
|
62
|
-
debit = the_hold.capture
|
72
|
+
debit = the_hold.capture
|
63
73
|
|
64
74
|
puts "hmm, how much money do i have in escrow? should equal the debit amount"
|
65
75
|
marketplace = marketplace.reload
|
@@ -82,8 +92,8 @@ bank_account = Balanced::BankAccount.new(
|
|
82
92
|
).save
|
83
93
|
|
84
94
|
merchant = marketplace.create_merchant(
|
85
|
-
"merchant@example.org",
|
86
|
-
{
|
95
|
+
:email_address => "merchant@example.org",
|
96
|
+
:merchant => {
|
87
97
|
:type => "person",
|
88
98
|
:name => "Billy Jones",
|
89
99
|
:street_address => "801 High St.",
|
@@ -92,27 +102,34 @@ merchant = marketplace.create_merchant(
|
|
92
102
|
:dob => "1842-01",
|
93
103
|
:phone_number => "+16505551234",
|
94
104
|
},
|
95
|
-
bank_account.uri,
|
96
|
-
"Jack Q Merchant",
|
105
|
+
:bank_account_uri => bank_account.uri,
|
106
|
+
:name => "Jack Q Merchant",
|
97
107
|
)
|
98
108
|
|
99
109
|
puts "oh our buyer is interested in buying something for 130.00$"
|
100
|
-
another_debit = buyer.debit(
|
110
|
+
another_debit = buyer.debit(
|
111
|
+
:amount => 13000,
|
112
|
+
:appears_on_statement_as => "MARKETPLACE.COM"
|
113
|
+
)
|
101
114
|
|
102
115
|
puts "lets credit our merchant 110.00$"
|
103
|
-
credit = merchant.credit(
|
116
|
+
credit = merchant.credit(
|
117
|
+
:amount => 11000,
|
118
|
+
:description => "Buyer purchased something on MARKETPLACE.COM"
|
119
|
+
)
|
104
120
|
|
105
121
|
puts "lets assume the marketplace charges 15%, so it earned ~20"
|
106
|
-
mp_credit = marketplace.owner_account.credit(
|
122
|
+
mp_credit = marketplace.owner_account.credit(
|
123
|
+
:amount => 2000,
|
124
|
+
:description => "Our commission from MARKETPLACE.COM"
|
125
|
+
)
|
107
126
|
|
108
127
|
puts "ok lets invalid a card"
|
109
|
-
card
|
110
|
-
card.save
|
128
|
+
card.invalidate
|
111
129
|
|
112
130
|
raise "This card is INCORRECTLY VALID" if card.is_valid
|
113
131
|
|
114
132
|
puts "invalidating a bank account"
|
115
|
-
bank_account
|
116
|
-
bank_account.save
|
133
|
+
bank_account.invalidate
|
117
134
|
|
118
135
|
raise "This card is INCORRECTLY VALID" if bank_account.is_valid
|
data/lib/balanced.rb
CHANGED
@@ -13,6 +13,7 @@ module Balanced
|
|
13
13
|
@client = nil
|
14
14
|
@config = {
|
15
15
|
:scheme => 'https',
|
16
|
+
|
16
17
|
:host => 'api.balancedpayments.com',
|
17
18
|
:port => 443,
|
18
19
|
:version => '1',
|
@@ -27,6 +28,10 @@ module Balanced
|
|
27
28
|
@client = Balanced::Client.new(api_key, @config.merge(options))
|
28
29
|
end
|
29
30
|
|
31
|
+
def is_configured_with_api_key?
|
32
|
+
!@client.api_key.nil?
|
33
|
+
end
|
34
|
+
|
30
35
|
def split_the_uri uri
|
31
36
|
URI.parse(uri).path.sub(/\/$/, '').split('/')
|
32
37
|
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require "cgi"
|
2
|
+
|
3
|
+
module Balanced
|
4
|
+
class Pager
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
# A pager for paginating through resource records.
|
8
|
+
def initialize uri, options = {}
|
9
|
+
@uri = uri
|
10
|
+
@options = options
|
11
|
+
@page = nil
|
12
|
+
@resource_class = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def resource_class
|
16
|
+
return @resource_class unless @resource_class.nil?
|
17
|
+
load! unless @page
|
18
|
+
@resource_class = Balanced.from_uri items.first[:uri]
|
19
|
+
end
|
20
|
+
|
21
|
+
def first
|
22
|
+
load! unless @page
|
23
|
+
items.first.nil? ? nil : resource_class.construct_from_response(items.first)
|
24
|
+
=begin
|
25
|
+
if items.first.nil?
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
resource_class.construct_from_response items.first
|
29
|
+
=end
|
30
|
+
end
|
31
|
+
|
32
|
+
def total
|
33
|
+
load! unless @page
|
34
|
+
@page[:total]
|
35
|
+
end
|
36
|
+
|
37
|
+
def limit
|
38
|
+
load! unless @page
|
39
|
+
@page[:limit]
|
40
|
+
end
|
41
|
+
|
42
|
+
def offset
|
43
|
+
load! unless @page
|
44
|
+
@page[:offset]
|
45
|
+
end
|
46
|
+
|
47
|
+
def items
|
48
|
+
load! unless @page
|
49
|
+
@page[:items]
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [Array] Iterates through the current page of records.
|
53
|
+
# @yield [record]
|
54
|
+
def each
|
55
|
+
return enum_for :each unless block_given?
|
56
|
+
|
57
|
+
load! unless @page
|
58
|
+
|
59
|
+
loop do
|
60
|
+
@page[:items].each do |record|
|
61
|
+
yield resource_class.construct_from_response record
|
62
|
+
end
|
63
|
+
break if @page[:next_uri].nil?
|
64
|
+
self.next
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# @return [nil]
|
70
|
+
# @see Resource.find_each
|
71
|
+
# @yield [record]
|
72
|
+
def find_each
|
73
|
+
return enum_for :find_each unless block_given?
|
74
|
+
begin
|
75
|
+
each { |record| yield record }
|
76
|
+
end while self.next
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [Array, nil] Refreshes the pager's collection of records with
|
80
|
+
# the next page.
|
81
|
+
def next
|
82
|
+
load! unless @page
|
83
|
+
next_uri = @page[:next_uri]
|
84
|
+
load_from next_uri, nil unless next_uri.nil?
|
85
|
+
end
|
86
|
+
|
87
|
+
# @return [Array, nil] Refreshes the pager's collection of records with
|
88
|
+
# the previous page.
|
89
|
+
def prev
|
90
|
+
load! unless @page
|
91
|
+
prev_uri = @page[:prev_uri]
|
92
|
+
load_from prev_uri, nil unless prev_uri.nil?
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [Array, nil] Refreshes the pager's collection of records with
|
96
|
+
# the first page.
|
97
|
+
def start
|
98
|
+
load! unless @page
|
99
|
+
first_page = @page[:first_page]
|
100
|
+
load_from first_page, nil unless first_page.nil?
|
101
|
+
end
|
102
|
+
|
103
|
+
# @return [Array, nil] Load (or reload) the pager's collection from the
|
104
|
+
# original, supplied options.
|
105
|
+
def load!
|
106
|
+
load_from @uri, @options
|
107
|
+
end
|
108
|
+
alias reload load!
|
109
|
+
|
110
|
+
# @return [Pager] Duplicates the pager, updating it with the options
|
111
|
+
# supplied. Useful for resource scopes.
|
112
|
+
# @see #initialize
|
113
|
+
def paginate options = {}
|
114
|
+
dup.instance_eval {
|
115
|
+
@page = nil
|
116
|
+
@options.update options and self
|
117
|
+
}
|
118
|
+
end
|
119
|
+
alias scoped paginate
|
120
|
+
alias where paginate
|
121
|
+
|
122
|
+
def all options = {}
|
123
|
+
paginate(options).to_a
|
124
|
+
end
|
125
|
+
|
126
|
+
def find uri
|
127
|
+
if resource_class.respond_to? :find
|
128
|
+
raise NoMethodError,
|
129
|
+
"#find must be called on #{resource_class} directly"
|
130
|
+
end
|
131
|
+
|
132
|
+
resource_class.find uri
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def load_from uri, params
|
139
|
+
parsed_uri = URI.parse(uri)
|
140
|
+
|
141
|
+
unless parsed_uri.query.nil?
|
142
|
+
params.merge! CGI::parse(parsed_uri.query)
|
143
|
+
parsed_uri.query = nil
|
144
|
+
end
|
145
|
+
|
146
|
+
response = Balanced.get parsed_uri.to_s, params
|
147
|
+
@page = Balanced::Utils.hash_with_indifferent_read_access response.body
|
148
|
+
@uri = @page[:uri]
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
@@ -10,7 +10,7 @@ module Balanced
|
|
10
10
|
def initialize attributes = {}
|
11
11
|
Balanced::Utils.stringify_keys! attributes
|
12
12
|
unless attributes.has_key? 'uri'
|
13
|
-
attributes['uri'] =
|
13
|
+
attributes['uri'] = self.class.uri
|
14
14
|
end
|
15
15
|
super attributes
|
16
16
|
end
|
@@ -21,33 +21,26 @@ module Balanced
|
|
21
21
|
# @return [Account] if buyer is found
|
22
22
|
# @return [nil] if buyer is not found
|
23
23
|
def self.find_by_email email
|
24
|
-
|
25
|
-
record = response.body["items"].first
|
26
|
-
construct_from_response(record) unless record.nil?
|
27
|
-
end
|
28
|
-
|
29
|
-
def save
|
30
|
-
the_response = super
|
31
|
-
if response.status == 300
|
32
|
-
raise MoreInformationRequiredError, response
|
33
|
-
end
|
34
|
-
the_response
|
24
|
+
self.find(:first, :email_address => email)
|
35
25
|
end
|
36
26
|
|
37
27
|
# Returns a new Debit that represents a flow of money from this
|
38
28
|
# Account to your Marketplace's escrow account.
|
39
29
|
#
|
40
30
|
# @return [Debit]
|
41
|
-
def debit
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
31
|
+
def debit *args
|
32
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
33
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
34
|
+
soft_descriptor = args[1] || options.fetch(:appears_on_statement_as) { nil }
|
35
|
+
hold_uri = args[2] || options.fetch(:hold_uri) { nil }
|
36
|
+
meta = args[3] || options.fetch(:meta) { nil }
|
37
|
+
description = args[4] || options.fetch(:description) { nil }
|
38
|
+
source_uri = args[5] || options.fetch(:source_uri) { nil }
|
39
|
+
|
47
40
|
debit = Debit.new(
|
48
41
|
:uri => self.debits_uri,
|
49
42
|
:amount => amount,
|
50
|
-
:appears_on_statement_as =>
|
43
|
+
:appears_on_statement_as => soft_descriptor,
|
51
44
|
:hold_uri => hold_uri,
|
52
45
|
:meta => meta,
|
53
46
|
:description => description,
|
@@ -65,7 +58,12 @@ module Balanced
|
|
65
58
|
# added Card is used.
|
66
59
|
# @return [Hold] A Hold representing the reservation of funds from
|
67
60
|
# this Account to your Marketplace.
|
68
|
-
def hold
|
61
|
+
def hold *args
|
62
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
63
|
+
amount = args[0] || options.fetch(:amount) { }
|
64
|
+
meta = args[1] || options.fetch(:meta) { nil }
|
65
|
+
source_uri = args[2] || options.fetch(:source_uri) { nil }
|
66
|
+
|
69
67
|
hold = Hold.new(
|
70
68
|
:uri => self.holds_uri,
|
71
69
|
:amount => amount,
|
@@ -82,7 +80,13 @@ module Balanced
|
|
82
80
|
# a BankAccount already associated with this account.
|
83
81
|
# @return [Credit] A Credit representing the transfer of funds from
|
84
82
|
# your Marketplace to this Account.
|
85
|
-
def credit
|
83
|
+
def credit *args
|
84
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
85
|
+
amount = args[0] || options.fetch(:amount) { }
|
86
|
+
description = args[1] || options.fetch(:description) { nil }
|
87
|
+
meta = args[2] || options.fetch(:meta) { nil }
|
88
|
+
destination_uri = args[3] || options.fetch(:destination_uri) { nil }
|
89
|
+
|
86
90
|
credit = Credit.new(
|
87
91
|
:uri => self.credits_uri,
|
88
92
|
:amount => amount,
|
@@ -10,7 +10,7 @@ module Balanced
|
|
10
10
|
def initialize attributes = {}
|
11
11
|
Balanced::Utils.stringify_keys! attributes
|
12
12
|
unless attributes.has_key? 'uri'
|
13
|
-
attributes['uri'] =
|
13
|
+
attributes['uri'] = self.class.uri
|
14
14
|
end
|
15
15
|
super attributes
|
16
16
|
end
|
@@ -20,15 +20,32 @@ module Balanced
|
|
20
20
|
# @param [String] appears_on_statement_as If nil then Balanced will use
|
21
21
|
# the +domain_name+ property from your Marketplace.
|
22
22
|
# @return [Debit]
|
23
|
-
def debit
|
24
|
-
|
23
|
+
def debit *args
|
24
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
25
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
26
|
+
soft_descriptor = args[1] || options.fetch(:appears_on_statement_as) { nil }
|
27
|
+
meta = args[2] || options.fetch(:meta) { nil }
|
28
|
+
description = args[3] || options.fetch(:description) { nil }
|
29
|
+
|
30
|
+
self.account.debit(amount, soft_descriptor, meta, description, self.uri)
|
25
31
|
end
|
26
32
|
|
27
33
|
# Creates a Credit of funds from your Marketplace's escrow account to this Account.
|
28
34
|
#
|
29
35
|
# @return [Credit]
|
30
|
-
def credit
|
36
|
+
def credit *args
|
37
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
38
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
39
|
+
description = args[1] || options.fetch(:description) { nil }
|
40
|
+
meta = args[2] || options.fetch(:meta) { nil }
|
41
|
+
|
31
42
|
self.account.credit(amount, description, meta, self.uri)
|
32
43
|
end
|
44
|
+
|
45
|
+
def invalidate
|
46
|
+
self.is_valid = false
|
47
|
+
save
|
48
|
+
end
|
49
|
+
|
33
50
|
end
|
34
51
|
end
|
@@ -8,7 +8,7 @@ module Balanced
|
|
8
8
|
def initialize attributes = {}
|
9
9
|
Balanced::Utils.stringify_keys! attributes
|
10
10
|
unless attributes.has_key? 'uri'
|
11
|
-
attributes['uri'] =
|
11
|
+
attributes['uri'] = self.class.uri
|
12
12
|
end
|
13
13
|
super attributes
|
14
14
|
end
|
@@ -19,16 +19,33 @@ module Balanced
|
|
19
19
|
# +domain_name+ property from your Marketplace.
|
20
20
|
#
|
21
21
|
# @return [Debit]
|
22
|
-
def debit
|
22
|
+
def debit *args
|
23
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
24
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
25
|
+
appears_on_statement_as = args[1] || options.fetch(:appears_on_statement_as) { nil }
|
26
|
+
holds_uri = args[2] || options.fetch(:holds_uri) { nil }
|
27
|
+
meta = args[3] || options.fetch(:meta) { nil }
|
28
|
+
description = args[3] || options.fetch(:description) { nil }
|
29
|
+
|
23
30
|
self.account.debit(amount, appears_on_statement_as, holds_uri, meta, description, self.uri)
|
24
31
|
end
|
25
32
|
|
26
33
|
# Creates a Hold of funds from this Card to your Marketplace.
|
27
34
|
#
|
28
35
|
# @return [Hold]
|
29
|
-
def hold
|
36
|
+
def hold *args
|
37
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
38
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
39
|
+
meta = args[3] || options.fetch(:meta) { nil }
|
40
|
+
|
30
41
|
self.account.hold(amount, meta, self.uri)
|
31
42
|
end
|
43
|
+
|
44
|
+
def invalidate
|
45
|
+
self.is_valid = false
|
46
|
+
save
|
47
|
+
end
|
48
|
+
|
32
49
|
end
|
33
50
|
|
34
51
|
end
|
@@ -11,7 +11,7 @@ module Balanced
|
|
11
11
|
def initialize attributes = {}
|
12
12
|
Balanced::Utils.stringify_keys! attributes
|
13
13
|
unless attributes.has_key? 'uri'
|
14
|
-
attributes['uri'] =
|
14
|
+
attributes['uri'] = self.class.uri
|
15
15
|
end
|
16
16
|
super attributes
|
17
17
|
end
|
@@ -17,7 +17,7 @@ module Balanced
|
|
17
17
|
def initialize attributes = {}
|
18
18
|
Balanced::Utils.stringify_keys! attributes
|
19
19
|
unless attributes.has_key? 'uri'
|
20
|
-
attributes['uri'] =
|
20
|
+
attributes['uri'] = self.class.uri
|
21
21
|
end
|
22
22
|
super attributes
|
23
23
|
end
|
@@ -27,7 +27,11 @@ module Balanced
|
|
27
27
|
# of the original Debit's amount.
|
28
28
|
#
|
29
29
|
# @return [Refund]
|
30
|
-
def refund
|
30
|
+
def refund *args
|
31
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
32
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
33
|
+
description = args[1] || options.fetch(:description) { nil }
|
34
|
+
|
31
35
|
refund = Refund.new(
|
32
36
|
:uri => self.refunds_uri,
|
33
37
|
:debit_uri => self.uri,
|
@@ -16,7 +16,7 @@ module Balanced
|
|
16
16
|
def initialize attributes = {}
|
17
17
|
Balanced::Utils.stringify_keys! attributes
|
18
18
|
unless attributes.has_key? 'uri'
|
19
|
-
attributes['uri'] =
|
19
|
+
attributes['uri'] = self.class.uri
|
20
20
|
end
|
21
21
|
super attributes
|
22
22
|
end
|
@@ -32,7 +32,13 @@ module Balanced
|
|
32
32
|
# funds from the buyer's Account to your Marketplace.
|
33
33
|
#
|
34
34
|
# @return [Debit]
|
35
|
-
def capture
|
35
|
+
def capture *args
|
36
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
37
|
+
amount = args[0] || options.fetch(:amount) { nil }
|
38
|
+
appears_on_statement_as = args[1] || options.fetch(:appears_on_statement_as) { nil }
|
39
|
+
meta = args[2] || options.fetch(:meta) { nil }
|
40
|
+
description = args[3] || options.fetch(:description) { nil }
|
41
|
+
|
36
42
|
amount ||= self.amount
|
37
43
|
self.account.debit(amount, appears_on_statement_as, self.uri, meta, description)
|
38
44
|
end
|
@@ -9,11 +9,7 @@ module Balanced
|
|
9
9
|
#
|
10
10
|
# @return [Marketplace]
|
11
11
|
def self.my_marketplace
|
12
|
-
|
13
|
-
response = Balanced.get collection_path
|
14
|
-
return nil if response.body.to_s.length.zero? or response.body['total'] == 0
|
15
|
-
payload = response.body
|
16
|
-
construct_from_response payload['items'][0]
|
12
|
+
Balanced::Merchant.me.marketplace
|
17
13
|
end
|
18
14
|
|
19
15
|
# Returns an instance representing the marketplace associated with
|
@@ -27,7 +23,13 @@ module Balanced
|
|
27
23
|
# Create a buyer Account associated with this Marketplace.
|
28
24
|
#
|
29
25
|
# @return [Account]
|
30
|
-
def create_buyer
|
26
|
+
def create_buyer *args
|
27
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
28
|
+
email_address = args[0] || options.fetch(:email_address) { nil }
|
29
|
+
card_uri = args[1] || options.fetch(:card_uri) { nil }
|
30
|
+
name = args[2] || options.fetch(:name) { nil }
|
31
|
+
meta = args[3] || options.fetch(:meta) { nil }
|
32
|
+
|
31
33
|
account = Account.new(
|
32
34
|
:uri => self.accounts_uri,
|
33
35
|
:email_address => email_address,
|
@@ -51,7 +53,14 @@ module Balanced
|
|
51
53
|
# Account.create_merchant('bob@example.com', '/v1/TEST-MRxxxx')
|
52
54
|
#
|
53
55
|
# @return [Account]
|
54
|
-
def create_merchant
|
56
|
+
def create_merchant *args
|
57
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
58
|
+
email_address = args[0] || options.fetch(:email_address) { nil }
|
59
|
+
merchant = args[1] || options.fetch(:merchant) { nil }
|
60
|
+
bank_account_uri = args[2] || options.fetch(:bank_account_uri) { nil }
|
61
|
+
name = args[3] || options.fetch(:name) { nil }
|
62
|
+
meta = args[4] || options.fetch(:meta) { nil }
|
63
|
+
|
55
64
|
account_attributes = {
|
56
65
|
:uri => self.accounts_uri,
|
57
66
|
:email_address => email_address,
|
@@ -6,11 +6,7 @@ module Balanced
|
|
6
6
|
#
|
7
7
|
# @return [Merchant]
|
8
8
|
def self.me
|
9
|
-
|
10
|
-
response = Balanced.get collection_path
|
11
|
-
return nil if response.body.to_s.length.zero? or response.body['total'] == 0
|
12
|
-
payload = response.body
|
13
|
-
construct_from_response payload['items'][0]
|
9
|
+
self.all.first
|
14
10
|
end
|
15
11
|
|
16
12
|
# Returns the Merchant associated with your Marketplace.
|
@@ -10,7 +10,7 @@ module Balanced
|
|
10
10
|
def initialize attributes = {}
|
11
11
|
Balanced::Utils.stringify_keys! attributes
|
12
12
|
unless attributes.has_key? 'uri'
|
13
|
-
attributes['uri'] =
|
13
|
+
attributes['uri'] = self.class.uri
|
14
14
|
end
|
15
15
|
super attributes
|
16
16
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require_relative "../pager"
|
2
|
+
|
3
|
+
|
1
4
|
module Balanced
|
2
5
|
module Resource
|
3
6
|
attr_accessor :attributes
|
@@ -8,8 +11,8 @@ module Balanced
|
|
8
11
|
|
9
12
|
# delegate the query to the pager module
|
10
13
|
|
11
|
-
def find
|
12
|
-
self.class.find
|
14
|
+
def find *arguments
|
15
|
+
self.class.find *arguments
|
13
16
|
end
|
14
17
|
|
15
18
|
def save
|
@@ -80,6 +83,23 @@ module Balanced
|
|
80
83
|
Utils.underscore resource_name
|
81
84
|
end
|
82
85
|
|
86
|
+
def uri
|
87
|
+
# the uri of a particular resource depends if there's a marketplace created or not
|
88
|
+
# if there's a marketplace, then all resources have their own uri from there and the top level ones
|
89
|
+
# if there's not a marketplace
|
90
|
+
# if there's an api key, then the merchant is available
|
91
|
+
# if there's no api key, the only resources exposed are purely top level
|
92
|
+
if self == Balanced::Merchant or self == Balanced::Marketplace or self == Balanced::ApiKey
|
93
|
+
collection_path
|
94
|
+
else
|
95
|
+
if Balanced::Marketplace.my_marketplace.nil?
|
96
|
+
raise Balanced::Error, "#{self.name} is nested under a marketplace, which is not created or configured."
|
97
|
+
else
|
98
|
+
Balanced::Marketplace.my_marketplace.send(collection_name + '_uri')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
83
103
|
def construct_from_response payload
|
84
104
|
payload = Balanced::Utils.hash_with_indifferent_read_access payload
|
85
105
|
return payload if payload[:uri].nil?
|
@@ -102,9 +122,7 @@ module Balanced
|
|
102
122
|
# if uri is a collection -> this would definitely be if it ends in a symbol
|
103
123
|
# then we should allow a lazy executor of the query pager
|
104
124
|
if Balanced.is_collection(value)
|
105
|
-
|
106
|
-
p "TODO: return the pager for this class: #{values_class}"
|
107
|
-
values_class.new
|
125
|
+
values_class.all
|
108
126
|
else
|
109
127
|
values_class.find(value)
|
110
128
|
end
|
@@ -122,14 +140,30 @@ module Balanced
|
|
122
140
|
instance
|
123
141
|
end
|
124
142
|
|
125
|
-
def find
|
126
|
-
|
127
|
-
|
143
|
+
def find *arguments
|
144
|
+
scope = arguments.slice!(0)
|
145
|
+
options = arguments.slice!(0) || {}
|
146
|
+
case scope
|
147
|
+
when :all then all(options)
|
148
|
+
when :first then paginate(options).first
|
149
|
+
else
|
150
|
+
response = Balanced.get scope, options
|
151
|
+
construct_from_response response.body
|
152
|
+
end
|
128
153
|
end
|
129
154
|
|
130
|
-
|
155
|
+
def paginate options = {}
|
156
|
+
Pager.new uri, options
|
157
|
+
end
|
158
|
+
alias scoped paginate
|
159
|
+
alias where paginate
|
131
160
|
|
161
|
+
def all options = {}
|
162
|
+
pager = paginate(options)
|
163
|
+
pager.to_a
|
164
|
+
end
|
132
165
|
|
166
|
+
end
|
133
167
|
|
134
168
|
end
|
135
169
|
end
|
@@ -8,7 +8,7 @@ module Balanced
|
|
8
8
|
def initialize attributes = {}
|
9
9
|
Balanced::Utils.stringify_keys! attributes
|
10
10
|
unless attributes.has_key? 'uri'
|
11
|
-
attributes['uri'] =
|
11
|
+
attributes['uri'] = self.class.uri
|
12
12
|
end
|
13
13
|
super attributes
|
14
14
|
end
|
data/lib/balanced/utils.rb
CHANGED
@@ -37,6 +37,13 @@ module Balanced
|
|
37
37
|
base.each_pair do |key, value|
|
38
38
|
if value.is_a? Hash
|
39
39
|
value = hash_with_indifferent_read_access value
|
40
|
+
elsif value.respond_to? :each
|
41
|
+
value.map! do |v|
|
42
|
+
if v.is_a? Hash
|
43
|
+
v = hash_with_indifferent_read_access v
|
44
|
+
end
|
45
|
+
v
|
46
|
+
end
|
40
47
|
end
|
41
48
|
indifferent[key.to_s] = value
|
42
49
|
end
|
data/lib/balanced/version.rb
CHANGED
@@ -8,6 +8,38 @@ describe Balanced::Account do
|
|
8
8
|
@marketplace = Balanced::Marketplace.new.save
|
9
9
|
end
|
10
10
|
|
11
|
+
describe "Account.uri" do
|
12
|
+
use_vcr_cassette
|
13
|
+
|
14
|
+
context "when ApiKey is not configured" do
|
15
|
+
use_vcr_cassette
|
16
|
+
before do
|
17
|
+
Balanced.configure nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "throw an exception that it can not generate the URI" do
|
21
|
+
expect {
|
22
|
+
Balanced::Account.uri
|
23
|
+
}.to raise_error Balanced::Error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when ApiKey is configured" do
|
28
|
+
use_vcr_cassette
|
29
|
+
before do
|
30
|
+
api_key = Balanced::ApiKey.new.save
|
31
|
+
Balanced.configure api_key.secret
|
32
|
+
@marketplace = Balanced::Marketplace.new.save
|
33
|
+
end
|
34
|
+
|
35
|
+
it "it matches the resource's uri structure" do
|
36
|
+
uri = Balanced::Account.uri
|
37
|
+
uri.should_not be_nil
|
38
|
+
uri.should match ACCOUNTS_URI_REGEX
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
11
43
|
describe "merchant" do
|
12
44
|
use_vcr_cassette
|
13
45
|
|
@@ -207,7 +239,7 @@ describe Balanced::Account do
|
|
207
239
|
describe "buyer" do
|
208
240
|
describe "#save" do
|
209
241
|
describe "when creating" do
|
210
|
-
use_vcr_cassette
|
242
|
+
use_vcr_cassette :record => :new_episodes
|
211
243
|
before do
|
212
244
|
card = Balanced::Card.new(
|
213
245
|
:card_number => "5105105105105100",
|
@@ -226,7 +258,7 @@ describe Balanced::Account do
|
|
226
258
|
|
227
259
|
describe "after #save" do
|
228
260
|
describe "attributes" do
|
229
|
-
use_vcr_cassette
|
261
|
+
use_vcr_cassette :record => :new_episodes
|
230
262
|
before do
|
231
263
|
card = Balanced::Card.new(
|
232
264
|
:card_number => "4111111111111111",
|
@@ -296,11 +328,16 @@ describe Balanced::Account do
|
|
296
328
|
subject { @buyer.cards_uri }
|
297
329
|
it { should match CARDS_URI_REGEX }
|
298
330
|
end
|
331
|
+
describe "#cards" do
|
332
|
+
subject { @buyer.cards }
|
333
|
+
it { should be_instance_of Array }
|
334
|
+
it { should_not be_empty }
|
335
|
+
it { subject.first.should be_instance_of Balanced::Card }
|
336
|
+
end
|
299
337
|
end
|
300
338
|
end
|
301
339
|
end
|
302
340
|
|
303
|
-
|
304
341
|
describe "#add_card" do
|
305
342
|
|
306
343
|
describe "when executing" do
|
@@ -418,10 +455,85 @@ describe Balanced::Account do
|
|
418
455
|
end
|
419
456
|
|
420
457
|
end
|
458
|
+
|
459
|
+
describe "#debit" do
|
460
|
+
use_vcr_cassette :match_requests_on => [:body], :record => :new_episodes
|
461
|
+
before do
|
462
|
+
card = Balanced::Card.new(
|
463
|
+
:card_number => "4111111111111111",
|
464
|
+
:expiration_month => "12",
|
465
|
+
:expiration_year => "2015",
|
466
|
+
).save
|
467
|
+
begin
|
468
|
+
@buyer = Balanced::Account.new(
|
469
|
+
:uri => @marketplace.accounts_uri,
|
470
|
+
:email_address => "buyer7@example.org",
|
471
|
+
:card_uri => card.uri,
|
472
|
+
:name => "Jack Q Buyer"
|
473
|
+
).save
|
474
|
+
rescue Balanced::Conflict => ex
|
475
|
+
@buyer = Balanced::Account.find(ex.extras[:account_uri])
|
476
|
+
end
|
477
|
+
end
|
478
|
+
it "takes optional parameters" do
|
479
|
+
debit = @buyer.debit(
|
480
|
+
:amount => 500,
|
481
|
+
:appears_on_statement_as => "BOBS BURGERS",
|
482
|
+
)
|
483
|
+
debit.should be_instance_of Balanced::Debit
|
484
|
+
debit.amount.should eql 500
|
485
|
+
debit.appears_on_statement_as.should eql "BOBS BURGERS"
|
486
|
+
end
|
487
|
+
it "takes positional parameters" do
|
488
|
+
debit = @buyer.debit(500, "FOO FIGHTER")
|
489
|
+
debit.should be_instance_of Balanced::Debit
|
490
|
+
debit.amount.should eql 500
|
491
|
+
debit.appears_on_statement_as.should eql "FOO FIGHTER"
|
492
|
+
end
|
493
|
+
|
494
|
+
end
|
421
495
|
end
|
422
496
|
|
423
|
-
describe ".
|
497
|
+
describe ".find" do
|
424
498
|
use_vcr_cassette
|
499
|
+
|
500
|
+
before do
|
501
|
+
api_key = Balanced::ApiKey.new.save
|
502
|
+
Balanced.configure api_key.secret
|
503
|
+
@marketplace = Balanced::Marketplace.new.save
|
504
|
+
card = Balanced::Card.new(
|
505
|
+
:card_number => "5105105105105100",
|
506
|
+
:expiration_month => "12",
|
507
|
+
:expiration_year => "2015",
|
508
|
+
).save
|
509
|
+
Balanced::Marketplace.my_marketplace.create_buyer(
|
510
|
+
"john.doe@example.com",
|
511
|
+
card.uri
|
512
|
+
)
|
513
|
+
end
|
514
|
+
|
515
|
+
context "(:all, :some_field => 'op')" do
|
516
|
+
use_vcr_cassette
|
517
|
+
it "should find the account by returning a page with items of one" do
|
518
|
+
response = Balanced::Account.find(:all, :email_address => "john.doe@example.com")
|
519
|
+
response.should be_instance_of Array
|
520
|
+
response[0].should be_instance_of Balanced::Account
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
context "(:first, :some_field => 'op')" do
|
525
|
+
use_vcr_cassette
|
526
|
+
it "should find the account by returning the first item" do
|
527
|
+
response = Balanced::Account.find(:first, :email_address => "john.doe@example.com")
|
528
|
+
response.should be_instance_of Balanced::Account
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
532
|
+
|
533
|
+
end
|
534
|
+
|
535
|
+
describe ".find_by_email" do
|
536
|
+
use_vcr_cassette :record => :new_episodes
|
425
537
|
before do
|
426
538
|
api_key = Balanced::ApiKey.new.save
|
427
539
|
Balanced.configure api_key.secret
|
@@ -438,14 +550,14 @@ describe Balanced::Account do
|
|
438
550
|
end
|
439
551
|
|
440
552
|
context "email address is in system" do
|
441
|
-
use_vcr_cassette
|
553
|
+
use_vcr_cassette :record => :new_episodes
|
442
554
|
it "should return account object" do
|
443
555
|
Balanced::Account.find_by_email("john.doe@example.com").should be_instance_of Balanced::Account
|
444
556
|
end
|
445
557
|
end
|
446
558
|
|
447
559
|
context "email address does not exist" do
|
448
|
-
use_vcr_cassette
|
560
|
+
use_vcr_cassette :record => :new_episodes
|
449
561
|
it "should return nil" do
|
450
562
|
Balanced::Account.find_by_email("foo@bar.com").should be_nil
|
451
563
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -20,6 +20,7 @@ RSpec.configure do |c|
|
|
20
20
|
end
|
21
21
|
|
22
22
|
|
23
|
+
ACCOUNTS_URI_REGEX = /\/v1\/marketplaces\/TEST-\w*\/accounts/
|
23
24
|
MERCHANT_URI_REGEX = /\/v1\/marketplaces\/TEST-\w*\/accounts\/\w*/
|
24
25
|
HOLDS_URI_REGEX = /\/v1\/marketplaces\/TEST-\w*\/accounts\/\w*\/holds/
|
25
26
|
BANK_ACCOUNTS_URI_REGEX = /\/v1\/marketplaces\/TEST-\w*\/accounts\/\w*\/bank_accounts/
|
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.3.
|
4
|
+
version: 0.3.5
|
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: 2012-
|
12
|
+
date: 2012-07-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/balanced.rb
|
78
78
|
- lib/balanced/client.rb
|
79
79
|
- lib/balanced/error.rb
|
80
|
+
- lib/balanced/pager.rb
|
80
81
|
- lib/balanced/resources.rb
|
81
82
|
- lib/balanced/resources/account.rb
|
82
83
|
- lib/balanced/resources/api_key.rb
|