Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

billing: Call make_end_of_cycle_updates_if_needed before plan is updated.

Vishnu KS878dcd95d000
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index bfeb724508a..16305407924 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -1780,15 +1780,16 @@ class StripeTest(StripeTestCase):
with patch("corporate.lib.stripe.timezone_now", return_value=self.now):
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")
with self.assertLogs("corporate.stripe", "INFO") as m:
- response = self.client_patch(
- "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
- )
- stripe_customer_id = Customer.objects.get(realm=user.realm).id
- new_plan = get_current_plan_by_realm(user.realm)
- assert new_plan is not None
- expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"
- self.assertEqual(m.output[0], expected_log)
- self.assert_json_success(response)
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
+ )
+ stripe_customer_id = Customer.objects.get(realm=user.realm).id
+ new_plan = get_current_plan_by_realm(user.realm)
+ assert new_plan is not None
+ expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"
+ self.assertEqual(m.output[0], expected_log)
+ self.assert_json_success(response)
# Verify that we still write LicenseLedger rows during the remaining
# part of the cycle
@@ -1869,13 +1870,14 @@ class StripeTest(StripeTestCase):
assert new_plan is not None
with self.assertLogs("corporate.stripe", "INFO") as m:
- response = self.client_patch(
- "/json/billing/plan",
- {"status": CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE},
- )
- expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE}"
- self.assertEqual(m.output[0], expected_log)
- self.assert_json_success(response)
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch(
+ "/json/billing/plan",
+ {"status": CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE},
+ )
+ expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE}"
+ self.assertEqual(m.output[0], expected_log)
+ self.assert_json_success(response)
monthly_plan.refresh_from_db()
self.assertEqual(monthly_plan.status, CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE)
with patch("corporate.views.timezone_now", return_value=self.now):
@@ -2053,15 +2055,16 @@ class StripeTest(StripeTestCase):
new_plan = get_current_plan_by_realm(user.realm)
assert new_plan is not None
with self.assertLogs("corporate.stripe", "INFO") as m:
- response = self.client_patch(
- "/json/billing/plan",
- {"status": CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE},
- )
- self.assertEqual(
- m.output[0],
- f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE}",
- )
- self.assert_json_success(response)
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch(
+ "/json/billing/plan",
+ {"status": CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE},
+ )
+ self.assertEqual(
+ m.output[0],
+ f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE}",
+ )
+ self.assert_json_success(response)
monthly_plan.refresh_from_db()
self.assertEqual(monthly_plan.status, CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE)
with patch("corporate.views.timezone_now", return_value=self.now):
@@ -2152,23 +2155,25 @@ class StripeTest(StripeTestCase):
with patch("corporate.lib.stripe.timezone_now", return_value=self.now):
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")
with self.assertLogs("corporate.stripe", "INFO") as m:
- response = self.client_patch(
- "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
- )
- stripe_customer_id = Customer.objects.get(realm=user.realm).id
- new_plan = get_current_plan_by_realm(user.realm)
- assert new_plan is not None
- expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"
- self.assertEqual(m.output[0], expected_log)
- self.assert_json_success(response)
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
+ )
+ stripe_customer_id = Customer.objects.get(realm=user.realm).id
+ new_plan = get_current_plan_by_realm(user.realm)
+ assert new_plan is not None
+ expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"
+ self.assertEqual(m.output[0], expected_log)
+ self.assert_json_success(response)
self.assertEqual(
CustomerPlan.objects.first().status, CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
)
with self.assertLogs("corporate.stripe", "INFO") as m:
- response = self.client_patch("/json/billing/plan", {"status": CustomerPlan.ACTIVE})
- expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.ACTIVE}"
- self.assertEqual(m.output[0], expected_log)
- self.assert_json_success(response)
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch("/json/billing/plan", {"status": CustomerPlan.ACTIVE})
+ expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.ACTIVE}"
+ self.assertEqual(m.output[0], expected_log)
+ self.assert_json_success(response)
self.assertEqual(CustomerPlan.objects.first().status, CustomerPlan.ACTIVE)
@patch("stripe.Invoice.create")
@@ -2188,9 +2193,10 @@ class StripeTest(StripeTestCase):
stripe_customer_id = Customer.objects.get(realm=user.realm).id
new_plan = get_current_plan_by_realm(user.realm)
assert new_plan is not None
- self.client_patch(
- "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
- )
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
+ )
expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {stripe_customer_id}, CustomerPlan.id: {new_plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"
self.assertEqual(m.output[0], expected_log)
@@ -2224,7 +2230,9 @@ class StripeTest(StripeTestCase):
self.assertEqual(last_ledger_entry.licenses_at_next_renewal, 21)
self.login_user(user)
- self.client_patch("/json/billing/plan", {"status": CustomerPlan.ENDED})
+
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ self.client_patch("/json/billing/plan", {"status": CustomerPlan.ENDED})
plan.refresh_from_db()
self.assertEqual(get_realm("zulip").plan_type, Realm.LIMITED)
@@ -2256,9 +2264,10 @@ class StripeTest(StripeTestCase):
self.login_user(user)
with self.assertLogs("corporate.stripe", "INFO") as m:
- self.client_patch(
- "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
- )
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
+ )
stripe_customer_id = Customer.objects.get(realm=user.realm).id
new_plan = get_current_plan_by_realm(user.realm)
assert new_plan is not None
@@ -2313,9 +2322,55 @@ class StripeTest(StripeTestCase):
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")
self.login_user(self.example_user("hamlet"))
- response = self.client_patch("/json/billing/plan", {})
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ response = self.client_patch("/json/billing/plan", {})
self.assert_json_error_contains(response, "Nothing to change")
+ def test_update_plan_that_which_is_due_for_expiry(self) -> None:
+ with patch("corporate.lib.stripe.timezone_now", return_value=self.now):
+ self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")
+
+ self.login_user(self.example_user("hamlet"))
+ with self.assertLogs("corporate.stripe", "INFO") as m:
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ result = self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}
+ )
+ self.assert_json_success(result)
+ self.assertRegexpMatches(
+ m.output[0],
+ r"INFO:corporate.stripe:Change plan status: Customer.id: \d*, CustomerPlan.id: \d*, status: 2",
+ )
+
+ with patch("corporate.lib.stripe.timezone_now", return_value=self.next_year):
+ result = self.client_patch("/json/billing/plan", {"status": CustomerPlan.ACTIVE})
+ self.assert_json_error_contains(
+ result, "Unable to update the plan. The plan has ended."
+ )
+
+ def test_update_plan_that_which_is_due_for_replacement(self) -> None:
+ with patch("corporate.lib.stripe.timezone_now", return_value=self.now):
+ self.local_upgrade(self.seat_count, True, CustomerPlan.MONTHLY, "token")
+
+ self.login_user(self.example_user("hamlet"))
+ with self.assertLogs("corporate.stripe", "INFO") as m:
+ with patch("corporate.views.timezone_now", return_value=self.now):
+ result = self.client_patch(
+ "/json/billing/plan", {"status": CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE}
+ )
+ self.assert_json_success(result)
+ self.assertRegexpMatches(
+ m.output[0],
+ r"INFO:corporate.stripe:Change plan status: Customer.id: \d*, CustomerPlan.id: \d*, status: 4",
+ )
+
+ with patch("corporate.lib.stripe.timezone_now", return_value=self.next_month):
+ result = self.client_patch("/json/billing/plan", {})
+ self.assert_json_error_contains(
+ result,
+ "Unable to update the plan. The plan has been expired and replaced with a new plan.",
+ )
+
@patch("corporate.lib.stripe.billing_logger.info")
def test_deactivate_realm(self, mock_: Mock) -> None:
user = self.example_user("hamlet")
diff --git a/corporate/views.py b/corporate/views.py
index bb249bc5914..2f3c540095f 100644
--- a/corporate/views.py
+++ b/corporate/views.py
@@ -362,6 +362,15 @@ def update_plan(
plan = get_current_plan_by_realm(user.realm)
assert plan is not None # for mypy
+ new_plan, last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, timezone_now())
+ if new_plan is not None:
+ return json_error(
+ _("Unable to update the plan. The plan has been expired and replaced with a new plan.")
+ )
+
+ if last_ledger_entry is None:
+ return json_error(_("Unable to update the plan. The plan has ended."))
+
if status is not None:
if status == CustomerPlan.ACTIVE:
assert plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE