Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions recurly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class AccountBalance(Resource):
attributes = (
'balance_in_cents',
'processing_prepayment_balance_in_cents',
'available_credit_balance_in_cents',
'past_due',
)

Expand Down Expand Up @@ -1140,6 +1141,16 @@ def force_collect(self, options={}):
invoice_collection = InvoiceCollection.from_element(elem)
return invoice_collection

def apply_credit_balance(self):
url = urljoin(self._url, '/apply_credit_balance')
response = self.http_request(url, 'PUT')
if response.status not in (200, 201):
self.raise_http_error(response)
response_xml = response.read()
elem = ElementTree.fromstring(response_xml)
invoice = Invoice.from_element(elem)
return invoice

def _create_refund_invoice(self, element):
url = urljoin(self._url, '/refund')
body = ElementTree.tostring(element, encoding='UTF-8')
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/account-balance/exists.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ Content-Type: application/xml; charset=utf-8
<USD type="integer">-3000</USD>
<EUR type="integer">0</EUR>
</processing_prepayment_balance_in_cents>
<available_credit_balance_in_cents>
<USD type="integer">-3000</USD>
<EUR type="integer">0</EUR>
</available_credit_balance_in_cents>
</account_balance>
70 changes: 70 additions & 0 deletions tests/fixtures/invoice/apply-credit-balance.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
PUT https://api.recurly.com/v2/invoices/6019/apply_credit_balance HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}
Content-Length: 0


HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/invoices/6019

<?xml version="1.0" encoding="UTF-8"?>
<invoice href="https://api.recurly.com/v2/invoices/6019">
<account href="https://api.recurly.com/v2/accounts/aa463d59-a618-4b71-b1f4-0410f835fe74"/>
<address>
<address1 nil="nil"></address1>
<address2 nil="nil"></address2>
<city nil="nil"></city>
<state nil="nil"></state>
<zip nil="nil"></zip>
<country nil="nil"></country>
<phone nil="nil"></phone>
</address>
<uuid>46036dca820357dae40c94420ab52632</uuid>
<state>paid</state>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number type="integer">6019</invoice_number>
<vat_number nil="nil"></vat_number>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">5000</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2018-07-13T17:13:57Z</created_at>
<updated_at type="datetime">2018-07-13T17:13:57Z</updated_at>
<attempt_next_collection_at nil="nil"></attempt_next_collection_at>
<closed_at nil="nil">2019-07-13T17:13:57Z</closed_at>
<customer_notes>notes</customer_notes>
<recovery_reason nil="nil"></recovery_reason>
<net_terms type="integer">0</net_terms>
<collection_method>manual</collection_method>
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/46036dc9823500f96f43ef44769df449" type="charge">
<account href="https://api.recurly.com/v2/accounts/aa463d59-a618-4b71-b1f4-0410f835fe74"/>
<invoice href="https://api.recurly.com/v2/invoices/6019"/>
<uuid>46036dc9823500f96f43ef44769df449</uuid>
<state>invoiced</state>
<description>Charge for extra bandwidth</description>
<accounting_code>bandwidth</accounting_code>
<product_code nil="nil"></product_code>
<origin>debit</origin>
<unit_amount_in_cents type="integer">5000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">5000</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<tax_exempt type="boolean">false</tax_exempt>
<tax_code nil="nil"></tax_code>
<start_date type="datetime">2018-07-13T17:13:57Z</start_date>
<end_date nil="nil"></end_date>
<created_at type="datetime">2018-07-13T17:13:57Z</created_at>
<updated_at type="datetime">2018-07-13T17:13:57Z</updated_at>
<revenue_schedule_type></revenue_schedule_type>
</adjustment>
</line_items>
<transactions type="array">
</transactions>
<a name="refund" href="https://api.recurly.com/v2/invoices/6019/refund" method="post"/>
</invoice>
1 change: 1 addition & 0 deletions tests/fixtures/invoice/show-invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ Location: https://api.recurly.com/v2/invoices/6019
</transactions>
<a name="mark_successful" href="https://api.recurly.com/v2/invoices/6019/mark_successful" method="put"/>
<a name="mark_failed" href="https://api.recurly.com/v2/invoices/6019/mark_failed" method="put"/>
<a name="apply_credit_balance" href="https://api.recurly.com/v2/invoices/6019/apply_credit_balance" method="put"/>
</invoice>
11 changes: 11 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ def test_account(self):
processing_prepayment_balance = account_balance.processing_prepayment_balance_in_cents
self.assertTrue(processing_prepayment_balance['USD'] == -3000)
self.assertTrue(processing_prepayment_balance['EUR'] == 0)
available_credit_balance_in_cents = account_balance.available_credit_balance_in_cents
self.assertTrue(available_credit_balance_in_cents['USD'] == -3000)
self.assertTrue(available_credit_balance_in_cents['EUR'] == 0)

account.username = 'shmohawk58'
account.email = 'larry.david'
Expand Down Expand Up @@ -1397,6 +1400,14 @@ def test_invoice_collect(self):
collection = invoice.force_collect()
self.assertIsInstance(collection, InvoiceCollection)

def test_apply_credit_balance(self):
with self.mock_request('invoice/show-invoice.xml'):
invoice = Invoice.get("6019")

with self.mock_request('invoice/apply-credit-balance.xml'):
updated_invoice = invoice.apply_credit_balance()
self.assertIsInstance(updated_invoice, Invoice)

def test_invoice_tax_details(self):
with self.mock_request('invoice/show-invoice.xml'):
invoice = Invoice.get("6019")
Expand Down