From 2489f86b38a7ed1964f79cb3cf7c03933c3229a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 19 Aug 2019 11:47:36 +0300 Subject: [PATCH] Delete customer from Braintree when closing account. --- hc/accounts/tests/test_close_account.py | 13 +++++++++---- hc/accounts/views.py | 2 +- hc/payments/models.py | 8 ++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/hc/accounts/tests/test_close_account.py b/hc/accounts/tests/test_close_account.py index f9f94350..d51931df 100644 --- a/hc/accounts/tests/test_close_account.py +++ b/hc/accounts/tests/test_close_account.py @@ -6,10 +6,12 @@ from mock import patch class CloseAccountTestCase(BaseTestCase): - @patch("hc.payments.models.Subscription.cancel") - def test_it_works(self, mock_cancel): + @patch("hc.payments.models.braintree") + def test_it_works(self, mock_braintree): 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") r = self.client.post("/accounts/close/") @@ -27,7 +29,10 @@ class CloseAccountTestCase(BaseTestCase): self.assertFalse(Check.objects.exists()) # 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 self.assertFalse(Subscription.objects.exists()) diff --git a/hc/accounts/views.py b/hc/accounts/views.py index f434b5cb..39ac353a 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -457,7 +457,7 @@ def close(request): # Subscription needs to be canceled before it is deleted: sub = Subscription.objects.filter(user=user).first() if sub: - sub.cancel() + sub.cancel(delete_customer=True) user.delete() diff --git a/hc/payments/models.py b/hc/payments/models.py index d4bfa12b..c26bc2db 100644 --- a/hc/payments/models.py +++ b/hc/payments/models.py @@ -136,11 +136,15 @@ class Subscription(models.Model): return result - def cancel(self): + def cancel(self, delete_customer=False): if 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_name = "" self.save()