billing: Inline is_realm_on_paid_plan.

This helps simplify the BillingSession interface to have less
realm-specific functions outside RealmBillingSesssion.
This commit is contained in:
Tim Abbott 2023-11-27 08:31:39 -08:00
parent f8b0e16ff2
commit 7de061cf10
2 changed files with 13 additions and 13 deletions

View File

@ -79,11 +79,6 @@ CARD_CAPITALIZATION = {
"visa": "Visa", "visa": "Visa",
} }
PAID_PLANS = [
Realm.PLAN_TYPE_STANDARD,
Realm.PLAN_TYPE_PLUS,
]
# The version of Stripe API the billing system supports. # The version of Stripe API the billing system supports.
STRIPE_API_VERSION = "2020-08-27" STRIPE_API_VERSION = "2020-08-27"
@ -212,10 +207,6 @@ def check_upgrade_parameters(
) )
def is_realm_on_paid_plan(realm: Realm) -> bool:
return realm.plan_type in PAID_PLANS
# Be extremely careful changing this function. Historical billing periods # Be extremely careful changing this function. Historical billing periods
# are not stored anywhere, and are just computed on the fly using this # are not stored anywhere, and are just computed on the fly using this
# function. Any change you make here should return the same value (or be # function. Any change you make here should return the same value (or be
@ -1636,6 +1627,11 @@ class RealmBillingSession(BillingSession):
assert realm is not None # for mypy assert realm is not None # for mypy
self.realm = realm self.realm = realm
PAID_PLANS = [
Realm.PLAN_TYPE_STANDARD,
Realm.PLAN_TYPE_PLUS,
]
@override @override
@property @property
def billing_session_url(self) -> str: def billing_session_url(self) -> str:
@ -1861,7 +1857,7 @@ class RealmBillingSession(BillingSession):
@override @override
def on_paid_plan(self) -> bool: def on_paid_plan(self) -> bool:
return is_realm_on_paid_plan(self.realm) return self.realm.plan_type in self.PAID_PLANS
@override @override
def add_sponsorship_info_to_context(self, context: Dict[str, Any]) -> None: def add_sponsorship_info_to_context(self, context: Dict[str, Any]) -> None:

View File

@ -5,7 +5,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from corporate.lib.stripe import RealmBillingSession, UpdatePlanRequest, is_realm_on_paid_plan from corporate.lib.stripe import RealmBillingSession, UpdatePlanRequest
from corporate.models import CustomerPlan, get_customer_by_realm from corporate.models import CustomerPlan, get_customer_by_realm
from zerver.decorator import require_billing_access, zulip_login_required from zerver.decorator import require_billing_access, zulip_login_required
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
@ -39,6 +39,11 @@ def billing_home(
user = request.user user = request.user
assert user.is_authenticated assert user.is_authenticated
# BUG: This should pass the acting_user; this is just working
# around that make_end_of_cycle_updates_if_needed doesn't do audit
# logging not using the session user properly.
billing_session = RealmBillingSession(user=None, realm=user.realm)
context: Dict[str, Any] = { context: Dict[str, Any] = {
"admin_access": user.has_billing_access, "admin_access": user.has_billing_access,
"has_active_plan": False, "has_active_plan": False,
@ -54,7 +59,7 @@ def billing_home(
customer = get_customer_by_realm(user.realm) customer = get_customer_by_realm(user.realm)
if customer is not None and customer.sponsorship_pending: if customer is not None and customer.sponsorship_pending:
# Don't redirect to sponsorship page if the realm is on a paid plan # Don't redirect to sponsorship page if the realm is on a paid plan
if not is_realm_on_paid_plan(user.realm): if not billing_session.on_paid_plan():
return HttpResponseRedirect(reverse("sponsorship_request")) return HttpResponseRedirect(reverse("sponsorship_request"))
# If the realm is on a paid plan, show the sponsorship pending message # If the realm is on a paid plan, show the sponsorship pending message
# TODO: Add a sponsorship pending message to the billing page # TODO: Add a sponsorship pending message to the billing page
@ -73,7 +78,6 @@ def billing_home(
return HttpResponseRedirect(reverse(upgrade_page)) return HttpResponseRedirect(reverse(upgrade_page))
billing_session = RealmBillingSession(user=None, realm=user.realm)
main_context = billing_session.get_billing_page_context() main_context = billing_session.get_billing_page_context()
if main_context: if main_context:
context.update(main_context) context.update(main_context)