stripe: End legacy plan on end_date.

This commit is contained in:
Aman Agrawal 2024-01-19 14:48:43 +00:00 committed by Tim Abbott
parent 59996ff353
commit 5a97a4d8dc
2 changed files with 39 additions and 0 deletions

View File

@ -1651,6 +1651,9 @@ class BillingSession(ABC):
else:
next_billing_cycle = start_of_next_billing_cycle(plan, last_renewal)
if plan.end_date is not None:
next_billing_cycle = min(next_billing_cycle, plan.end_date)
return next_billing_cycle
# event_time should roughly be timezone_now(). Not designed to handle
@ -1666,6 +1669,15 @@ class BillingSession(ABC):
if event_in_next_billing_cycle and last_ledger_entry is not None:
licenses_at_next_renewal = last_ledger_entry.licenses_at_next_renewal
assert licenses_at_next_renewal is not None
if (
plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY
and plan.end_date == next_billing_cycle
and plan.status == CustomerPlan.ACTIVE
):
self.process_downgrade(plan, True)
return None, None
if plan.status == CustomerPlan.ACTIVE:
return None, LicenseLedger.objects.create(
plan=plan,

View File

@ -7341,3 +7341,30 @@ class TestRemoteServerBillingFlow(StripeTestCase, RemoteServerTestCase):
# Verify Zulip team receives mail for the next cycle.
invoice_plans_as_needed(add_months(self.next_month, 1))
self.assert_length(outbox, messages_count + 1)
@responses.activate
@mock_stripe()
def test_legacy_plan_ends_on_plan_end_date(self, *mocks: Mock) -> None:
self.login("hamlet")
self.add_mock_response()
with time_machine.travel(self.now, tick=False):
send_server_data_to_push_bouncer(consider_usage_statistics=False)
plan_end_date = add_months(self.now, 3)
self.billing_session.migrate_customer_to_legacy_plan(self.now, plan_end_date)
# Legacy plan ends on plan end date.
customer = self.billing_session.get_customer()
assert customer is not None
plan = get_current_plan_by_customer(customer)
assert plan is not None
self.assertEqual(
self.remote_server.plan_type, RemoteZulipServer.PLAN_TYPE_SELF_MANAGED_LEGACY
)
self.billing_session.make_end_of_cycle_updates_if_needed(plan, plan_end_date)
plan.refresh_from_db()
self.remote_server.refresh_from_db()
self.assertEqual(self.remote_server.plan_type, RemoteZulipServer.PLAN_TYPE_SELF_MANAGED)
self.assertEqual(plan.next_invoice_date, None)
self.assertEqual(plan.status, CustomerPlan.ENDED)