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
14 changes: 14 additions & 0 deletions recurly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,20 @@ def __getattr__(self, name):
else:
return super(Adjustment, self).__getattr__(name)

def credit_adjustments(self):
"""A list of credit adjustments that were issued against this adjustment"""
url = recurly.base_uri() + (self.member_path % self.uuid) + '/credit_adjustments'

response = self.http_request(url, 'GET')
if response.status not in (200, 201):
self.raise_http_error(response)
response_xml = response.read()

# This can't be defined at the class level because it is a circular reference
Adjustment._classes_for_nodename['adjustment'] = Adjustment
elem = ElementTree.fromstring(response_xml)
return Adjustment.value_for_element(elem)

class Invoice(Resource):

"""A payable charge to an account for the customer's charges and
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/adjustment/account-has-adjustments.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ X-Records: 1
<start_date type="datetime">2009-11-03T23:27:46Z</start_date>
<end_date nil="nil" type="datetime"></end_date>
<created_at type="datetime">2009-11-03T23:27:46Z</created_at>
<custom_fields type="array">
<custom_field>
<name>size</name>
<value>small</value>
</custom_field>
<custom_field>
<name>color</name>
<value>blue</value>
</custom_field>
</custom_fields>
</adjustment>
</adjustments>
51 changes: 51 additions & 0 deletions tests/fixtures/adjustment/charged-with-custom-fields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
POST https://api.recurly.com/v2/accounts/chargemock/adjustments HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<adjustment type="charge">
<currency>USD</currency>
<custom_fields>
<custom_field>
<name>size</name>
<value>small</value>
</custom_field>
<custom_field>
<name>color</name>
<value>blue</value>
</custom_field>
</custom_fields>
<description>test charge</description>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
</adjustment>

HTTP/1.1 201 Created
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/adjustments/4ba1531325014b4f969cd13676f514d8

<?xml version="1.0" encoding="UTF-8"?>
<adjustment>
<uuid>4ba1531325014b4f969cd13676f514d8</uuid>
<account_code>chargemock</account_code>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<currency>
USD</currency>
<start_date type="datetime">2009-11-03T23:27:46-08:00</start_date>
<end_date type="datetime"></end_date>
<description>test charge</description>
<custom_fields type="array">
<custom_field>
<name>size</name>
<value>small</value>
</custom_field>
<custom_field>
<name>color</name>
<value>blue</value>
</custom_field>
</custom_fields>
<created_at type="datetime">
2009-11-03T23:27:46-08:00</created_at>
</adjustment>
51 changes: 51 additions & 0 deletions tests/fixtures/adjustment/credit-adjustments.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
GET https://api.recurly.com/v2/adjustments/4ba1531325014b4f969cd13676f514d8/credit_adjustments HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}


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

<adjustments type="array">
<adjustment type="charge">
<uuid>4ba1531325014b4f969cd13676f514d9</uuid>
<description>test charge</description>
<account_code>chargemock</account_code>
<tax_in_cents type="integer">5000</tax_in_cents>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.0875</tax_rate>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<currency>USD</currency>
<tax_details type="array">
<tax_detail>
<name>california</name>
<type>state</type>
<tax_rate type="float">0.065</tax_rate>
<tax_in_cents type="integer">3000</tax_in_cents>
</tax_detail>
<tax_detail>
<name>san francisco</name>
<type>county</type>
<tax_rate type="float">0.02</tax_rate>
<tax_in_cents type="integer">2000</tax_in_cents>
</tax_detail>
</tax_details>
<start_date type="datetime"> 2009-11-03T23:27:46Z</start_date>
<end_date nil="nil" type="datetime"></end_date>
<created_at type="datetime">2009-11-03T23:27:46Z</created_at>
<custom_fields type="array">
<custom_field>
<name>size</name>
<value>small</value>
</custom_field>
<custom_field>
<name>color</name>
<value>blue</value>
</custom_field>
</custom_fields>
</adjustment>
</adjustments>
49 changes: 49 additions & 0 deletions tests/fixtures/adjustment/lookup.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
GET https://api.recurly.com/v2/adjustments/4ba1531325014b4f969cd13676f514d8 HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}


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

<adjustment type="charge">
<uuid>4ba1531325014b4f969cd13676f514d8</uuid>
<description>test charge</description>
<account_code>chargemock</account_code>
<tax_in_cents type="integer">5000</tax_in_cents>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.0875</tax_rate>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<currency>USD</currency>
<tax_details type="array">
<tax_detail>
<name>california</name>
<type>state</type>
<tax_rate type="float">0.065</tax_rate>
<tax_in_cents type="integer">3000</tax_in_cents>
</tax_detail>
<tax_detail>
<name>san francisco</name>
<type>county</type>
<tax_rate type="float">0.02</tax_rate>
<tax_in_cents type="integer">2000</tax_in_cents>
</tax_detail>
</tax_details>
<start_date type="datetime">2009-11-03T23:27:46Z</start_date>
<end_date nil="nil" type="datetime"></end_date>
<created_at type="datetime">2009-11-03T23:27:46Z</created_at>
<custom_fields type="array">
<custom_field>
<name>size</name>
<value>small</value>
</custom_field>
<custom_field>
<name>
color</name>
<value>blue</value>
</custom_field>
</custom_fields>
</adjustment>
28 changes: 28 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,34 @@ def test_charge(self):
credits = account.adjustments(type='credit')
self.assertEqual(len(credits), 0)

"""Test custom_fields"""
with self.mock_request('adjustment/charged-with-custom-fields.xml'):
# account = Account.get('chargemock')
charge = Adjustment(
unit_amount_in_cents=1000,
currency='USD',
description='test charge',
type='charge',
custom_fields=[
CustomField(name='size', value='small'),
CustomField(name='color', value='blue'),
],
)
account.charge(charge)
self.assertEqual(charge.custom_fields[0].value, 'small')
self.assertEqual(charge.custom_fields[1].value, 'blue')

with self.mock_request('adjustment/account-has-adjustments.xml'):
adjustments = account.adjustments()
with self.mock_request('adjustment/lookup.xml'):
adjustment = Adjustment.get(adjustments[0].uuid)
self.assertEqual(adjustment.custom_fields[0].value, 'small')
self.assertEqual(adjustment.custom_fields[1].value, 'blue')

with self.mock_request('adjustment/credit-adjustments.xml'):
credits = adjustment.credit_adjustments()
self.assertEqual(len(credits), 1)

finally:
with self.mock_request('adjustment/account-deleted.xml'):
account.delete()
Expand Down