2018-09-25 12:24:11 +02:00
|
|
|
import logging
|
2023-12-12 19:35:16 +01:00
|
|
|
from typing import Any, Dict, Literal, Optional
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2023-12-12 19:35:16 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed, HttpResponseRedirect
|
2019-02-02 23:53:22 +01:00
|
|
|
from django.shortcuts import render
|
2018-09-25 12:24:11 +02:00
|
|
|
from django.urls import reverse
|
2023-12-12 19:35:16 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2023-11-22 12:44:02 +01:00
|
|
|
|
2023-12-01 10:43:04 +01:00
|
|
|
from corporate.lib.decorator import (
|
|
|
|
authenticated_remote_realm_management_endpoint,
|
|
|
|
authenticated_remote_server_management_endpoint,
|
|
|
|
)
|
|
|
|
from corporate.lib.stripe import (
|
|
|
|
RealmBillingSession,
|
|
|
|
RemoteRealmBillingSession,
|
|
|
|
RemoteServerBillingSession,
|
2023-12-13 02:44:55 +01:00
|
|
|
ServerDeactivateWithExistingPlanError,
|
2023-12-01 10:43:04 +01:00
|
|
|
UpdatePlanRequest,
|
2023-12-12 19:35:16 +01:00
|
|
|
do_deactivate_remote_server,
|
2023-12-01 10:43:04 +01:00
|
|
|
)
|
2023-12-06 05:56:20 +01:00
|
|
|
from corporate.models import CustomerPlan, get_current_plan_by_customer, get_customer_by_realm
|
2023-12-06 12:33:33 +01:00
|
|
|
from zerver.decorator import process_as_post, require_billing_access, zulip_login_required
|
2023-12-12 19:35:16 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-09-25 12:24:11 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-07-04 08:19:18 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-12-01 10:43:04 +01:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
|
|
|
from zerver.lib.validator import check_int, check_int_in
|
2023-11-27 13:08:43 +01:00
|
|
|
from zerver.models import UserProfile
|
2023-12-07 15:27:39 +01:00
|
|
|
from zilencer.lib.remote_counts import MissingDataError
|
2023-12-01 10:43:04 +01:00
|
|
|
from zilencer.models import RemoteRealm, RemoteZulipServer
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
billing_logger = logging.getLogger("corporate.stripe")
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2023-12-02 09:09:43 +01:00
|
|
|
ALLOWED_PLANS_API_STATUS_VALUES = [
|
|
|
|
CustomerPlan.ACTIVE,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.SWITCH_TO_MONTHLY_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.FREE_TRIAL,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,
|
|
|
|
CustomerPlan.ENDED,
|
|
|
|
]
|
|
|
|
|
2023-02-13 20:40:51 +01:00
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
@zulip_login_required
|
2023-12-01 10:43:04 +01:00
|
|
|
@typed_endpoint
|
2023-12-01 07:39:05 +01:00
|
|
|
def billing_page(
|
2023-11-13 15:56:10 +01:00
|
|
|
request: HttpRequest,
|
2023-12-01 10:43:04 +01:00
|
|
|
*,
|
|
|
|
success_message: str = "",
|
2021-07-29 19:01:39 +02:00
|
|
|
) -> HttpResponse:
|
2018-09-25 12:24:11 +02:00
|
|
|
user = request.user
|
2021-07-24 20:37:35 +02:00
|
|
|
assert user.is_authenticated
|
|
|
|
|
2023-12-24 15:56:33 +01:00
|
|
|
billing_session = RealmBillingSession(user=user, realm=user.realm)
|
2023-11-27 17:31:39 +01:00
|
|
|
|
2020-08-21 14:45:43 +02:00
|
|
|
context: Dict[str, Any] = {
|
|
|
|
"admin_access": user.has_billing_access,
|
2021-02-12 08:20:45 +01:00
|
|
|
"has_active_plan": False,
|
2023-10-28 15:29:31 +02:00
|
|
|
"org_name": user.realm.name,
|
2023-12-02 09:09:43 +01:00
|
|
|
"billing_base_url": "",
|
2020-08-21 14:45:43 +02:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:34:37 +01:00
|
|
|
if not user.has_billing_access:
|
|
|
|
return render(request, "corporate/billing.html", context=context)
|
|
|
|
|
2021-10-18 23:28:17 +02:00
|
|
|
if user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:
|
2023-11-02 16:34:37 +01:00
|
|
|
return HttpResponseRedirect(reverse("sponsorship_request"))
|
|
|
|
|
|
|
|
customer = get_customer_by_realm(user.realm)
|
2023-11-04 07:23:15 +01:00
|
|
|
if customer is not None and customer.sponsorship_pending:
|
|
|
|
# Don't redirect to sponsorship page if the realm is on a paid plan
|
2023-11-27 17:31:39 +01:00
|
|
|
if not billing_session.on_paid_plan():
|
2023-11-04 07:23:15 +01:00
|
|
|
return HttpResponseRedirect(reverse("sponsorship_request"))
|
|
|
|
# If the realm is on a paid plan, show the sponsorship pending message
|
|
|
|
context["sponsorship_pending"] = True
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2023-11-08 17:38:47 +01:00
|
|
|
if user.realm.plan_type == user.realm.PLAN_TYPE_LIMITED:
|
|
|
|
return HttpResponseRedirect(reverse("plans"))
|
|
|
|
|
2023-12-06 05:56:20 +01:00
|
|
|
if customer is None or get_current_plan_by_customer(customer) is None:
|
2023-12-01 08:07:26 +01:00
|
|
|
return HttpResponseRedirect(reverse("upgrade_page"))
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2023-11-16 16:14:43 +01:00
|
|
|
main_context = billing_session.get_billing_page_context()
|
|
|
|
if main_context:
|
|
|
|
context.update(main_context)
|
2023-11-22 12:02:09 +01:00
|
|
|
context["success_message"] = success_message
|
2020-04-03 16:17:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "corporate/billing.html", context=context)
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-12-01 10:43:04 +01:00
|
|
|
@authenticated_remote_realm_management_endpoint
|
|
|
|
@typed_endpoint
|
|
|
|
def remote_realm_billing_page(
|
|
|
|
request: HttpRequest,
|
|
|
|
billing_session: RemoteRealmBillingSession,
|
|
|
|
*,
|
|
|
|
success_message: str = "",
|
2023-12-13 12:25:23 +01:00
|
|
|
) -> HttpResponse:
|
2023-12-05 12:35:21 +01:00
|
|
|
realm_uuid = billing_session.remote_realm.uuid
|
2023-12-01 10:43:04 +01:00
|
|
|
context: Dict[str, Any] = {
|
|
|
|
# We wouldn't be here if user didn't have access.
|
|
|
|
"admin_access": billing_session.has_billing_access(),
|
|
|
|
"has_active_plan": False,
|
|
|
|
"org_name": billing_session.remote_realm.name,
|
2023-12-06 05:44:12 +01:00
|
|
|
"billing_base_url": billing_session.billing_base_url,
|
2023-12-01 10:43:04 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 12:25:23 +01:00
|
|
|
if billing_session.remote_realm.plan_type == RemoteRealm.PLAN_TYPE_COMMUNITY: # nocoverage
|
2023-12-05 12:35:21 +01:00
|
|
|
return HttpResponseRedirect(reverse("remote_realm_sponsorship_page", args=(realm_uuid,)))
|
2023-12-01 10:43:04 +01:00
|
|
|
|
|
|
|
customer = billing_session.get_customer()
|
2023-12-13 12:25:23 +01:00
|
|
|
if customer is not None and customer.sponsorship_pending: # nocoverage
|
2023-12-14 09:46:06 +01:00
|
|
|
# Don't redirect to sponsorship page if the remote realm is on a paid plan or scheduled for an upgrade.
|
|
|
|
if (
|
|
|
|
not billing_session.on_paid_plan()
|
|
|
|
and billing_session.get_legacy_remote_server_next_plan_name(customer) is None
|
|
|
|
):
|
2023-12-05 12:35:21 +01:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("remote_realm_sponsorship_page", args=(realm_uuid,))
|
|
|
|
)
|
2023-12-01 10:43:04 +01:00
|
|
|
# If the realm is on a paid plan, show the sponsorship pending message
|
|
|
|
context["sponsorship_pending"] = True
|
|
|
|
|
2023-12-11 18:00:42 +01:00
|
|
|
if (
|
|
|
|
customer is None
|
|
|
|
or get_current_plan_by_customer(customer) is None
|
|
|
|
or (
|
|
|
|
billing_session.get_legacy_remote_server_next_plan_name(customer) is None
|
2023-12-14 00:17:55 +01:00
|
|
|
and billing_session.remote_realm.plan_type
|
|
|
|
in [
|
|
|
|
RemoteRealm.PLAN_TYPE_SELF_MANAGED,
|
|
|
|
RemoteRealm.PLAN_TYPE_SELF_MANAGED_LEGACY,
|
|
|
|
]
|
2023-12-11 18:00:42 +01:00
|
|
|
)
|
2023-12-13 12:25:23 +01:00
|
|
|
): # nocoverage
|
2023-12-05 12:35:21 +01:00
|
|
|
return HttpResponseRedirect(reverse("remote_realm_plans_page", args=(realm_uuid,)))
|
2023-12-01 10:43:04 +01:00
|
|
|
|
2023-12-07 15:27:39 +01:00
|
|
|
try:
|
|
|
|
main_context = billing_session.get_billing_page_context()
|
2023-12-13 12:25:23 +01:00
|
|
|
except MissingDataError: # nocoverage
|
2023-12-07 15:27:39 +01:00
|
|
|
return billing_session.missing_data_error_page(request)
|
|
|
|
|
2023-12-01 10:43:04 +01:00
|
|
|
if main_context:
|
|
|
|
context.update(main_context)
|
|
|
|
context["success_message"] = success_message
|
|
|
|
|
|
|
|
return render(request, "corporate/billing.html", context=context)
|
|
|
|
|
|
|
|
|
|
|
|
@authenticated_remote_server_management_endpoint
|
|
|
|
@typed_endpoint
|
|
|
|
def remote_server_billing_page(
|
|
|
|
request: HttpRequest,
|
|
|
|
billing_session: RemoteServerBillingSession,
|
|
|
|
*,
|
|
|
|
success_message: str = "",
|
2023-12-19 12:24:15 +01:00
|
|
|
) -> HttpResponse:
|
2023-12-01 10:43:04 +01:00
|
|
|
context: Dict[str, Any] = {
|
|
|
|
# We wouldn't be here if user didn't have access.
|
|
|
|
"admin_access": billing_session.has_billing_access(),
|
|
|
|
"has_active_plan": False,
|
|
|
|
"org_name": billing_session.remote_server.hostname,
|
2023-12-06 05:44:12 +01:00
|
|
|
"billing_base_url": billing_session.billing_base_url,
|
2023-12-01 10:43:04 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:24:15 +01:00
|
|
|
if (
|
|
|
|
billing_session.remote_server.plan_type == RemoteZulipServer.PLAN_TYPE_COMMUNITY
|
|
|
|
): # nocoverage
|
2023-12-01 10:43:04 +01:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"remote_server_sponsorship_page",
|
|
|
|
kwargs={"server_uuid": billing_session.remote_server.uuid},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
customer = billing_session.get_customer()
|
|
|
|
if customer is not None and customer.sponsorship_pending:
|
2023-12-14 09:46:06 +01:00
|
|
|
# Don't redirect to sponsorship page if the remote realm is on a paid plan or scheduled for an upgrade.
|
|
|
|
if (
|
|
|
|
not billing_session.on_paid_plan()
|
|
|
|
and billing_session.get_legacy_remote_server_next_plan_name(customer) is None
|
|
|
|
):
|
2023-12-01 10:43:04 +01:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"remote_server_sponsorship_page",
|
|
|
|
kwargs={"server_uuid": billing_session.remote_server.uuid},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# If the realm is on a paid plan, show the sponsorship pending message
|
2023-12-19 12:24:15 +01:00
|
|
|
context["sponsorship_pending"] = True # nocoverage
|
2023-12-01 10:43:04 +01:00
|
|
|
|
|
|
|
if (
|
2023-12-06 13:37:19 +01:00
|
|
|
customer is None
|
2023-12-06 05:56:20 +01:00
|
|
|
or get_current_plan_by_customer(customer) is None
|
2023-12-06 13:37:19 +01:00
|
|
|
or (
|
2023-12-09 09:00:34 +01:00
|
|
|
billing_session.get_legacy_remote_server_next_plan_name(customer) is None
|
2023-12-14 00:17:55 +01:00
|
|
|
and billing_session.remote_server.plan_type
|
|
|
|
in [
|
|
|
|
RemoteZulipServer.PLAN_TYPE_SELF_MANAGED,
|
|
|
|
RemoteZulipServer.PLAN_TYPE_SELF_MANAGED_LEGACY,
|
|
|
|
]
|
2023-12-06 13:37:19 +01:00
|
|
|
)
|
2023-12-01 10:43:04 +01:00
|
|
|
):
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"remote_server_upgrade_page",
|
|
|
|
kwargs={"server_uuid": billing_session.remote_server.uuid},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-12-07 15:27:39 +01:00
|
|
|
try:
|
|
|
|
main_context = billing_session.get_billing_page_context()
|
2023-12-19 12:24:15 +01:00
|
|
|
except MissingDataError: # nocoverage
|
2023-12-07 15:27:39 +01:00
|
|
|
return billing_session.missing_data_error_page(request)
|
|
|
|
|
2023-12-01 10:43:04 +01:00
|
|
|
if main_context:
|
|
|
|
context.update(main_context)
|
|
|
|
context["success_message"] = success_message
|
|
|
|
|
|
|
|
return render(request, "corporate/billing.html", context=context)
|
|
|
|
|
|
|
|
|
2018-11-01 11:26:29 +01:00
|
|
|
@require_billing_access
|
2019-04-08 05:16:35 +02:00
|
|
|
@has_request_variables
|
2020-12-10 18:15:09 +01:00
|
|
|
def update_plan(
|
2021-04-14 15:50:40 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user: UserProfile,
|
2020-12-10 18:15:09 +01:00
|
|
|
status: Optional[int] = REQ(
|
2021-04-14 15:50:40 +02:00
|
|
|
"status",
|
2023-12-02 09:09:43 +01:00
|
|
|
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES),
|
2020-12-10 18:15:09 +01:00
|
|
|
default=None,
|
2021-04-14 15:50:40 +02:00
|
|
|
),
|
2020-12-23 17:08:27 +01:00
|
|
|
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None),
|
|
|
|
licenses_at_next_renewal: Optional[int] = REQ(
|
|
|
|
"licenses_at_next_renewal", json_validator=check_int, default=None
|
|
|
|
),
|
2023-11-26 15:41:28 +01:00
|
|
|
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-11-22 12:44:02 +01:00
|
|
|
update_plan_request = UpdatePlanRequest(
|
|
|
|
status=status,
|
|
|
|
licenses=licenses,
|
|
|
|
licenses_at_next_renewal=licenses_at_next_renewal,
|
2023-11-26 15:41:28 +01:00
|
|
|
schedule=schedule,
|
2023-11-13 15:05:56 +01:00
|
|
|
)
|
2023-11-22 12:44:02 +01:00
|
|
|
billing_session = RealmBillingSession(user=user)
|
|
|
|
billing_session.do_update_plan(update_plan_request)
|
|
|
|
return json_success(request)
|
2023-12-02 09:09:43 +01:00
|
|
|
|
|
|
|
|
2023-12-04 15:56:48 +01:00
|
|
|
@authenticated_remote_realm_management_endpoint
|
2023-12-06 12:33:33 +01:00
|
|
|
@process_as_post
|
|
|
|
@has_request_variables
|
2023-12-02 09:09:43 +01:00
|
|
|
def update_plan_for_remote_realm(
|
|
|
|
request: HttpRequest,
|
|
|
|
billing_session: RemoteRealmBillingSession,
|
|
|
|
status: Optional[int] = REQ(
|
|
|
|
"status",
|
|
|
|
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES),
|
|
|
|
default=None,
|
|
|
|
),
|
|
|
|
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None),
|
|
|
|
licenses_at_next_renewal: Optional[int] = REQ(
|
|
|
|
"licenses_at_next_renewal", json_validator=check_int, default=None
|
|
|
|
),
|
|
|
|
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
|
2023-12-20 12:22:00 +01:00
|
|
|
) -> HttpResponse:
|
2023-12-02 09:09:43 +01:00
|
|
|
update_plan_request = UpdatePlanRequest(
|
|
|
|
status=status,
|
|
|
|
licenses=licenses,
|
|
|
|
licenses_at_next_renewal=licenses_at_next_renewal,
|
|
|
|
schedule=schedule,
|
|
|
|
)
|
|
|
|
billing_session.do_update_plan(update_plan_request)
|
|
|
|
return json_success(request)
|
|
|
|
|
|
|
|
|
2023-12-04 15:56:48 +01:00
|
|
|
@authenticated_remote_server_management_endpoint
|
2023-12-06 12:33:33 +01:00
|
|
|
@process_as_post
|
|
|
|
@has_request_variables
|
2023-12-02 09:09:43 +01:00
|
|
|
def update_plan_for_remote_server(
|
|
|
|
request: HttpRequest,
|
|
|
|
billing_session: RemoteServerBillingSession,
|
|
|
|
status: Optional[int] = REQ(
|
|
|
|
"status",
|
|
|
|
json_validator=check_int_in(ALLOWED_PLANS_API_STATUS_VALUES),
|
|
|
|
default=None,
|
|
|
|
),
|
|
|
|
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None),
|
|
|
|
licenses_at_next_renewal: Optional[int] = REQ(
|
|
|
|
"licenses_at_next_renewal", json_validator=check_int, default=None
|
|
|
|
),
|
|
|
|
schedule: Optional[int] = REQ("schedule", json_validator=check_int, default=None),
|
2023-12-19 12:24:15 +01:00
|
|
|
) -> HttpResponse:
|
2023-12-02 09:09:43 +01:00
|
|
|
update_plan_request = UpdatePlanRequest(
|
|
|
|
status=status,
|
|
|
|
licenses=licenses,
|
|
|
|
licenses_at_next_renewal=licenses_at_next_renewal,
|
|
|
|
schedule=schedule,
|
|
|
|
)
|
|
|
|
billing_session.do_update_plan(update_plan_request)
|
|
|
|
return json_success(request)
|
2023-12-12 19:35:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@authenticated_remote_server_management_endpoint
|
|
|
|
@typed_endpoint
|
|
|
|
def remote_server_deactivate_page(
|
|
|
|
request: HttpRequest,
|
|
|
|
billing_session: RemoteServerBillingSession,
|
|
|
|
*,
|
|
|
|
confirmed: Literal[None, "true"] = None,
|
|
|
|
) -> HttpResponse:
|
|
|
|
if request.method not in ["GET", "POST"]: # nocoverage
|
|
|
|
return HttpResponseNotAllowed(["GET", "POST"])
|
|
|
|
|
|
|
|
remote_server = billing_session.remote_server
|
2023-12-13 02:44:55 +01:00
|
|
|
context = {
|
|
|
|
"server_hostname": remote_server.hostname,
|
|
|
|
"action_url": reverse(remote_server_deactivate_page, args=[str(remote_server.uuid)]),
|
|
|
|
}
|
2023-12-12 19:35:16 +01:00
|
|
|
if request.method == "GET":
|
|
|
|
return render(request, "corporate/remote_billing_server_deactivate.html", context=context)
|
|
|
|
|
|
|
|
assert request.method == "POST"
|
|
|
|
if confirmed is None: # nocoverage
|
|
|
|
# Should be impossible if the user is using the UI.
|
|
|
|
raise JsonableError(_("Parameter 'confirmed' is required"))
|
|
|
|
|
2023-12-13 02:44:55 +01:00
|
|
|
try:
|
|
|
|
do_deactivate_remote_server(remote_server, billing_session)
|
|
|
|
except ServerDeactivateWithExistingPlanError: # nocoverage
|
|
|
|
context["show_existing_plan_error"] = "true"
|
|
|
|
return render(request, "corporate/remote_billing_server_deactivate.html", context=context)
|
|
|
|
|
2023-12-12 19:35:16 +01:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"corporate/remote_billing_server_deactivated_success.html",
|
|
|
|
context={"server_hostname": remote_server.hostname},
|
|
|
|
)
|