balanced 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  Online Marketplace Payments
4
4
 
5
5
  [![Build Status](https://secure.travis-ci.org/balanced/balanced-ruby.png)](http://travis-ci.org/balanced/balanced-ruby)
6
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/balanced/balanced-ruby)
6
7
 
7
8
  ## Installation
8
9
 
@@ -0,0 +1,80 @@
1
+ """
2
+ Welcome weary traveller. Sick of polling for state changes? Well today have I
3
+ got good news for you. Run this example below to see how to get yourself some
4
+ callback goodness and to understand how events work.
5
+ """
6
+ cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
7
+ $:.unshift(cwd + "/lib")
8
+ require 'balanced'
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'json'
12
+
13
+ api_key = Balanced::ApiKey.new.save
14
+ Balanced.configure(api_key.secret)
15
+ marketplace = Balanced::Marketplace.new.save
16
+
17
+ # mumble, let's create a requestb.in
18
+ request_bin = Net::HTTP.post_form(URI.parse('http://requestb.in/api/v1/bins'), {})
19
+ response = JSON.parse request_bin.body
20
+ bin_name = response['name']
21
+ callback_url = 'http://requestb.in/' + bin_name
22
+ requests_url = 'http://requestb.in/api/v1/bins/%s/requests' % bin_name
23
+
24
+ puts 'let\'s create a callback'
25
+ marketplace.create_callback(
26
+ :url => callback_url
27
+ )
28
+
29
+ puts 'let\'s create a card and associate it with a new account'
30
+ card = Balanced::Card.new(
31
+ :expiration_month => '12',
32
+ :security_code => '123',
33
+ :card_number => '5105105105105100',
34
+ :expiration_year => '2020'
35
+ ).save()
36
+ buyer = Balanced::Account.new(
37
+ 'card_uri' => card.uri
38
+ ).save()
39
+
40
+ puts 'generate a debit (which implicitly creates and captures a hold)'
41
+ buyer.debit(:amount => 100)
42
+
43
+ puts 'event creation is an async operation, let\'s wait until we have ' \
44
+ 'some events!'
45
+ while Balanced::Event.find(:all).length == 0
46
+ puts 'Zzzz'
47
+ sleep(0)
48
+ end
49
+
50
+ puts 'Woop, we got some events, let us see what there is to look at'
51
+ for event in Balanced::Event.find(:all)
52
+ puts 'this was a %s event, it occurred at %s, the callback has a ' \
53
+ 'status of %s' % [
54
+ event.type,
55
+ event.occurred_at,
56
+ event.callback_statuses
57
+ ]
58
+ end
59
+
60
+ puts 'you can inspect each event to see the logs'
61
+ event = Balanced::Event.find(:first)
62
+ for callback in event.callbacks
63
+ puts 'inspecting callback to %s for event %s' % [
64
+ callback.url,
65
+ event.type
66
+ ]
67
+
68
+ for log in callback.logs
69
+ puts 'this attempt to the callback has a status of %s' % [log.status]
70
+ end
71
+ end
72
+
73
+ puts 'ok, let\'s check with requestb.in to see if our callbacks fired at %s' % callback_url
74
+ uri = URI.parse(requests_url)
75
+ request_bin = Net::HTTP.get_response(uri)
76
+ response = JSON.parse request_bin.body
77
+ puts 'we received %s callbacks, you can view them at http://requestb.in/%s?inspect' % [
78
+ response.length,
79
+ bin_name,
80
+ ]
@@ -7,15 +7,15 @@ require "balanced_exception_middleware"
7
7
 
8
8
  module Balanced
9
9
  class Client
10
-
10
+
11
11
  DEFAULTS = {
12
12
  :scheme => 'http',
13
13
  :host => 'localhost',
14
14
  :port => 5000,
15
15
  :version => '1',
16
16
  :logging_level => 'WARN',
17
- :connection_timeout => 2,
18
- :read_timeout => 5,
17
+ :connection_timeout => 30,
18
+ :read_timeout => 30,
19
19
  :logger => nil,
20
20
  :ssl_verify => true
21
21
  }
@@ -11,3 +11,5 @@ require "marketplace"
11
11
  require "merchant"
12
12
  require "refund"
13
13
  require "transaction"
14
+ require "callback"
15
+ require "event"
@@ -38,6 +38,19 @@ module Balanced
38
38
  meta = args[3] || options.fetch(:meta) { nil }
39
39
  description = args[4] || options.fetch(:description) { nil }
40
40
  source_uri = args[5] || options.fetch(:source_uri) { nil }
41
+ on_behalf_of = args[6] || options.fetch(:on_behalf_of) { nil }
42
+
43
+ if on_behalf_of
44
+ if on_behalf_of.respond_to? :uri
45
+ on_behalf_of = on_behalf_of.uri
46
+ end
47
+ if !on_behalf_of.is_a?(String)
48
+ raise ArgumentError, 'The on_behalf_of parameter needs to be an account URI'
49
+ end
50
+ if on_behalf_of == self.uri
51
+ raise ArgumentError, 'The on_behalf_of parameter MAY NOT be the same account as the account you are debiting!'
52
+ end
53
+ end
41
54
 
42
55
  debit = Debit.new(
43
56
  :uri => self.debits_uri,
@@ -47,6 +60,7 @@ module Balanced
47
60
  :meta => meta,
48
61
  :description => description,
49
62
  :source_uri => source_uri,
63
+ :on_behalf_of_uri => on_behalf_of,
50
64
  )
51
65
  debit.save
52
66
  end
@@ -146,8 +160,12 @@ module Balanced
146
160
  # }
147
161
  # }
148
162
  #
149
- def promote_to_merchant merchant_data
150
- self.merchant = merchant_data
163
+ def promote_to_merchant merchant
164
+ if merchant.is_a?(String)
165
+ self.merchant_uri = merchant
166
+ else
167
+ self.merchant = merchant
168
+ end
151
169
  save
152
170
  end
153
171
 
@@ -0,0 +1,11 @@
1
+ module Balanced
2
+
3
+ # A Callback is a publicly accessible location that can receive POSTed JSON
4
+ # data whenever an Event is generated.
5
+ #
6
+ class Callback
7
+ include Balanced::Resource
8
+
9
+ end
10
+
11
+ end
@@ -18,6 +18,14 @@ module Balanced
18
18
  # If +appears_on_statement_as+ is nil, then Balanced will use the
19
19
  # +domain_name+ property from your Marketplace.
20
20
  #
21
+ # @param [Array] args
22
+ # @option args [Integer] :amount the amount of the purchase in cents
23
+ # @option args [String] :appears_on_statement_as
24
+ # @option args [String] :hold_uri
25
+ # @option args [Hash] :meta a hash of data to save with the Debit
26
+ # @option args [String] :description
27
+ # @option args [String] :source_uri
28
+ #
21
29
  # @return [Debit]
22
30
  def debit *args
23
31
  warn_on_positional args
@@ -9,6 +9,14 @@ module Balanced
9
9
  class Credit
10
10
  include Balanced::Resource
11
11
 
12
+ # @param [Array] args
13
+ # @option [String] :uri the uri of the credit, in the case of an update
14
+ # @option [Hash] :bank_account
15
+ # @option [Integer] :amount amount in cents
16
+ # @option [String] :description a text description of the credit
17
+ # @option [String] :appears_on_statement_as
18
+ # @option [Hash] :meta
19
+ # @option [String] :destination_uri the uri of the bank account to credit
12
20
  def initialize *args
13
21
  options = args.last.is_a?(Hash) ? args.pop : {}
14
22
  uri = options.fetch(:uri) { self.class.uri }
@@ -0,0 +1,27 @@
1
+ module Balanced
2
+
3
+ # An Event is a snapshot of another resource at a point in time when
4
+ # something significant occurred. Events are created when resources are
5
+ # created, updated, deleted or otherwise change state such as a Credit being
6
+ # marked as failed.
7
+ #
8
+ class Event
9
+ include Balanced::Resource
10
+
11
+ end
12
+
13
+ # Represents a single event being sent to a callback.
14
+ #
15
+ class EventCallback
16
+ include Balanced::Resource
17
+
18
+ end
19
+
20
+ # Represents a single log of an event sent to a callback.
21
+ #
22
+ class Log
23
+ include Balanced::Resource
24
+
25
+ end
26
+
27
+ end
@@ -209,6 +209,19 @@ module Balanced
209
209
  card.save
210
210
  end
211
211
 
212
+ def create_callback *args
213
+ warn_on_positional args
214
+
215
+ options = args.last.is_a?(Hash) ? args.pop : {}
216
+ url = args[0] || options.fetch(:url) { nil }
217
+
218
+ callback = Callback.new(
219
+ 'uri' => self.callbacks_uri,
220
+ 'url' => url
221
+ )
222
+ callback.save
223
+ end
224
+
212
225
  end
213
226
 
214
227
  end
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.5.4'
2
+ VERSION = '0.5.5'
3
3
  end
@@ -461,6 +461,36 @@ describe Balanced::Account do
461
461
  debit.amount.should eql 500
462
462
  debit.appears_on_statement_as.should eql "BOBS BURGERS"
463
463
  end
464
+ it "accepts on_behalf_of parameter" do
465
+ merchant = Balanced::Account.new.save
466
+ debit = @buyer.debit(
467
+ :amount => 500,
468
+ :on_behalf_of => merchant
469
+ )
470
+ debit.should be_instance_of Balanced::Debit
471
+ debit.amount.should eql 500
472
+ # TODO: once the API returns on_behalf_of in the response, make sure it shows up. For now we just make sure the debit didn't fail.
473
+ end
474
+ it "accepts URI for on_behalf_of" do
475
+ merchant = Balanced::Account.new.save
476
+ debit = @buyer.debit(
477
+ :amount => 500,
478
+ :on_behalf_of => merchant.uri
479
+ )
480
+ debit.should be_instance_of Balanced::Debit
481
+ debit.amount.should eql 500
482
+ # TODO: once the API returns on_behalf_of in the response, make sure it shows up. For now we just make sure the debit didn't fail.
483
+ end
484
+
485
+ it "fails with bad on_behalf_of parameter" do
486
+ expect {
487
+ @buyer.debit(
488
+ :amount => 500,
489
+ :on_behalf_of => @buyer
490
+ )
491
+ }.to raise_error ArgumentError
492
+ end
493
+
464
494
  # this is deprecated
465
495
  it "takes positional parameters" do
466
496
  debit = @buyer.debit(500, "FOO FIGHTER")
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Callback do
4
+ use_vcr_cassette
5
+
6
+ before do
7
+ api_key = Balanced::ApiKey.new.save
8
+ Balanced.configure api_key.secret
9
+
10
+ @marketplace = Balanced::Marketplace.new.save
11
+
12
+ end
13
+
14
+ describe "create" do
15
+ use_vcr_cassette
16
+
17
+ before do
18
+ @callback = @marketplace.create_callback(
19
+ :url => "http://www.example.com/balanced_callback"
20
+ )
21
+ end
22
+
23
+ context 'check url' do
24
+
25
+ subject { @callback.url}
26
+ it { should eql "http://www.example.com/balanced_callback" }
27
+
28
+ end
29
+
30
+ end
31
+
32
+ describe "delete" do
33
+ use_vcr_cassette
34
+
35
+ before do
36
+ @callback = @marketplace.create_callback(
37
+ :url => "http://www.example.com/balanced_callback"
38
+ )
39
+ end
40
+
41
+ context 'delete' do
42
+ it "should delete without an error" do
43
+
44
+ @callback.destroy
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ 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
4
+ version: 0.5.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-12 00:00:00.000000000 Z
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -47,7 +47,8 @@ description: ! "Balanced is the payments platform for marketplaces.\n Integra
47
47
  a payments experience just like Amazon for your marketplace.\n Forget about dealing
48
48
  with banking systems, compliance, fraud, and security.\n "
49
49
  email:
50
- - mahmoud@poundpay.com
50
+ - !binary |-
51
+ bWFobW91ZEBwb3VuZHBheS5jb20=
51
52
  executables: []
52
53
  extensions: []
53
54
  extra_rdoc_files: []
@@ -73,6 +74,7 @@ files:
73
74
  - doc/balanced_templates/default/onefile/html/layout.erb
74
75
  - doc/balanced_templates/default/onefile/html/setup.rb
75
76
  - doc/balanced_templates/default/tags/html/index.erb
77
+ - examples/events_and_callbacks.rb
76
78
  - examples/examples.rb
77
79
  - lib/balanced.rb
78
80
  - lib/balanced/client.rb
@@ -82,9 +84,11 @@ files:
82
84
  - lib/balanced/resources/account.rb
83
85
  - lib/balanced/resources/api_key.rb
84
86
  - lib/balanced/resources/bank_account.rb
87
+ - lib/balanced/resources/callback.rb
85
88
  - lib/balanced/resources/card.rb
86
89
  - lib/balanced/resources/credit.rb
87
90
  - lib/balanced/resources/debit.rb
91
+ - lib/balanced/resources/event.rb
88
92
  - lib/balanced/resources/hold.rb
89
93
  - lib/balanced/resources/marketplace.rb
90
94
  - lib/balanced/resources/merchant.rb
@@ -99,6 +103,7 @@ files:
99
103
  - spec/balanced/resources/account_spec.rb
100
104
  - spec/balanced/resources/api_key_spec.rb
101
105
  - spec/balanced/resources/bank_account_spec.rb
106
+ - spec/balanced/resources/callback_spec.rb
102
107
  - spec/balanced/resources/credit_spec.rb
103
108
  - spec/balanced/resources/hold_spec.rb
104
109
  - spec/balanced/resources/marketplace_spec.rb
@@ -131,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
136
  version: '0'
132
137
  requirements: []
133
138
  rubyforge_project:
134
- rubygems_version: 1.8.23
139
+ rubygems_version: 1.8.24
135
140
  signing_key:
136
141
  specification_version: 3
137
142
  summary: Sign up on https://balancedpayments.com/
@@ -141,6 +146,7 @@ test_files:
141
146
  - spec/balanced/resources/account_spec.rb
142
147
  - spec/balanced/resources/api_key_spec.rb
143
148
  - spec/balanced/resources/bank_account_spec.rb
149
+ - spec/balanced/resources/callback_spec.rb
144
150
  - spec/balanced/resources/credit_spec.rb
145
151
  - spec/balanced/resources/hold_spec.rb
146
152
  - spec/balanced/resources/marketplace_spec.rb