Skip to content

Commit 016a902

Browse files
authored
DP-311: Add new list invoices endpoint (#9)
* Add new list invoices endpoint * Python 2.7 didn't agree...
1 parent 9edc890 commit 016a902

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ import chartmogul
187187

188188
chartmogul.Invoice.create(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={})
189189
chartmogul.Invoice.all(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', page=2, per_page=10)
190+
chartmogul.Invoice.all(config, customer_uuid='cus_f466e33d-ff2b-4a11-8f85-417eb02157a7', external_id='INV0001')
190191
```
191192

192193
#### [Transactions](https://dev.chartmogul.com/docs/transactions)

chartmogul/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"""
3030

3131
__title__ = 'chartmogul'
32-
__version__ = '1.1.1'
32+
__version__ = '1.1.2'
3333
__build__ = 0x000000
3434
__author__ = 'ChartMogul Ltd'
3535
__license__ = 'MIT'

chartmogul/api/invoice.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Invoice(Resource):
3939

4040
class _Schema(Schema):
4141
uuid = fields.String()
42+
customer_uuid = fields.String(allow_none=True)
4243
external_id = fields.String(allow_none=True)
4344
date = fields.DateTime()
4445
due_date = fields.DateTime(allow_none=True)
@@ -51,3 +52,16 @@ def make(self, data):
5152
return Invoice(**data)
5253

5354
_schema = _Schema(strict=True)
55+
56+
@classmethod
57+
def all(cls, config, **kwargs):
58+
"""
59+
Actually uses two different endpoints, where it dispatches the call depends on whether
60+
customer uuid is given with the old parameter name ('uuid') or not.
61+
"""
62+
if 'uuid' in kwargs:
63+
return super(Invoice, cls).all(config, **kwargs)
64+
else:
65+
return cls.all_any(config, **kwargs)
66+
67+
Invoice.all_any = Invoice._method('all', 'get', '/invoices')

test/api/test_invoice.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,61 @@
145145
]
146146
}
147147

148+
newInvoiceListExample = """
149+
{
150+
"invoices": [
151+
{
152+
"uuid": "inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9",
153+
"customer_uuid": "cus_f466e33d-ff2b-4a11-8f85-417eb02157a7",
154+
"external_id": "INV0001",
155+
"date": "2015-11-01T00:00:00.000Z",
156+
"due_date": "2015-11-15T00:00:00.000Z",
157+
"currency": "USD",
158+
"line_items": [
159+
{
160+
"uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56",
161+
"external_id": null,
162+
"type": "subscription",
163+
"subscription_uuid": "sub_e6bc5407-e258-4de0-bb43-61faaf062035",
164+
"plan_uuid": "pl_eed05d54-75b4-431b-adb2-eb6b9e543206",
165+
"prorated": false,
166+
"service_period_start": "2015-11-01T00:00:00.000Z",
167+
"service_period_end": "2015-12-01T00:00:00.000Z",
168+
"amount_in_cents": 5000,
169+
"quantity": 1,
170+
"discount_code": "PSO86",
171+
"discount_amount_in_cents": 1000,
172+
"tax_amount_in_cents": 900,
173+
"account_code": null
174+
},
175+
{
176+
"uuid": "li_0cc8c112-beac-416d-af11-f35744ca4e83",
177+
"external_id": null,
178+
"type": "one_time",
179+
"description": "Setup Fees",
180+
"amount_in_cents": 2500,
181+
"quantity": 1,
182+
"discount_code": "PSO86",
183+
"discount_amount_in_cents": 500,
184+
"tax_amount_in_cents": 450,
185+
"account_code": null
186+
}
187+
],
188+
"transactions": [
189+
{
190+
"uuid": "tr_879d560a-1bec-41bb-986e-665e38a2f7bc",
191+
"external_id": null,
192+
"type": "payment",
193+
"date": "2015-11-05T00:14:23.000Z",
194+
"result": "successful"
195+
}
196+
]
197+
}
198+
],
199+
"current_page": 1,
200+
"total_pages": 1
201+
}
202+
"""
148203

149204
class InvoiceTestCase(unittest.TestCase):
150205
"""
@@ -192,4 +247,38 @@ def test_list_has_customer_uuid(self, mock_requests):
192247
# Struct too complex to do 1:1 comparison
193248
self.assertTrue(isinstance(result, Invoice._many))
194249
self.assertEqual(len(result.invoices), 1)
250+
self.assertTrue(isinstance(result.invoices[0], Invoice))
251+
self.assertEqual(result.invoices[0].uuid, "inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9")
195252
self.assertEqual(result.customer_uuid, 'UUID')
253+
254+
@requests_mock.mock()
255+
def test_new_list(self, mock_requests):
256+
257+
mock_requests.register_uri(
258+
'GET',
259+
("https://api.chartmogul.com/v1/invoices"
260+
"?external_id=INV0001&customer_uuid=cus_f466e33d-ff2b-4a11-8f85-417eb02157a7"),
261+
request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'},
262+
headers={'Content-Type': 'application/json'},
263+
status_code=200,
264+
text=newInvoiceListExample
265+
)
266+
267+
config = Config("token", "secret") # is actually checked in mock
268+
result = Invoice.all(config,
269+
customer_uuid='cus_f466e33d-ff2b-4a11-8f85-417eb02157a7',
270+
external_id='INV0001').get()
271+
272+
self.assertEqual(mock_requests.call_count, 1, "expected call")
273+
cu = []
274+
cu.append('cus_f466e33d-ff2b-4a11-8f85-417eb02157a7')
275+
ei = []
276+
ei.append('inv0001')
277+
self.assertEqual(mock_requests.last_request.qs, {'customer_uuid': cu,'external_id': ei})
278+
# Struct too complex to do 1:1 comparison
279+
self.assertTrue(isinstance(result, Invoice._many))
280+
self.assertEqual(len(result.invoices), 1)
281+
282+
self.assertEqual(result.invoices[0].customer_uuid, 'cus_f466e33d-ff2b-4a11-8f85-417eb02157a7')
283+
self.assertEqual(result.current_page, 1)
284+
self.assertEqual(result.total_pages, 1)

0 commit comments

Comments
 (0)