From 5ffd66ac12767215c91eb9a1ce360ff7e9d2b309 Mon Sep 17 00:00:00 2001 From: Charlie Denton Date: Thu, 9 Mar 2017 14:59:14 +0000 Subject: [PATCH] Catch InvalidRequestError thrown by invoices.create I've been getting "Nothing to invoice for customer" thrown by the create(customer) call, and this small change fixes the error. --- pinax/stripe/actions/invoices.py | 2 +- pinax/stripe/tests/test_actions.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pinax/stripe/actions/invoices.py b/pinax/stripe/actions/invoices.py index fe5742ab5..b711dc3ee 100644 --- a/pinax/stripe/actions/invoices.py +++ b/pinax/stripe/actions/invoices.py @@ -37,8 +37,8 @@ def create_and_pay(customer): Returns: True, if invoice was created, False if there was an error """ - invoice = create(customer) try: + invoice = create(customer) if invoice.amount_due > 0: invoice.pay() return True diff --git a/pinax/stripe/tests/test_actions.py b/pinax/stripe/tests/test_actions.py index c5109cfd6..364eb75c2 100644 --- a/pinax/stripe/tests/test_actions.py +++ b/pinax/stripe/tests/test_actions.py @@ -341,6 +341,11 @@ def test_create_and_pay_invalid_request_error(self, CreateMock): self.assertFalse(invoices.create_and_pay(Mock())) self.assertTrue(invoice.pay.called) + @patch("stripe.Invoice.create") + def test_create_and_pay_invalid_request_error_on_create(self, CreateMock): + CreateMock.side_effect = stripe.InvalidRequestError("Bad", "error") + self.assertFalse(invoices.create_and_pay(Mock())) + class RefundsTests(TestCase):