Fix account paid status between subscription payments

master
Alice 8 years ago
parent 381eab7943
commit 1db8f98ebe

@ -44,6 +44,8 @@ class VPNUser(models.Model):
@property @property
def is_paid(self): def is_paid(self):
if self.get_subscription():
return True
if not self.expiration: if not self.expiration:
return False return False
return self.expiration > timezone.now() return self.expiration > timezone.now()

@ -62,6 +62,19 @@ class UserModelTest(TestCase, UserTestMixin):
vu.add_paid_time(p) vu.add_paid_time(p)
self.assertTrue(vu.is_paid) 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): def test_grant_trial(self):
p = timedelta(days=1) p = timedelta(days=1)
u = User.objects.get(username='aaa') u = User.objects.get(username='aaa')

@ -175,15 +175,12 @@ class StripeBackend(BackendBase):
subscr.backend_data['stripe_error'] = e.json_body['error']['message'] subscr.backend_data['stripe_error'] = e.json_body['error']['message']
return return
try: # We don't know much about the new Payment, but we know it
if subscr.status == 'new': # succeeded. Wekhooks aren't very reliable, so let's mark it as active
subscr.status = 'unconfirmed' # anyway.
subscr.backend_extid = cust['id'] subscr.status = 'active'
subscr.save() subscr.backend_extid = cust['id']
except (self.stripe.error.InvalidRequestError, self.stripe.error.CardError) as e: subscr.save()
subscr.status = 'error'
subscr.backend_data['stripe_error'] = e.json_body['error']['message']
subscr.save()
def webhook_payment_succeeded(self, event): def webhook_payment_succeeded(self, event):
from payments.models import Subscription, Payment from payments.models import Subscription, Payment

Loading…
Cancel
Save