Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Add BillingSession.get_current_billed_license_count helper.
Lauryn Menardec9ada65f206
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex f63f09e97ea..82c64471f9a 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -755,6 +755,11 @@ class BillingSession(ABC):def org_name(self) -> str:pass+ def get_current_billed_license_count(self, event_time: datetime | None = None) -> int:+ # A "workplace user" translates to a "license" tracked in LicenseLedger+ # objects for a plan.+ return self.current_counts_for_billed_users(event_time).workplace_users+def customer_plan_exists(self) -> bool:# Checks if the realm / server had a plan anytime in the past.customer = self.get_customer()@@ -1769,14 +1774,17 @@ class BillingSession(ABC):)return self.create_stripe_invoice_and_charge(updated_metadata)- def stale_seat_count_check(self, request_seat_count: int, tier: int) -> int:- current_seat_count = self.current_counts_for_billed_users().workplace_users- minimum_seat_count = self.min_licenses_for_plan(tier)- if request_seat_count == minimum_seat_count and current_seat_count < minimum_seat_count:+ def stale_license_count_check(self, request_license_count: int, tier: int) -> int:+ current_license_count = self.get_current_billed_license_count()+ minimum_license_count = self.min_licenses_for_plan(tier)+ if (+ request_license_count == minimum_license_count+ and current_license_count < minimum_license_count+ ):# Continue to use the minimum licenses for the plan tier.- return request_seat_count+ return request_license_count# Otherwise, we want to check the current count against the minimum.- return max(current_seat_count, minimum_seat_count)+ return max(current_license_count, minimum_license_count)def ensure_current_plan_is_upgradable(self, customer: Customer, new_plan_tier: int) -> None:# Upgrade for customers with an existing plan is only supported for remote realm / server right now.@@ -2118,7 +2126,7 @@ class BillingSession(ABC):)# For automated license management, we check for changes to the# billable licenses count made after the billing portal was loaded.- seat_count = self.stale_seat_count_check(request_seat_count, upgrade_request.tier)+ seat_count = self.stale_license_count_check(request_seat_count, upgrade_request.tier)if billing_modality == "charge_automatically" and license_management == "automatic":licenses = seat_count@@ -2299,7 +2307,7 @@ class BillingSession(ABC):if plan.automanage_licenses:return- if self.current_counts_for_billed_users().workplace_users > renewal_license_count:+ if self.get_current_billed_license_count() > renewal_license_count:raise BillingError(f"Customer has not manually updated plan for current license count: {plan.customer!s}")@@ -2573,10 +2581,10 @@ class BillingSession(ABC):licenses_at_next_renewal = last_ledger_entry.licenses_at_next_renewalassert licenses_at_next_renewal is not Nonemin_licenses_for_plan = self.min_licenses_for_plan(plan.tier)- seat_count = self.current_counts_for_billed_users().workplace_users+ billable_license_count = self.get_current_billed_license_count()using_min_licenses_for_plan = (min_licenses_for_plan == licenses_at_next_renewal- and licenses_at_next_renewal > seat_count+ and licenses_at_next_renewal > billable_license_count)# Should do this in JavaScript, using the user's time zone@@ -2675,7 +2683,7 @@ class BillingSession(ABC):"switch_to_monthly_at_end_of_cycle": switch_to_monthly_at_end_of_cycle,"licenses": licenses,"licenses_at_next_renewal": licenses_at_next_renewal,- "seat_count": seat_count,+ "seat_count": billable_license_count,"exempt_from_license_number_check": customer.exempt_from_license_number_check,"renewal_date": renewal_date,"renewal_amount": cents_to_dollar_string(renewal_cents) if renewal_cents != 0 else None,@@ -2851,11 +2859,11 @@ class BillingSession(ABC):if setup_payment_by_invoice:initial_upgrade_request.manual_license_management = True- seat_count = self.current_counts_for_billed_users().workplace_users- using_min_licenses_for_plan = min_licenses_for_plan > seat_count+ billable_license_count = self.get_current_billed_license_count()+ using_min_licenses_for_plan = min_licenses_for_plan > billable_license_countif using_min_licenses_for_plan:- seat_count = min_licenses_for_plan- signed_seat_count, salt = sign_string(str(seat_count))+ billable_license_count = min_licenses_for_plan+ signed_seat_count, salt = sign_string(str(billable_license_count))free_trial_days = Nonefree_trial_end_date = None@@ -2898,7 +2906,7 @@ class BillingSession(ABC):"page_type": "upgrade","annual_price": annual_price,"monthly_price": monthly_price,- "seat_count": seat_count,+ "seat_count": billable_license_count,"billing_base_url": self.billing_base_url,"tier": tier,"flat_discount": flat_discount,@@ -2916,7 +2924,7 @@ class BillingSession(ABC):"fixed_price_plan": fixed_price is not None,"pay_by_invoice_payments_page": pay_by_invoice_payments_page,"salt": salt,- "seat_count": seat_count,+ "seat_count": billable_license_count,"signed_seat_count": signed_seat_count,"success_message": initial_upgrade_request.success_message,"is_sponsorship_pending": customer is not None and customer.sponsorship_pending,@@ -3122,7 +3130,7 @@ class BillingSession(ABC):validate_licenses(plan.charge_automatically,licenses,- self.current_counts_for_billed_users().workplace_users,+ self.get_current_billed_license_count(),plan.customer.exempt_from_license_number_check,self.min_licenses_for_plan(plan.tier),)@@ -3158,7 +3166,7 @@ class BillingSession(ABC):validate_licenses(plan.charge_automatically,licenses_at_next_renewal,- self.current_counts_for_billed_users().workplace_users,+ self.get_current_billed_license_count(),plan.customer.exempt_from_license_number_check,self.min_licenses_for_plan(plan.tier, is_plan_free_trial_with_invoice_payment),)@@ -3885,7 +3893,7 @@ class BillingSession(ABC):) -> None:if licenses is not None:if not plan.customer.exempt_from_license_number_check:- assert self.current_counts_for_billed_users().workplace_users <= licenses+ assert self.get_current_billed_license_count() <= licensesassert licenses > plan.licenses()LicenseLedger.objects.create(plan=plan,@@ -3919,12 +3927,12 @@ class BillingSession(ABC):if licenses is not None and customer.exempt_from_license_number_check:return licenses- current_licenses_count = self.current_counts_for_billed_users(event_time).workplace_users+ current_license_count = self.get_current_billed_license_count()min_licenses_for_plan = self.min_licenses_for_plan(tier)if customer.exempt_from_license_number_check: # nocoverage- billed_licenses = current_licenses_count+ billed_licenses = current_license_countelse:- billed_licenses = max(current_licenses_count, min_licenses_for_plan)+ billed_licenses = max(current_license_count, min_licenses_for_plan)return billed_licensesdef update_license_ledger_for_automanaged_plan(@@ -6002,9 +6010,7 @@ def get_push_status_for_remote_request(user_count: int | None = Noneif current_plan is None:try:- user_count = (- user_count_billing_session.current_counts_for_billed_users().workplace_users- )+ user_count = user_count_billing_session.get_current_billed_license_count()except MissingDataError:return PushNotificationsEnabledStatus(can_push=False,@@ -6037,7 +6043,7 @@ def get_push_status_for_remote_request()try:- user_count = user_count_billing_session.current_counts_for_billed_users().workplace_users+ user_count = user_count_billing_session.get_current_billed_license_count()except MissingDataError:user_count = Nonediff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex ceb0697726d..d49dceaf212 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -2233,7 +2233,7 @@ class StripeTest(StripeTestCase):# Change the seat count in upgrade flow: after do_upgrade, during process_initial_upgradewith (patch(- "corporate.lib.stripe.BillingSession.stale_seat_count_check",+ "corporate.lib.stripe.BillingSession.stale_license_count_check",return_value=self.seat_count,),patch("corporate.lib.stripe.get_latest_seat_count", return_value=new_seat_count),@@ -2285,7 +2285,7 @@ class StripeTest(StripeTestCase):# Change the seat count in upgrade flow: after do_upgrade, during process_initial_upgradewith (patch(- "corporate.lib.stripe.BillingSession.stale_seat_count_check",+ "corporate.lib.stripe.BillingSession.stale_license_count_check",return_value=self.seat_count,),patch("corporate.lib.stripe.get_latest_seat_count", return_value=new_seat_count),