forked from GithubBackups/healthchecks
Delete customer from Braintree when closing account.
This commit is contained in:
parent
fa16bd4e42
commit
2489f86b38
@ -6,10 +6,12 @@ from mock import patch
|
|||||||
|
|
||||||
|
|
||||||
class CloseAccountTestCase(BaseTestCase):
|
class CloseAccountTestCase(BaseTestCase):
|
||||||
@patch("hc.payments.models.Subscription.cancel")
|
@patch("hc.payments.models.braintree")
|
||||||
def test_it_works(self, mock_cancel):
|
def test_it_works(self, mock_braintree):
|
||||||
Check.objects.create(project=self.project, tags="foo a-B_1 baz@")
|
Check.objects.create(project=self.project, tags="foo a-B_1 baz@")
|
||||||
Subscription.objects.create(user=self.alice, subscription_id="123")
|
Subscription.objects.create(
|
||||||
|
user=self.alice, subscription_id="123", customer_id="fake-customer-id"
|
||||||
|
)
|
||||||
|
|
||||||
self.client.login(username="alice@example.org", password="password")
|
self.client.login(username="alice@example.org", password="password")
|
||||||
r = self.client.post("/accounts/close/")
|
r = self.client.post("/accounts/close/")
|
||||||
@ -27,7 +29,10 @@ class CloseAccountTestCase(BaseTestCase):
|
|||||||
self.assertFalse(Check.objects.exists())
|
self.assertFalse(Check.objects.exists())
|
||||||
|
|
||||||
# Subscription should have been canceled
|
# Subscription should have been canceled
|
||||||
self.assertTrue(mock_cancel.called)
|
self.assertTrue(mock_braintree.Subscription.cancel.called)
|
||||||
|
|
||||||
|
# Braintree customer should have been deleted
|
||||||
|
self.assertTrue(mock_braintree.Customer.delete.called)
|
||||||
|
|
||||||
# Subscription should be gone
|
# Subscription should be gone
|
||||||
self.assertFalse(Subscription.objects.exists())
|
self.assertFalse(Subscription.objects.exists())
|
||||||
|
@ -457,7 +457,7 @@ def close(request):
|
|||||||
# Subscription needs to be canceled before it is deleted:
|
# Subscription needs to be canceled before it is deleted:
|
||||||
sub = Subscription.objects.filter(user=user).first()
|
sub = Subscription.objects.filter(user=user).first()
|
||||||
if sub:
|
if sub:
|
||||||
sub.cancel()
|
sub.cancel(delete_customer=True)
|
||||||
|
|
||||||
user.delete()
|
user.delete()
|
||||||
|
|
||||||
|
@ -136,11 +136,15 @@ class Subscription(models.Model):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self, delete_customer=False):
|
||||||
if self.subscription_id:
|
if self.subscription_id:
|
||||||
braintree.Subscription.cancel(self.subscription_id)
|
braintree.Subscription.cancel(self.subscription_id)
|
||||||
|
self.subscription_id = ""
|
||||||
|
|
||||||
|
if self.customer_id and delete_customer:
|
||||||
|
braintree.Customer.delete(self.customer_id)
|
||||||
|
self.customer_id = ""
|
||||||
|
|
||||||
self.subscription_id = ""
|
|
||||||
self.plan_id = ""
|
self.plan_id = ""
|
||||||
self.plan_name = ""
|
self.plan_name = ""
|
||||||
self.save()
|
self.save()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user