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

Commit

Permalink
Modify record_exchange and record_exchange_result to handle all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Apr 9, 2015
1 parent de55183 commit c7222ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
17 changes: 15 additions & 2 deletions gratipay/billing/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,18 @@ 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 amount < 0:
amount -= fee
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'
propagate_exchange(cursor, participant, route, '', amount)

return exchange_id
Expand All @@ -317,8 +325,13 @@ 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_positive_amounts(self):
def test_record_exchange_doesnt_update_balance_for_cc_charges(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_positive_amounts(self):
alice = Participant.from_username('alice')
assert alice.balance == D('0.00')

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

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):
def test_record_exchange_result_restores_balance_on_ach_credit_failure(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 @@ -304,18 +298,7 @@ def test_record_exchange_result_restores_balance_on_error(self):
alice = Participant.from_username('alice')
assert alice.balance == D('30.00')

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):
def test_record_exchange_result_doesnt_update_balance_on_ach_credit_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 @@ -324,7 +307,7 @@ def test_record_exchange_result_doesnt_restore_balance_on_success(self):
alice = Participant.from_username('alice')
assert alice.balance == D('4.42')

def test_record_exchange_result_updates_balance_for_positive_amounts(self):
def test_record_exchange_result_updates_balance_for_cc_charges(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 @@ -333,6 +316,23 @@ def test_record_exchange_result_updates_balance_for_positive_amounts(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 c7222ae

Please sign in to comment.