Skip to content

Commit

Permalink
Add invoice previews
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarton committed Oct 1, 2014
1 parent afc7254 commit 6174956
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions recurly/__init__.py
Expand Up @@ -190,6 +190,21 @@ def invoice(self):
invoice._url = response.getheader('Location')
return invoice

def build_invoice(self):
"""Preview an invoice for any outstanding adjustments this account has."""
url = urljoin(self._url, '%s/invoices/preview' % self.account_code)

response = self.http_request(url, 'POST')
if response.status != 200:
self.raise_http_error(response)

response_xml = response.read()
logging.getLogger('recurly.http.response').debug(response_xml)
elem = ElementTree.fromstring(response_xml)

invoice = Invoice.from_element(elem)
return invoice

def notes(self):
"""Fetch Notes for this account."""
url = urljoin(self._url, '%s/notes' % self.account_code)
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/invoice/preview-error-no-charges.xml
@@ -0,0 +1,15 @@
POST https://api.recurly.com/v2/accounts/invoicemock/invoices/preview HTTP/1.1
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: recurly-python/{version}
Content-Length: 0


HTTP/1.1 422 Unprocessable Entity
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<error>
<symbol>will_not_invoice</symbol>
<description>No charges to invoice</description>
</error>
22 changes: 22 additions & 0 deletions tests/fixtures/invoice/preview-invoice.xml
@@ -0,0 +1,22 @@
POST https://api.recurly.com/v2/accounts/invoicemock/invoices/preview HTTP/1.1
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: recurly-python/{version}
Content-Length: 0


HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<invoice href="">
<uuid>4ba1531325014b4f969cd13676f514d8</uuid>
<account_code>invoicemock</account_code>
<amount_in_cents>
<USD type="integer">1000</USD>
</amount_in_cents>
<start_date type="datetime">2009-11-03T23:27:46-08:00</start_date>
<end_date type="datetime"></end_date>
<description>test charge</description>
<created_at type="datetime">2009-11-03T23:27:46-08:00</created_at>
</invoice>
25 changes: 25 additions & 0 deletions tests/test_resources.py
Expand Up @@ -541,6 +541,31 @@ def test_invoice(self):
invoice = account.invoices()[0]
self.assertEqual(invoice.tax_type, 'usst')

def test_build_invoice(self):
account = Account(account_code='invoice%s' % self.test_id)
with self.mock_request('invoice/account-created.xml'):
account.save()

try:
with self.mock_request('invoice/preview-error-no-charges.xml'):
try:
account.build_invoice()
except ValidationError as exc:
error = exc
else:
self.fail("Invoicing an account with no charges did not raise a ValidationError")
self.assertEqual(error.symbol, 'will_not_invoice')

charge = Adjustment(unit_amount_in_cents=1000, currency='USD', description='test charge', type='charge')
with self.mock_request('invoice/charged.xml'):
account.charge(charge)

with self.mock_request('invoice/preview-invoice.xml'):
account.build_invoice()
finally:
with self.mock_request('invoice/account-deleted.xml'):
account.delete()

def test_pages(self):
account_code = 'pages-%s-%%d' % self.test_id
all_test_accounts = list()
Expand Down

0 comments on commit 6174956

Please sign in to comment.