From 2a8de32cf10a31c474e13a735c2605f2672cab41 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Sun, 14 Oct 2018 08:17:25 -0700 Subject: [PATCH] Add new methods to Invoice resource --- stripe/api_resources/invoice.py | 29 +++++++++++++++++-- tests/api_resources/test_invoice.py | 45 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/stripe/api_resources/invoice.py b/stripe/api_resources/invoice.py index aa1e15af8..834801c09 100644 --- a/stripe/api_resources/invoice.py +++ b/stripe/api_resources/invoice.py @@ -2,20 +2,39 @@ from stripe import api_requestor, util from stripe.api_resources.abstract import CreateableAPIResource +from stripe.api_resources.abstract import DeletableAPIResource from stripe.api_resources.abstract import UpdateableAPIResource from stripe.api_resources.abstract import ListableAPIResource -class Invoice(CreateableAPIResource, ListableAPIResource, - UpdateableAPIResource): +class Invoice(CreateableAPIResource, UpdateableAPIResource, + DeletableAPIResource, ListableAPIResource): OBJECT_NAME = 'invoice' + def finalize_invoice(self, idempotency_key=None, **params): + url = self.instance_url() + '/finalize' + headers = util.populate_headers(idempotency_key) + self.refresh_from(self.request('post', url, params, headers)) + return self + + def mark_uncollectible(self, idempotency_key=None, **params): + url = self.instance_url() + '/mark_uncollectible' + headers = util.populate_headers(idempotency_key) + self.refresh_from(self.request('post', url, params, headers)) + return self + def pay(self, idempotency_key=None, **params): url = self.instance_url() + '/pay' headers = util.populate_headers(idempotency_key) self.refresh_from(self.request('post', url, params, headers)) return self + def send_invoice(self, idempotency_key=None, **params): + url = self.instance_url() + '/send' + headers = util.populate_headers(idempotency_key) + self.refresh_from(self.request('post', url, params, headers)) + return self + @classmethod def upcoming(cls, api_key=None, stripe_version=None, stripe_account=None, **params): @@ -26,3 +45,9 @@ def upcoming(cls, api_key=None, stripe_version=None, stripe_account=None, response, api_key = requestor.request('get', url, params) return util.convert_to_stripe_object(response, api_key, stripe_version, stripe_account) + + def void_invoice(self, idempotency_key=None, **params): + url = self.instance_url() + '/void' + headers = util.populate_headers(idempotency_key) + self.refresh_from(self.request('post', url, params, headers)) + return self diff --git a/tests/api_resources/test_invoice.py b/tests/api_resources/test_invoice.py index bff7c9756..8d5462908 100644 --- a/tests/api_resources/test_invoice.py +++ b/tests/api_resources/test_invoice.py @@ -54,6 +54,33 @@ def test_is_modifiable(self, request_mock): ) assert isinstance(resource, stripe.Invoice) + def test_is_deletable(self, request_mock): + resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) + resource.delete() + request_mock.assert_requested( + 'delete', + '/v1/invoices/%s' % TEST_RESOURCE_ID + ) + assert resource.deleted is True + + def test_can_finalize_invoice(self, request_mock): + resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) + resource = resource.finalize_invoice() + request_mock.assert_requested( + 'post', + '/v1/invoices/%s/finalize' % TEST_RESOURCE_ID + ) + assert isinstance(resource, stripe.Invoice) + + def test_can_mark_uncollectible(self, request_mock): + resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) + resource = resource.mark_uncollectible() + request_mock.assert_requested( + 'post', + '/v1/invoices/%s/mark_uncollectible' % TEST_RESOURCE_ID + ) + assert isinstance(resource, stripe.Invoice) + def test_can_pay(self, request_mock): resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) resource = resource.pay() @@ -63,6 +90,15 @@ def test_can_pay(self, request_mock): ) assert isinstance(resource, stripe.Invoice) + def test_can_send_invoice(self, request_mock): + resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) + resource = resource.send_invoice() + request_mock.assert_requested( + 'post', + '/v1/invoices/%s/send' % TEST_RESOURCE_ID + ) + assert isinstance(resource, stripe.Invoice) + def test_can_upcoming(self, request_mock): resource = stripe.Invoice.upcoming( customer="cus_123" @@ -72,3 +108,12 @@ def test_can_upcoming(self, request_mock): '/v1/invoices/upcoming' ) assert isinstance(resource, stripe.Invoice) + + def test_can_void_invoice(self, request_mock): + resource = stripe.Invoice.retrieve(TEST_RESOURCE_ID) + resource = resource.void_invoice() + request_mock.assert_requested( + 'post', + '/v1/invoices/%s/void' % TEST_RESOURCE_ID + ) + assert isinstance(resource, stripe.Invoice)