Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Revert "Modify record_exchange and record_exchange_result to handle a…
Browse files Browse the repository at this point in the history
…ll routes"

This reverts commit c7222ae.
  • Loading branch information
rohitpaulk committed Apr 15, 2015
1 parent fefe657 commit efef589
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
17 changes: 2 additions & 15 deletions gratipay/billing/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,10 @@ def record_exchange(db, route, amount, fee, participant, status, error=''):
RETURNING id
""", (amount, fee, participant.username, status, route.id))

# For payouts, we need to take the fee out of their Gratipay balance.
if amount < 0:
amount -= fee

if status == 'failed':
propagate_exchange(cursor, participant, route, error, 0)
elif route.network == 'balanced-ba':
assert amount < 0
# Always update balance for ach credits, as long as status is not 'failed'
propagate_exchange(cursor, participant, route, '', amount)
elif status == 'succeeded':
# For all other routes, only update balance if status is 'succeeded'
elif amount < 0:
amount -= fee
propagate_exchange(cursor, participant, route, '', amount)

return exchange_id
Expand All @@ -325,13 +317,8 @@ def record_exchange_result(db, exchange_id, status, error, participant):
assert participant.username == username
assert isinstance(route, ExchangeRoute)

# For payouts, we need to take the fee out of their Gratipay balance.
if amount < 0:
amount -= fee

if route.network == 'balanced-ba':
assert amount < 0
# Only restore balance if ach credit fails.
amount = amount if status == 'failed' else 0
propagate_exchange(cursor, participant, route, error, -amount)
else:
Expand Down
44 changes: 22 additions & 22 deletions tests/py/test_billing_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_skim_credit(self):

class TestRecordExchange(Harness):

def test_record_exchange_doesnt_update_balance_for_cc_charges(self):
def test_record_exchange_doesnt_update_balance_for_positive_amounts(self):
alice = self.make_participant('alice', last_bill_result='')
record_exchange( self.db
, ExchangeRoute.from_network(alice, 'balanced-cc')
Expand All @@ -277,7 +277,7 @@ def test_record_exchange_doesnt_update_balance_for_cc_charges(self):
alice = Participant.from_username('alice')
assert alice.balance == D('0.00')

def test_record_exchange_updates_balance_for_ach_credits(self):
def test_record_exchange_updates_balance_for_negative_amounts(self):
alice = self.make_participant('alice', balance=50, last_ach_result='')
record_exchange( self.db
, ExchangeRoute.from_network(alice, 'balanced-ba')
Expand All @@ -289,7 +289,13 @@ def test_record_exchange_updates_balance_for_ach_credits(self):
alice = Participant.from_username('alice')
assert alice.balance == D('13.41')

def test_record_exchange_result_restores_balance_on_ach_credit_failure(self):
def test_record_exchange_fails_if_negative_balance(self):
alice = self.make_participant('alice', last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
with pytest.raises(NegativeBalance):
record_exchange(self.db, ba, D("-10.00"), D("0.41"), alice, 'pre')

def test_record_exchange_result_restores_balance_on_error(self):
alice = self.make_participant('alice', balance=30, last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
e_id = record_exchange(self.db, ba, D('-27.06'), D('0.81'), alice, 'pre')
Expand All @@ -298,7 +304,18 @@ def test_record_exchange_result_restores_balance_on_ach_credit_failure(self):
alice = Participant.from_username('alice')
assert alice.balance == D('30.00')

def test_record_exchange_result_doesnt_update_balance_on_ach_credit_success(self):
def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(self):
alice = self.make_participant('alice', balance=37, last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
e_id = record_exchange(self.db, ba, D('-32.45'), D('0.86'), alice, 'pre')
assert alice.balance == D('3.69')
ba.update_error('invalidated')
record_exchange_result(self.db, e_id, 'failed', 'oops', alice)
alice = Participant.from_username('alice')
assert alice.balance == D('37.00')
assert ba.error == alice.get_bank_account_error() == 'invalidated'

def test_record_exchange_result_doesnt_restore_balance_on_success(self):
alice = self.make_participant('alice', balance=50, last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
e_id = record_exchange(self.db, ba, D('-43.98'), D('1.60'), alice, 'pre')
Expand All @@ -307,7 +324,7 @@ def test_record_exchange_result_doesnt_update_balance_on_ach_credit_success(self
alice = Participant.from_username('alice')
assert alice.balance == D('4.42')

def test_record_exchange_result_updates_balance_for_cc_charges(self):
def test_record_exchange_result_updates_balance_for_positive_amounts(self):
alice = self.make_participant('alice', balance=4, last_bill_result='')
cc = ExchangeRoute.from_network(alice, 'balanced-cc')
e_id = record_exchange(self.db, cc, D('31.59'), D('0.01'), alice, 'pre')
Expand All @@ -316,23 +333,6 @@ def test_record_exchange_result_updates_balance_for_cc_charges(self):
alice = Participant.from_username('alice')
assert alice.balance == D('35.59')

def test_record_exchange_fails_if_negative_balance(self):
alice = self.make_participant('alice', last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
with pytest.raises(NegativeBalance):
record_exchange(self.db, ba, D("-10.00"), D("0.41"), alice, 'pre')

def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(self):
alice = self.make_participant('alice', balance=37, last_ach_result='')
ba = ExchangeRoute.from_network(alice, 'balanced-ba')
e_id = record_exchange(self.db, ba, D('-32.45'), D('0.86'), alice, 'pre')
assert alice.balance == D('3.69')
ba.update_error('invalidated')
record_exchange_result(self.db, e_id, 'failed', 'oops', alice)
alice = Participant.from_username('alice')
assert alice.balance == D('37.00')
assert ba.error == alice.get_bank_account_error() == 'invalidated'


class TestSyncWithBalanced(BalancedHarness):

Expand Down

0 comments on commit efef589

Please sign in to comment.