Browse Source

Delete customer from Braintree when closing account.

pull/287/head
Pēteris Caune 5 years ago
parent
commit
2489f86b38
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 16 additions and 7 deletions
  1. +9
    -4
      hc/accounts/tests/test_close_account.py
  2. +1
    -1
      hc/accounts/views.py
  3. +6
    -2
      hc/payments/models.py

+ 9
- 4
hc/accounts/tests/test_close_account.py View File

@ -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="[email protected]", 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())


+ 1
- 1
hc/accounts/views.py View File

@ -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()


+ 6
- 2
hc/payments/models.py View File

@ -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()


Loading…
Cancel
Save