From 1db8f98ebefda2bd2ef51c8f8b5c597553c0457d Mon Sep 17 00:00:00 2001 From: Alice Date: Mon, 16 Jan 2017 02:24:46 +0000 Subject: [PATCH] Fix account paid status between subscription payments --- lambdainst/models.py | 2 ++ lambdainst/tests.py | 13 +++++++++++++ payments/backends/stripe.py | 15 ++++++--------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lambdainst/models.py b/lambdainst/models.py index 5d54e91..0bf225d 100644 --- a/lambdainst/models.py +++ b/lambdainst/models.py @@ -44,6 +44,8 @@ class VPNUser(models.Model): @property def is_paid(self): + if self.get_subscription(): + return True if not self.expiration: return False return self.expiration > timezone.now() diff --git a/lambdainst/tests.py b/lambdainst/tests.py index 05c0610..f26d530 100644 --- a/lambdainst/tests.py +++ b/lambdainst/tests.py @@ -62,6 +62,19 @@ class UserModelTest(TestCase, UserTestMixin): vu.add_paid_time(p) self.assertTrue(vu.is_paid) + def test_paid_between_subscr_payments(self): + u = User.objects.get(username='aaa') + vu = u.vpnuser + s = Subscription(user=u, backend_id='paypal', status='new') + s.save() + + self.assertFalse(vu.is_paid) + + s.status = 'active' + s.save() + + self.assertTrue(vu.is_paid) + def test_grant_trial(self): p = timedelta(days=1) u = User.objects.get(username='aaa') diff --git a/payments/backends/stripe.py b/payments/backends/stripe.py index 597704a..e77bc28 100644 --- a/payments/backends/stripe.py +++ b/payments/backends/stripe.py @@ -175,15 +175,12 @@ class StripeBackend(BackendBase): subscr.backend_data['stripe_error'] = e.json_body['error']['message'] return - try: - if subscr.status == 'new': - subscr.status = 'unconfirmed' - subscr.backend_extid = cust['id'] - subscr.save() - except (self.stripe.error.InvalidRequestError, self.stripe.error.CardError) as e: - subscr.status = 'error' - subscr.backend_data['stripe_error'] = e.json_body['error']['message'] - subscr.save() + # We don't know much about the new Payment, but we know it + # succeeded. Wekhooks aren't very reliable, so let's mark it as active + # anyway. + subscr.status = 'active' + subscr.backend_extid = cust['id'] + subscr.save() def webhook_payment_succeeded(self, event): from payments.models import Subscription, Payment