balanced 1.1.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +10 -6
  4. data/CHANGELOG.md +4 -0
  5. data/README.md +1 -1
  6. data/lib/balanced/resources.rb +2 -0
  7. data/lib/balanced/resources/account.rb +33 -0
  8. data/lib/balanced/resources/card.rb +1 -1
  9. data/lib/balanced/resources/customer.rb +8 -0
  10. data/lib/balanced/resources/resource.rb +4 -6
  11. data/lib/balanced/resources/settlement.rb +12 -0
  12. data/lib/balanced/response/balanced_exception_middleware.rb +1 -1
  13. data/lib/balanced/utils.rb +1 -1
  14. data/lib/balanced/version.rb +1 -1
  15. data/scenarios/account_credit/definition.rb +1 -0
  16. data/scenarios/account_credit/request.rb +5 -0
  17. data/scenarios/account_credit/ruby.mako +49 -0
  18. data/scenarios/account_list/definition.rb +1 -0
  19. data/scenarios/account_list/request.rb +2 -0
  20. data/scenarios/account_list/ruby.mako +64 -0
  21. data/scenarios/account_list_customer/definition.rb +1 -0
  22. data/scenarios/account_list_customer/request.rb +3 -0
  23. data/scenarios/account_list_customer/ruby.mako +38 -0
  24. data/scenarios/account_show/definition.rb +1 -0
  25. data/scenarios/account_show/request.rb +2 -0
  26. data/scenarios/account_show/ruby.mako +37 -0
  27. data/scenarios/api_key_create/ruby.mako +6 -6
  28. data/scenarios/api_key_delete/ruby.mako +2 -2
  29. data/scenarios/api_key_list/ruby.mako +10 -10
  30. data/scenarios/api_key_show/ruby.mako +6 -6
  31. data/scenarios/bank_account_associate_to_customer/ruby.mako +16 -14
  32. data/scenarios/bank_account_create/ruby.mako +13 -11
  33. data/scenarios/bank_account_credit/ruby.mako +16 -16
  34. data/scenarios/bank_account_debit/ruby.mako +21 -18
  35. data/scenarios/bank_account_debit_order/definition.rb +1 -0
  36. data/scenarios/bank_account_debit_order/request.rb +7 -0
  37. data/scenarios/bank_account_debit_order/ruby.mako +52 -0
  38. data/scenarios/bank_account_delete/ruby.mako +2 -2
  39. data/scenarios/bank_account_list/ruby.mako +26 -22
  40. data/scenarios/bank_account_show/ruby.mako +14 -12
  41. data/scenarios/bank_account_update/ruby.mako +14 -12
  42. data/scenarios/bank_account_verification_create/ruby.mako +10 -10
  43. data/scenarios/bank_account_verification_show/ruby.mako +10 -10
  44. data/scenarios/bank_account_verification_update/ruby.mako +9 -9
  45. data/scenarios/callback_create/ruby.mako +4 -4
  46. data/scenarios/callback_delete/ruby.mako +2 -2
  47. data/scenarios/callback_list/ruby.mako +4 -4
  48. data/scenarios/callback_show/ruby.mako +5 -5
  49. data/scenarios/card_associate_to_customer/ruby.mako +26 -26
  50. data/scenarios/card_create/ruby.mako +13 -15
  51. data/scenarios/card_create_creditable/ruby.mako +11 -11
  52. data/scenarios/card_create_dispute/ruby.mako +13 -13
  53. data/scenarios/card_credit/ruby.mako +16 -14
  54. data/scenarios/card_credit_order/definition.rb +1 -0
  55. data/scenarios/card_credit_order/request.rb +7 -0
  56. data/scenarios/card_credit_order/ruby.mako +45 -0
  57. data/scenarios/card_debit/ruby.mako +20 -17
  58. data/scenarios/card_debit_dispute/ruby.mako +20 -17
  59. data/scenarios/card_delete/ruby.mako +2 -2
  60. data/scenarios/card_hold_capture/ruby.mako +20 -17
  61. data/scenarios/card_hold_create/ruby.mako +16 -14
  62. data/scenarios/card_hold_list/ruby.mako +33 -27
  63. data/scenarios/card_hold_order/definition.rb +1 -0
  64. data/scenarios/card_hold_order/request.rb +6 -0
  65. data/scenarios/card_hold_order/ruby.mako +45 -0
  66. data/scenarios/card_hold_show/ruby.mako +16 -14
  67. data/scenarios/card_hold_update/ruby.mako +16 -14
  68. data/scenarios/card_hold_void/ruby.mako +17 -15
  69. data/scenarios/card_list/ruby.mako +25 -29
  70. data/scenarios/card_show/ruby.mako +14 -16
  71. data/scenarios/card_update/ruby.mako +14 -16
  72. data/scenarios/credit_list/ruby.mako +44 -14
  73. data/scenarios/credit_list_bank_account/ruby.mako +3 -3
  74. data/scenarios/credit_order/request.rb +2 -2
  75. data/scenarios/credit_order/ruby.mako +35 -14
  76. data/scenarios/credit_show/ruby.mako +16 -16
  77. data/scenarios/credit_update/ruby.mako +16 -16
  78. data/scenarios/customer_create/ruby.mako +22 -20
  79. data/scenarios/customer_delete/ruby.mako +1 -1
  80. data/scenarios/customer_list/ruby.mako +43 -39
  81. data/scenarios/customer_show/ruby.mako +23 -21
  82. data/scenarios/customer_update/ruby.mako +23 -21
  83. data/scenarios/debit_dispute_show/ruby.mako +12 -12
  84. data/scenarios/debit_list/ruby.mako +43 -37
  85. data/scenarios/debit_order/ruby.mako +22 -19
  86. data/scenarios/debit_show/ruby.mako +20 -17
  87. data/scenarios/debit_update/ruby.mako +20 -17
  88. data/scenarios/dispute_list/ruby.mako +11 -11
  89. data/scenarios/dispute_show/ruby.mako +12 -12
  90. data/scenarios/event_list/ruby.mako +55 -81
  91. data/scenarios/event_show/ruby.mako +35 -45
  92. data/scenarios/order_create/ruby.mako +16 -14
  93. data/scenarios/order_list/ruby.mako +33 -29
  94. data/scenarios/order_show/ruby.mako +16 -14
  95. data/scenarios/order_update/ruby.mako +16 -14
  96. data/scenarios/refund_create/ruby.mako +13 -13
  97. data/scenarios/refund_list/ruby.mako +12 -12
  98. data/scenarios/refund_show/ruby.mako +13 -13
  99. data/scenarios/refund_update/ruby.mako +13 -13
  100. data/scenarios/reversal_create/ruby.mako +13 -13
  101. data/scenarios/reversal_list/ruby.mako +12 -12
  102. data/scenarios/reversal_show/ruby.mako +13 -13
  103. data/scenarios/reversal_update/ruby.mako +13 -13
  104. data/scenarios/settlement_create/definition.rb +1 -0
  105. data/scenarios/settlement_create/request.rb +5 -0
  106. data/scenarios/settlement_create/ruby.mako +48 -0
  107. data/scenarios/settlement_list/definition.rb +1 -0
  108. data/scenarios/settlement_list/request.rb +2 -0
  109. data/scenarios/settlement_list/ruby.mako +40 -0
  110. data/scenarios/settlement_list_account/definition.rb +1 -0
  111. data/scenarios/settlement_list_account/request.rb +3 -0
  112. data/scenarios/settlement_list_account/ruby.mako +41 -0
  113. data/scenarios/settlement_show/definition.rb +1 -0
  114. data/scenarios/settlement_show/request.rb +2 -0
  115. data/scenarios/settlement_show/ruby.mako +40 -0
  116. data/snippets/account-balance.rb +1 -0
  117. data/snippets/bank-account-create.rb +6 -0
  118. data/snippets/bank-account-debit.rb +9 -0
  119. data/snippets/bank-account-verification-confirm.rb +5 -0
  120. data/snippets/bank-account-verification-create.rb +1 -0
  121. data/snippets/callback-create.rb +4 -0
  122. data/snippets/card-associate-to-customer.rb +2 -0
  123. data/snippets/card-create-dispute.rb +6 -0
  124. data/snippets/card-create.rb +7 -0
  125. data/snippets/card-credit.rb +9 -0
  126. data/snippets/card-debit.rb +9 -0
  127. data/snippets/card-hold-capture.rb +6 -0
  128. data/snippets/card-hold-create.rb +6 -0
  129. data/snippets/card-hold-void.rb +3 -0
  130. data/snippets/create-buyer-and-card.rb +13 -0
  131. data/snippets/credit-create.rb +8 -0
  132. data/snippets/credit-fetch.rb +1 -0
  133. data/snippets/credit-marketplace-escrow.rb +4 -0
  134. data/snippets/credit-reverse.rb +1 -0
  135. data/snippets/credit-soft-descriptor.rb +9 -0
  136. data/snippets/credit-split.rb +13 -0
  137. data/snippets/customer-create.rb +8 -0
  138. data/snippets/debit-dispute-show.rb +3 -0
  139. data/snippets/debit-fetch.rb +1 -0
  140. data/snippets/debit-marketplace-escrow.rb +4 -0
  141. data/snippets/debit-refund.rb +1 -0
  142. data/snippets/dispute-list.rb +1 -0
  143. data/snippets/dispute-show.rb +2 -0
  144. data/snippets/examine-order-after-refund.rb +3 -0
  145. data/snippets/examine-order-after-reversal.rb +3 -0
  146. data/snippets/example.rb +1 -0
  147. data/snippets/marketplace-in-escrow.rb +1 -0
  148. data/snippets/merchant-payable-account-fetch.rb +2 -0
  149. data/snippets/order-amount-escrowed.rb +3 -0
  150. data/snippets/order-bank-account-create.rb +8 -0
  151. data/snippets/order-create.rb +1 -0
  152. data/snippets/order-credit-marketplace.rb +5 -0
  153. data/snippets/order-credit-merchant-payable-account.rb +4 -0
  154. data/snippets/order-credit.rb +4 -0
  155. data/snippets/order-credits-fetch.rb +1 -0
  156. data/snippets/order-debit.rb +4 -0
  157. data/snippets/order-debits-fetch.rb +1 -0
  158. data/snippets/order-fetch.rb +1 -0
  159. data/snippets/order-update.rb +5 -0
  160. data/snippets/refund-create.rb +13 -0
  161. data/snippets/reversal-create.rb +13 -0
  162. data/snippets/settlement-create.rb +5 -0
  163. data/spec/balanced/resources/account_spec.rb +265 -0
  164. data/spec/balanced/resources/card_spec.rb +6 -6
  165. metadata +86 -2
