balanced 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/examples.rb +36 -21
- data/lib/balanced/base.rb +9 -4
- data/lib/balanced/client.rb +11 -10
- data/lib/balanced/version.rb +1 -1
- metadata +1 -1
data/examples/examples.rb
CHANGED
@@ -6,56 +6,65 @@ rescue NameError
|
|
6
6
|
raise "wtf"
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
puts "create our new api key"
|
10
10
|
api_key = Balanced::ApiKey.new.save
|
11
11
|
puts "Our secret is: ", api_key.secret
|
12
12
|
|
13
|
-
|
13
|
+
puts "configure with our secret #{api_key}"
|
14
14
|
Balanced.configure(api_key.secret)
|
15
15
|
|
16
|
-
|
16
|
+
puts "create our marketplace"
|
17
17
|
marketplace = Balanced::Marketplace.new.save
|
18
18
|
|
19
|
-
# what's my merchant?
|
20
19
|
raise "Merchant.me should not be nil" if Balanced::Merchant.me.nil?
|
20
|
+
puts "what's my merchant?, easy: Merchant.me: ", Balanced::Merchant.me
|
21
21
|
|
22
22
|
# what's my marketplace?
|
23
23
|
raise "Marketplace.my_marketplace should not be nil" if Balanced::Marketplace.my_marketplace.nil?
|
24
|
+
puts "what's my marketplace?, easy: Marketplace.my_marketplace: ", Balanced::Marketplace.my_marketplace
|
24
25
|
|
25
|
-
|
26
|
+
puts "My marketplace's name is: #{marketplace.name}"
|
27
|
+
puts "Changing it to TestFooey"
|
28
|
+
marketplace.name = "TestFooey"
|
29
|
+
marketplace.save
|
30
|
+
puts "My marketplace name is now: #{Balanced::Marketplace.my_marketplace.name}"
|
31
|
+
raise "Marketplace name is NOT TestFooey!" if Balanced::Marketplace.my_marketplace.name != "TestFooey"
|
26
32
|
|
33
|
+
puts "cool! let's create a new card."
|
27
34
|
card = Balanced::Card.new(
|
28
35
|
:card_number => "5105105105105100",
|
29
36
|
:expiration_month => "12",
|
30
37
|
:expiration_year => "2015",
|
31
38
|
).save
|
39
|
+
puts "Our card uri: #{card.uri}"
|
32
40
|
|
33
|
-
|
41
|
+
puts "create our **buyer** account"
|
34
42
|
buyer = marketplace.create_buyer("buyer@example.org", card.uri)
|
43
|
+
puts "our buyer account: #{buyer.uri}"
|
35
44
|
|
36
|
-
|
45
|
+
puts "hold some amount of funds on the buyer, lets say 15$"
|
37
46
|
the_hold = buyer.hold(1500)
|
38
47
|
|
39
|
-
|
48
|
+
puts "the hold has a fee of 35c, here's the fee #{the_hold.fee}"
|
40
49
|
raise "The hold's fee is incorrect" if the_hold.fee != 35
|
41
50
|
|
42
|
-
|
51
|
+
puts "ok, no more holds! lets just capture it (for the full amount)"
|
43
52
|
debit = the_hold.capture()
|
44
53
|
|
45
|
-
|
46
|
-
|
47
|
-
marketplace = marketplace.my_marketplace
|
54
|
+
puts "hmm, how much money do i have in escrow? should equal the debit amount"
|
55
|
+
marketplace = marketplace.reload
|
48
56
|
raise "1500 is not in escrow! this is wrong" if marketplace.in_escrow != 1500
|
57
|
+
puts "i have #{marketplace.in_escrow} in escrow!"
|
49
58
|
|
50
|
-
|
51
|
-
# cool. now let me refund
|
59
|
+
puts "cool. now let me refund the full amount"
|
52
60
|
refund = debit.refund() # the full amount!
|
53
61
|
|
54
|
-
|
62
|
+
puts "notice how Balanced refunds you your fees? refund fees: #{refund.fee}"
|
55
63
|
raise "Woah, fees are incorrect" if (refund.fee + debit.fee) != 0
|
56
64
|
|
57
|
-
|
58
|
-
|
65
|
+
puts "ok, we have a merchant that's signing up, let's create an account for them " \
|
66
|
+
"first, lets create their bank account."
|
67
|
+
|
59
68
|
bank_account = Balanced::BankAccount.new(
|
60
69
|
:account_number => "1234567890",
|
61
70
|
:bank_code => "12",
|
@@ -77,13 +86,19 @@ merchant = marketplace.create_merchant(
|
|
77
86
|
"Jack Q Merchant",
|
78
87
|
)
|
79
88
|
|
80
|
-
|
89
|
+
puts "oh our buyer is interested in buying something for 130.00$"
|
81
90
|
another_debit = buyer.debit(13000, "MARKETPLACE.COM")
|
82
91
|
|
83
|
-
|
92
|
+
puts "lets credit our merchant 110.00$"
|
84
93
|
credit = merchant.credit(11000, "Buyer purchased something on MARKETPLACE.COM")
|
85
94
|
|
86
|
-
|
95
|
+
puts "lets assume the marketplace charges 15%, so it earned ~20"
|
87
96
|
mp_credit = marketplace.owner_account.credit(2000, "Our commission from MARKETPLACE.COM")
|
88
97
|
|
89
|
-
|
98
|
+
puts "ok lets invalid a card"
|
99
|
+
card['is_valid'] = false
|
100
|
+
card.save
|
101
|
+
|
102
|
+
raise "This card is NOT VALID" if card.is_valid
|
103
|
+
|
104
|
+
puts "and there you have it :)"
|
data/lib/balanced/base.rb
CHANGED
@@ -50,7 +50,12 @@ module Balanced
|
|
50
50
|
}
|
51
51
|
end
|
52
52
|
|
53
|
-
instance.
|
53
|
+
instance.class.instance_eval {
|
54
|
+
define_method(name) { self[name] } # Get.
|
55
|
+
define_method("#{name}=") { |value| self[name] = value } # Set.
|
56
|
+
define_method("#{name}?") { !!self[name] } # Present.
|
57
|
+
}
|
58
|
+
instance.send("#{name}=".to_s, value)
|
54
59
|
end
|
55
60
|
instance
|
56
61
|
end
|
@@ -83,8 +88,8 @@ module Balanced
|
|
83
88
|
# delegate the query to the pager module
|
84
89
|
|
85
90
|
def find uri, options={}
|
86
|
-
|
87
|
-
construct_from_response
|
91
|
+
response = Balanced.get uri
|
92
|
+
self.class.construct_from_response response.body
|
88
93
|
end
|
89
94
|
|
90
95
|
def save
|
@@ -100,7 +105,7 @@ module Balanced
|
|
100
105
|
end
|
101
106
|
|
102
107
|
def destroy
|
103
|
-
Balanced.delete
|
108
|
+
Balanced.delete self.attributes['uri']
|
104
109
|
end
|
105
110
|
|
106
111
|
def reload response = nil
|
data/lib/balanced/client.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'logger'
|
1
2
|
require "uri"
|
2
3
|
require "faraday"
|
3
4
|
require "faraday_middleware"
|
@@ -5,17 +6,12 @@ require "faraday_middleware"
|
|
5
6
|
module Balanced
|
6
7
|
class Client
|
7
8
|
|
8
|
-
HTTP_HEADERS = {
|
9
|
-
'Accept' => 'application/json',
|
10
|
-
'Accept-Charset' => 'utf-8',
|
11
|
-
'User-Agent' => "balanced-ruby/#{Balanced::VERSION}",
|
12
|
-
}
|
13
|
-
|
14
9
|
DEFAULTS = {
|
15
10
|
:scheme => 'http',
|
16
11
|
:host => 'localhost',
|
17
12
|
:port => 5000,
|
18
13
|
:version => '1',
|
14
|
+
:logging_level => 'WARN',
|
19
15
|
}
|
20
16
|
|
21
17
|
attr :api_key, true
|
@@ -30,24 +26,29 @@ module Balanced
|
|
30
26
|
|
31
27
|
|
32
28
|
def build_conn
|
29
|
+
logger = Logger.new(STDOUT)
|
30
|
+
logger.level = Logger.const_get(DEFAULTS[:logging_level].to_s)
|
31
|
+
|
33
32
|
@conn = Faraday.new url do |cxn|
|
34
33
|
cxn.request :json
|
35
34
|
|
36
|
-
cxn.response :logger
|
35
|
+
cxn.response :logger, logger
|
37
36
|
cxn.response :json
|
37
|
+
cxn.response :raise_error # raise exceptions on 40x, 50x responses
|
38
38
|
cxn.adapter Faraday.default_adapter
|
39
39
|
end
|
40
40
|
@conn.path_prefix = '/'
|
41
|
+
@conn.headers['User-Agent'] = "balanced-ruby/#{Balanced::VERSION}"
|
41
42
|
end
|
42
43
|
|
43
44
|
def inspect # :nodoc:
|
44
|
-
"<Balanced::Client @api_key
|
45
|
+
"<Balanced::Client @api_key=#@api_key, @url=#{url}>"
|
45
46
|
end
|
46
47
|
|
47
48
|
def url
|
48
49
|
_url = URI::HTTP.build(
|
49
|
-
|
50
|
-
|
50
|
+
:host => @config[:host],
|
51
|
+
:port => @config[:port],
|
51
52
|
)
|
52
53
|
# wow. yes, this is what you actually have to do.
|
53
54
|
_url.scheme = @config[:scheme]
|
data/lib/balanced/version.rb
CHANGED