diff --git a/recurly/__init__.py b/recurly/__init__.py index 74743b3e..f0bbd974 100644 --- a/recurly/__init__.py +++ b/recurly/__init__.py @@ -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 diff --git a/tests/fixtures/adjustment/account-has-adjustments.xml b/tests/fixtures/adjustment/account-has-adjustments.xml index 0c02359c..c3ef5764 100644 --- a/tests/fixtures/adjustment/account-has-adjustments.xml +++ b/tests/fixtures/adjustment/account-has-adjustments.xml @@ -37,5 +37,15 @@ X-Records: 1 2009-11-03T23:27:46Z 2009-11-03T23:27:46Z + + + size + small + + + color + blue + + diff --git a/tests/fixtures/adjustment/charged-with-custom-fields.xml b/tests/fixtures/adjustment/charged-with-custom-fields.xml new file mode 100644 index 00000000..ec5e8cb1 --- /dev/null +++ b/tests/fixtures/adjustment/charged-with-custom-fields.xml @@ -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 + + + + USD + + + size + small + + + color + blue + + + test charge + 1000 + + +HTTP/1.1 201 Created +Content-Type: application/xml; charset=utf-8 +Location: https://api.recurly.com/v2/adjustments/4ba1531325014b4f969cd13676f514d8 + + + + 4ba1531325014b4f969cd13676f514d8 + chargemock + 1000 + + USD + 2009-11-03T23:27:46-08:00 + + test charge + + + size + small + + + color + blue + + + + 2009-11-03T23:27:46-08:00 + diff --git a/tests/fixtures/adjustment/credit-adjustments.xml b/tests/fixtures/adjustment/credit-adjustments.xml new file mode 100644 index 00000000..3292557c --- /dev/null +++ b/tests/fixtures/adjustment/credit-adjustments.xml @@ -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 + + + + 4ba1531325014b4f969cd13676f514d9 + test charge + chargemock + 5000 + usst + CA + 0.0875 + 1000 + USD + + + california + state + 0.065 + 3000 + + + san francisco + county + 0.02 + 2000 + + + 2009-11-03T23:27:46Z + + 2009-11-03T23:27:46Z + + + size + small + + + color + blue + + + + diff --git a/tests/fixtures/adjustment/lookup.xml b/tests/fixtures/adjustment/lookup.xml new file mode 100644 index 00000000..a5cc237d --- /dev/null +++ b/tests/fixtures/adjustment/lookup.xml @@ -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 + + + 4ba1531325014b4f969cd13676f514d8 + test charge + chargemock + 5000 + usst + CA + 0.0875 + 1000 + USD + + + california + state + 0.065 + 3000 + + + san francisco + county + 0.02 + 2000 + + + 2009-11-03T23:27:46Z + + 2009-11-03T23:27:46Z + + + size + small + + + + color + blue + + + diff --git a/tests/test_resources.py b/tests/test_resources.py index c708c836..d8414518 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -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()