@@ -0,0 +1,2 @@
1
+ card = Balanced::Card.fetch(card_href)
2
+ card.associate_to_customer(customer_href)
@@ -0,0 +1,6 @@
1
+ card = Balanced::Card.new(
2
+ :cvv => '123',
3
+ :expiration_month => '12',
4
+ :expiration_year => '2020',
5
+ :number => '6500000000000002'
6
+ ).save
@@ -0,0 +1,7 @@
1
+ card = Balanced::Card.new(
2
+ :name => 'John Doe',
3
+ :expiration_month => '12',
4
+ :number => '5105105105105100',
5
+ :expiration_year => '2020',
6
+ :security_code => '123'
7
+ ).save
@@ -0,0 +1,9 @@
1
+ # card_href is the stored href for the Card
2
+ # order_href is the stored href for the Order
3
+ card = Balanced::Card.fetch(card_href)
4
+ card.credit(
5
+ :amount => 5000,
6
+ :appears_on_statement_as => 'Some text',
7
+ :description => 'Some descriptive text for the debit in the dashboard',
8
+ :order => order_href
9
+ )
@@ -0,0 +1,9 @@
1
+ # card_href is the stored href for the Card
2
+ # order_href is the stored href for the Order
3
+ card = Balanced::Card.fetch(card_href)
4
+ card.debit(
5
+ :amount => 5000,
6
+ :appears_on_statement_as => 'Statement text',
7
+ :description => 'Some descriptive text for the debit in the dashboard',
8
+ :order => order_href
9
+ )
@@ -0,0 +1,6 @@
1
+ # card_hold_href is the stored href for the CardHold
2
+ card_hold = Balanced::CardHold.fetch(card_hold_href)
3
+ debit = card_hold.capture(
4
+ :description => 'Some descriptive text for the debit in the dashboard',
5
+ :appears_on_statement_as => 'ShowsUpOnStmt'
6
+ )
@@ -0,0 +1,6 @@
1
+ # card_href is the stored href for the Card
2
+ card = Balanced::Card.fetch(card_href)
3
+ card.hold(
4
+ :amount => 5000,
5
+ :description => 'Some descriptive text for the debit in the dashboard'
6
+ )
@@ -0,0 +1,3 @@
1
+ # card_hold_href is the stored href for the CardHold
2
+ card_hold = Balanced::CardHold.fetch(card_hold_href)
3
+ card_hold.unstore
@@ -0,0 +1,13 @@
1
+ buyer = Balanced::Customer.new(
2
+ :name => 'John Buyer'
3
+ ).save
4
+
5
+ card = Balanced::Card.new(
6
+ :number => '5105105105105100',
7
+ :expiration_month => '12',
8
+ :expiration_year => '2020',
9
+ :cvv => '123',
10
+ :name => 'John Buyer'
11
+ ).save
12
+
13
+ card.associate_to_customer(buyer)
@@ -0,0 +1,8 @@
1
+ # bank_account_href is the stored href for the BankAccount
2
+ # order_href is the stored href for the Order
3
+ bank_account = Balanced::BankAccount.fetch(bank_account_href)
4
+ credit = bank_account.credit(
5
+ :amount => 100000,
6
+ :description => 'Payout for order #1111',
7
+ :order => order_href
8
+ )
@@ -0,0 +1 @@
1
+ credit = Balanced::Credit.fetch(credit_href)
@@ -0,0 +1,4 @@
1
+ Balanced::Marketplace.mine.owner_customer.bank_accounts.first.credit(
2
+ :amount => 2000000,
3
+ :description => 'Credit from Balanced escrow'
4
+ )
@@ -0,0 +1 @@
1
+ credit.reverse
@@ -0,0 +1,9 @@
1
+ # bank_account_href is the stored href for the BankAccount
2
+ # order_href is the stored href for the Order
3
+ bank_account = Balanced::BankAccount.fetch(bank_account_href)
4
+ credit = bank_account.credit(
5
+ :amount => 100000,
6
+ :description => 'Payout for order #1111',
7
+ :appears_on_statement_as => 'GoodCo #1111',
8
+ :order => order_href
9
+ )
@@ -0,0 +1,13 @@
1
+ # bank_account_href_a is the stored href for the BankAccount for Person A
2
+ bank_account_person_a = Balanced::BankAccount.fetch(bank_account_href_a)
3
+ credit = bank_account_person_a.credit(
4
+ :amount => 50000,
5
+ :description => 'Payout for order #1111'
6
+ )
7
+
8
+ # bank_account_href_b is the stored href for the BankAccount for Person B
9
+ bank_account_person_b = Balanced::BankAccount.fetch(bank_account_href_b)
10
+ credit = bank_account_person_b.credit(
11
+ :amount => 50000,
12
+ :description => 'Payout for order #1111'
13
+ )
@@ -0,0 +1,8 @@
1
+ merchant = Balanced::Customer.new(
2
+ :name => 'Henry Ford',
3
+ :dob_month => 7,
4
+ :dob_year => 1963,
5
+ :address => {
6
+ :postal_code => '48120'
7
+ }
8
+ ).save
@@ -0,0 +1,3 @@
1
+ # debit_href is the stored href of the debit
2
+ debit = Balanced::Debit.fetch(debit_href)
3
+ dispute = debit.dispute
@@ -0,0 +1 @@
1
+ debit = Balanced::Debit.fetch(debit_href)
@@ -0,0 +1,4 @@
1
+ Balanced::Marketplace.mine.owner_customer.bank_accounts.first.debit(
2
+ :amount => 2000000,
3
+ :description => 'Pre-fund Balanced escrow'
4
+ )
@@ -0,0 +1 @@
1
+ debit.refund
@@ -0,0 +1 @@
1
+ disputes = Balanced::Dispute.all
@@ -0,0 +1,2 @@
1
+ # dispute_href is the stored href of the dispute
2
+ dispute = Balanced::Dispute.fetch(dispute_href)
@@ -0,0 +1,3 @@
1
+ order = Balanced::Order.find(order_href)
2
+ order.amount # original order amount
3
+ order.amount_escrowed # will decrease by amount of refunded debit
@@ -0,0 +1,3 @@
1
+ order = Balanced::Order.find(order_href)
2
+ order.amount # original order amount
3
+ order.amount_escrowed # will increase by amount of reversed credit
@@ -0,0 +1 @@
1
+ require 'balanced'
@@ -0,0 +1 @@
1
+ Balanced::Marketplace.mine.in_escrow
@@ -0,0 +1,2 @@
1
+ # merchant is a Customer instance
2
+ merchant.payable_account
@@ -0,0 +1,3 @@
1
+ order.reload # reload the order to get recent changes
2
+ order.amount
3
+ order.amount_escrowed
@@ -0,0 +1,8 @@
1
+ bank_account = Balanced::BankAccount.new(
2
+ :name => 'Henry Ford',
3
+ :routing_number => '321174851',
4
+ :account_number => '0987654321',
5
+ :type => 'checking'
6
+ ).save
7
+
8
+ bank_account.associate_to_customer(merchant)
@@ -0,0 +1 @@
1
+ order = merchant.create_order
@@ -0,0 +1,5 @@
1
+ marketplace_bank_account = Balanced::Marketplace.mine.owner_customer.bank_accounts.first
2
+ order.credit_to(
3
+ :destination => marketplace_bank_account,
4
+ :amount => 2000
5
+ )
@@ -0,0 +1,4 @@
1
+ order.credit_to(
2
+ :destination => account_href,
3
+ :amount => 8000
4
+ )
@@ -0,0 +1,4 @@
1
+ order.credit_to(
2
+ :destination => bank_account,
3
+ :amount => 8000
4
+ )
@@ -0,0 +1 @@
1
+ order.credits # instance of Balanced::Pager. Lazily iterate as an array.
@@ -0,0 +1,4 @@
1
+ debit = order.debit_from(
2
+ :source => card,
3
+ :amount => 10000
4
+ )
@@ -0,0 +1 @@
1
+ order.debits # instance of Balanced::Pager. Lazily iterate as an array.
@@ -0,0 +1 @@
1
+ order = Balanced::Order.fetch(order_href)
@@ -0,0 +1,5 @@
1
+ order.description = 'Item description'
2
+ order.meta = {
3
+ 'item_url' => 'https://neatitems.com/12342134123'
4
+ }
5
+ order.save
@@ -0,0 +1,13 @@
1
+ # debit_href is the stored href for the Debit
2
+ # order_href is the stored href for the Order
3
+ debit = Balanced::Debit.fetch(debit_href)
4
+ debit.refund(
5
+ :amount => 3000,
6
+ :description => 'Refund for Order #1111',
7
+ :meta => {
8
+ 'merchant.feedback' => 'positive',
9
+ 'fulfillment.item.condition' => 'OK',
10
+ 'user.refund_reason' => 'not happy with product'
11
+ },
12
+ :order => order_href
13
+ )
@@ -0,0 +1,13 @@
1
+ # credit_href is the stored href for the Credit
2
+ # order_href is the stored href for the Order
3
+ credit = Balanced::Credit.fetch(credit_href)
4
+ reversal = credit.reverse(
5
+ :amount => 100000,
6
+ :description => 'Reversal for Order #1111',
7
+ :meta => {
8
+ 'merchant.feedback' => 'positive',
9
+ 'fulfillment.item.condition' => 'OK',
10
+ 'user.refund_reason' => 'not happy with product'
11
+ },
12
+ :order => order_href
13
+ )
@@ -0,0 +1,5 @@
1
+ account.settle(
2
+ :appears_on_statement_as => 'ThingsCo',
3
+ :description => 'A simple description',
4
+ :funding_instrument => bank_account_href
5
+ )
@@ -0,0 +1,265 @@
1
+ require 'spec_helper'
2
+
3
+ describe Balanced::Account, :vcr, :marketplace => true do
4
+
5
+ describe 'credit funds from order to account' do
6
+ before do
7
+ # create seller
8
+ @merchant = Balanced::Customer.new(
9
+ :name => 'Henry Ford',
10
+ :dob_month => 7,
11
+ :dob_year => 1963,
12
+ :address => {
13
+ :postal_code => '48120'
14
+ }
15
+ ).save
16
+
17
+ # create buyer
18
+ buyer = Balanced::Customer.new(
19
+ :name => 'John Buyer'
20
+ ).save
21
+ @card = buyer.cards.create(
22
+ :number => '5105105105105100',
23
+ :expiration_month => '12',
24
+ :expiration_year => '2020',
25
+ :cvv => '123'
26
+ )
27
+
28
+ # create order
29
+ @order = @merchant.create_order
30
+
31
+ # set amount for debit
32
+ @amount = 10000
33
+ end
34
+
35
+ it 'should increase an account balance when credited' do
36
+ @order.debit_from(
37
+ :source => @card,
38
+ :amount => @amount
39
+ )
40
+
41
+ account_credit = @merchant.payable_account.credit(
42
+ :amount => @amount,
43
+ :order => @order
44
+ )
45
+
46
+ @merchant.payable_account.balance.should eq @amount
47
+ account_credit.status.should eq 'succeeded'
48
+
49
+ end
50
+ end
51
+
52
+ describe "settling an account" do
53
+ before do
54
+ # create seller
55
+ @merchant = Balanced::Customer.new(
56
+ :name => 'Henry Ford',
57
+ :dob_month => 7,
58
+ :dob_year => 1963,
59
+ :address => {
60
+ :postal_code => '48120'
61
+ }
62
+ ).save
63
+ @bank_account = @merchant.bank_accounts.create(
64
+ :name => 'William Henry Cavendish III',
65
+ :routing_number => '321174851',
66
+ :account_number => '0987654321',
67
+ :type => 'checking'
68
+ )
69
+ @bank_account.associate_to_customer(@merchant)
70
+
71
+ # create buyer
72
+ buyer = Balanced::Customer.new(
73
+ :name => 'John Buyer'
74
+ ).save
75
+ @card = buyer.cards.create(
76
+ :number => '5105105105105100',
77
+ :expiration_month => '12',
78
+ :expiration_year => '2020',
79
+ :cvv => '123'
80
+ )
81
+
82
+ # create order
83
+ @order = @merchant.create_order
84
+
85
+ # set amount for debit
86
+ @amount = 10000
87
+
88
+ # debit into the order
89
+ debit = @order.debit_from(
90
+ :source => @card,
91
+ :amount => @amount
92
+ )
93
+
94
+ # credit the account
95
+ account_credit = @merchant.payable_account.credit(
96
+ :amount => @amount,
97
+ :order => @order
98
+ )
99
+ end
100
+
101
+ it 'reduces the account balance to zero' do
102
+ # settle the account
103
+ settlement = @merchant.payable_account.settle(
104
+ :funding_instrument => @bank_account,
105
+ :appears_on_statement_as => "Settlement Oct",
106
+ :description => "Settlement for payouts from October"
107
+ )
108
+
109
+ settlement.amount.should eq @amount
110
+ settlement.appears_on_statement_as.should eq "BAL*Settlement Oct"
111
+ settlement.description.should eq "Settlement for payouts from October"
112
+ @merchant.payable_account.balance.should eq 0
113
+ end
114
+ end
115
+
116
+ describe 'reversing a settlement' do
117
+ before do
118
+ # create seller
119
+ @merchant = Balanced::Customer.new(
120
+ :name => 'Henry Ford',
121
+ :dob_month => 7,
122
+ :dob_year => 1963,
123
+ :address => {
124
+ :postal_code => '48120'
125
+ }
126
+ ).save
127
+ @bank_account = @merchant.bank_accounts.create(
128
+ :name => 'William Henry Cavendish III',
129
+ :routing_number => '321174851',
130
+ :account_number => '0987654321',
131
+ :type => 'checking'
132
+ )
133
+ @bank_account.associate_to_customer(@merchant)
134
+
135
+ # create buyer
136
+ buyer = Balanced::Customer.new(
137
+ :name => 'John Buyer'
138
+ ).save
139
+ @card = buyer.cards.create(
140
+ :number => '5105105105105100',
141
+ :expiration_month => '12',
142
+ :expiration_year => '2020',
143
+ :cvv => '123'
144
+ )
145
+
146
+ # create order
147
+ @order = @merchant.create_order
148
+
149
+ # set amount for debit
150
+ @amount = 10000
151
+
152
+ # debit into the order
153
+ @order.debit_from(
154
+ :source => @card,
155
+ :amount => @amount
156
+ )
157
+
158
+ # credit the account
159
+ @account_credit = @merchant.payable_account.credit(
160
+ :amount => @amount,
161
+ :order => @order
162
+ )
163
+
164
+ # settle the account
165
+ @settlement = @merchant.payable_account.settle(
166
+ :funding_instrument => @bank_account.href,
167
+ :appears_on_statement_as => "Settlement Oct",
168
+ :description => "Settlement for payouts from October"
169
+ )
170
+ end
171
+
172
+ it 'credit should be reversible when account has pending balance' do
173
+ order_two = @merchant.create_order
174
+ order_two.debit_from(
175
+ :source => @card,
176
+ :amount => @amount
177
+ )
178
+
179
+ credit = @merchant.payable_account.credit(
180
+ :amount => @amount,
181
+ :order => order_two
182
+ )
183
+ @merchant.payable_account.balance.should eq @amount
184
+
185
+ credit.reverse(:amount => @amount)
186
+
187
+ #@payable_account.reload
188
+ @merchant.payable_account.balance.should eq 0
189
+
190
+ end
191
+ end
192
+
193
+ describe 'settling an account with an negative balance' do
194
+ before do
195
+
196
+ # create seller
197
+ @merchant = Balanced::Customer.new(
198
+ :name => 'Henry Ford',
199
+ :dob_month => 7,
200
+ :dob_year => 1963,
201
+ :address => {
202
+ :postal_code => '48120'
203
+ }
204
+ ).save
205
+ @bank_account = @merchant.bank_accounts.create(
206
+ :name => 'William Henry Cavendish III',
207
+ :routing_number => '321174851',
208
+ :account_number => '0987654321',
209
+ :type => 'checking'
210
+ )
211
+ @bank_account.associate_to_customer(@merchant)
212
+
213
+ # create buyer
214
+ buyer = Balanced::Customer.new(
215
+ :name => 'John Buyer'
216
+ ).save
217
+ @card = buyer.cards.create(
218
+ :number => '5105105105105100',
219
+ :expiration_month => '12',
220
+ :expiration_year => '2020',
221
+ :cvv => '123'
222
+ )
223
+
224
+ # create order
225
+ @order = @merchant.create_order
226
+
227
+ # set amount for debit
228
+ @amount = 10000
229
+
230
+ # debit into the order
231
+ @order.debit_from(
232
+ :source => @card,
233
+ :amount => @amount
234
+ )
235
+
236
+ # credit the account
237
+ @account_credit = @merchant.payable_account.credit(
238
+ :amount => @amount,
239
+ :order => @order
240
+ )
241
+
242
+ # settle the account
243
+ @settlement = @merchant.payable_account.settle(
244
+ :funding_instrument => @bank_account,
245
+ :appears_on_statement_as => "BAL*Settlement Oct",
246
+ :description => "Settlement for payouts from October"
247
+ )
248
+ end
249
+
250
+ it 'should create reversal and refund' do
251
+ @account_credit.reverse(:amount => @amount)
252
+
253
+ @merchant.payable_account.reload
254
+ @merchant.payable_account.balance.should eq -@amount
255
+
256
+ @settlement = @merchant.payable_account.settle(
257
+ :funding_instrument => @bank_account,
258
+ :appears_on_statement_as => "Reverse Settlement Oct",
259
+ :description => "Settlement for payouts from October"
260
+ )
261
+
262
+ @merchant.payable_account.balance.should eq 0
263
+ end
264
+ end
265
+ end