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

Commit

Permalink
Handle the Balanced API more saferly during payday
Browse files Browse the repository at this point in the history
This makes us more robust in the face of failures communicating with the
Balanced API. Now we've got a blanket try/except around the parts where
we talk to Balanced, so we will never totally crash because of a
mishandling of some sort. The ach_credit side is not as well-factored
as the charge_on_balanced side, and is not tested as a result. This
commit nonetheless attempts to implement the same error handling on both
sides.
  • Loading branch information
chadwhitacre committed Feb 20, 2014
1 parent a97d1be commit 28f2fa3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions gittip/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
from __future__ import unicode_literals

import sys
from decimal import Decimal, ROUND_UP

import balanced
Expand Down Expand Up @@ -633,10 +634,14 @@ def ach_credit(self, ts_start, participant, tips, total):
.credit(amount=cents,
description=participant.username)

error = ""
log(msg + "succeeded.")
error = ""
except balanced.exc.HTTPError as err:
error = err.message
error = err.message.message
except:
error = repr(sys.exc_info()[1])

if error:
log(msg + "failed: %s" % error)

self.record_credit(credit_amount, fee, error, participant.username)
Expand All @@ -660,6 +665,10 @@ def charge_on_balanced(self, username, balanced_customer_href, amount):
error = ""
except balanced.exc.HTTPError as err:
error = err.message.message
except:
error = repr(sys.exc_info()[1])

if error:
log(msg + "failed: %s" % error)

return charge_amount, fee, error
Expand Down
4 changes: 2 additions & 2 deletions tests/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ def test_charge_on_balanced_handles_MultipleFoundError(self):
, self.balanced_customer_href
, D('10.00')
)
assert actual == (D('10.61'), D('0.61'), 'MultipleFoundError')
assert actual == (D('10.61'), D('0.61'), 'MultipleResultsFound()')

def test_charge_on_balanced_handles_NotFoundError(self):
customer_with_no_card = unicode(balanced.Customer().save().href)
actual = self.payday.charge_on_balanced( 'whatever username'
, customer_with_no_card
, D('10.00')
)
assert actual == (D('10.61'), D('0.61'), 'NotFoundError')
assert actual == (D('10.61'), D('0.61'), 'NoResultFound()')


class TestBillingCharges(PaydayHarness):
Expand Down

0 comments on commit 28f2fa3

Please sign in to comment.