2018-09-25 12:24:11 +02:00
|
|
|
import logging
|
2021-08-29 15:33:29 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2018-09-25 12:24:11 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse, 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
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from corporate.lib.stripe import (
|
2023-11-13 15:05:56 +01:00
|
|
|
RealmBillingSession,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_change_plan_status,
|
2020-08-13 13:06:05 +02:00
|
|
|
downgrade_at_the_end_of_billing_cycle,
|
2020-08-13 10:22:58 +02:00
|
|
|
downgrade_now_without_creating_additional_invoices,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_latest_seat_count,
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan,
|
2020-12-17 16:33:19 +01:00
|
|
|
validate_licenses,
|
2020-06-11 00:54:34 +02:00
|
|
|
)
|
2023-11-16 16:14:43 +01:00
|
|
|
from corporate.models import CustomerPlan, get_current_plan_by_realm, get_customer_by_realm
|
2021-07-15 16:38:37 +02:00
|
|
|
from zerver.decorator import require_billing_access, zulip_login_required
|
2021-06-30 18:35:50 +02: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-11-22 12:02:09 +01:00
|
|
|
from zerver.lib.validator import check_int, check_int_in, check_string
|
2023-02-13 20:40:51 +01:00
|
|
|
from zerver.models import Realm, UserProfile
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-02-13 20:40:51 +01:00
|
|
|
def add_sponsorship_info_to_context(context: Dict[str, Any], user_profile: UserProfile) -> None:
|
|
|
|
def key_helper(d: Any) -> int:
|
|
|
|
return d[1]["display_order"]
|
|
|
|
|
|
|
|
context.update(
|
|
|
|
realm_org_type=user_profile.realm.org_type,
|
|
|
|
sorted_org_types=sorted(
|
|
|
|
(
|
|
|
|
[org_type_name, org_type]
|
|
|
|
for (org_type_name, org_type) in Realm.ORG_TYPES.items()
|
|
|
|
if not org_type.get("hidden")
|
|
|
|
),
|
|
|
|
key=key_helper,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-02 09:36:27 +01:00
|
|
|
@zulip_login_required
|
|
|
|
@has_request_variables
|
|
|
|
def sponsorship_request(request: HttpRequest) -> HttpResponse:
|
|
|
|
user = request.user
|
|
|
|
assert user.is_authenticated
|
|
|
|
context: Dict[str, Any] = {}
|
2023-11-02 16:34:37 +01:00
|
|
|
|
|
|
|
customer = get_customer_by_realm(user.realm)
|
|
|
|
if customer is not None and customer.sponsorship_pending:
|
|
|
|
context["is_sponsorship_pending"] = True
|
|
|
|
|
|
|
|
if user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:
|
|
|
|
context["is_sponsored"] = True
|
|
|
|
|
2023-11-02 09:36:27 +01:00
|
|
|
add_sponsorship_info_to_context(context, user)
|
|
|
|
return render(request, "corporate/sponsorship.html", context=context)
|
|
|
|
|
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
@zulip_login_required
|
2021-07-29 19:01:39 +02:00
|
|
|
@has_request_variables
|
|
|
|
def billing_home(
|
2023-11-13 15:56:10 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
success_message: str = REQ(default="", str_validator=check_string),
|
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
|
|
|
|
|
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,
|
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"))
|
|
|
|
|
2023-11-04 07:23:15 +01:00
|
|
|
PAID_PLANS = [
|
|
|
|
Realm.PLAN_TYPE_STANDARD,
|
|
|
|
Realm.PLAN_TYPE_PLUS,
|
|
|
|
]
|
|
|
|
|
2023-11-02 16:34:37 +01:00
|
|
|
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
|
|
|
|
if user.realm.plan_type not in PAID_PLANS:
|
|
|
|
return HttpResponseRedirect(reverse("sponsorship_request"))
|
|
|
|
# If the realm is on a paid plan, show the sponsorship pending message
|
|
|
|
# TODO: Add a sponsorship pending message to the billing page
|
|
|
|
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"))
|
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
if customer is None:
|
2021-07-15 16:38:37 +02:00
|
|
|
from corporate.views.upgrade import initial_upgrade
|
|
|
|
|
2020-09-22 02:54:44 +02:00
|
|
|
return HttpResponseRedirect(reverse(initial_upgrade))
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
if not CustomerPlan.objects.filter(customer=customer).exists():
|
2021-07-15 16:38:37 +02:00
|
|
|
from corporate.views.upgrade import initial_upgrade
|
|
|
|
|
2020-09-22 02:54:44 +02:00
|
|
|
return HttpResponseRedirect(reverse(initial_upgrade))
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2023-11-16 16:14:43 +01:00
|
|
|
billing_session = RealmBillingSession(user=None, realm=user.realm)
|
|
|
|
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
|
|
|
|
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",
|
|
|
|
json_validator=check_int_in(
|
|
|
|
[
|
|
|
|
CustomerPlan.ACTIVE,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
|
2023-11-20 13:01:25 +01:00
|
|
|
CustomerPlan.SWITCH_TO_MONTHLY_AT_END_OF_CYCLE,
|
2021-04-14 15:50:40 +02:00
|
|
|
CustomerPlan.ENDED,
|
|
|
|
]
|
|
|
|
),
|
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
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-03-24 14:22:27 +01:00
|
|
|
plan = get_current_plan_by_realm(user.realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
assert plan is not None # for mypy
|
2020-04-23 20:10:15 +02:00
|
|
|
|
2023-11-13 15:05:56 +01:00
|
|
|
realm = plan.customer.realm
|
|
|
|
billing_session = RealmBillingSession(user=None, realm=realm)
|
|
|
|
new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
|
|
|
|
plan, timezone_now()
|
|
|
|
)
|
2020-12-17 16:35:33 +01:00
|
|
|
if new_plan is not None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-17 16:35:33 +01:00
|
|
|
_("Unable to update the plan. The plan has been expired and replaced with a new plan.")
|
|
|
|
)
|
|
|
|
|
|
|
|
if last_ledger_entry is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Unable to update the plan. The plan has ended."))
|
2020-12-17 16:35:33 +01:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
if status is not None:
|
|
|
|
if status == CustomerPlan.ACTIVE:
|
2023-11-20 13:01:25 +01:00
|
|
|
assert plan.status < CustomerPlan.LIVE_STATUS_THRESHOLD
|
2020-12-10 18:15:09 +01:00
|
|
|
do_change_plan_status(plan, status)
|
|
|
|
elif status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE:
|
2023-11-20 13:01:25 +01:00
|
|
|
assert plan.status < CustomerPlan.LIVE_STATUS_THRESHOLD
|
2020-12-10 18:15:09 +01:00
|
|
|
downgrade_at_the_end_of_billing_cycle(user.realm)
|
|
|
|
elif status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE:
|
|
|
|
assert plan.billing_schedule == CustomerPlan.MONTHLY
|
2023-11-20 13:01:25 +01:00
|
|
|
assert plan.status < CustomerPlan.LIVE_STATUS_THRESHOLD
|
|
|
|
# Customer needs to switch to an active plan first to avoid unexpected behavior.
|
|
|
|
assert plan.status != CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
|
|
|
|
assert plan.fixed_price is None
|
|
|
|
do_change_plan_status(plan, status)
|
|
|
|
elif status == CustomerPlan.SWITCH_TO_MONTHLY_AT_END_OF_CYCLE:
|
|
|
|
assert plan.billing_schedule == CustomerPlan.ANNUAL
|
|
|
|
assert plan.status < CustomerPlan.LIVE_STATUS_THRESHOLD
|
|
|
|
# Customer needs to switch to an active plan first to avoid unexpected behavior.
|
|
|
|
assert plan.status != CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
|
2020-12-10 18:15:09 +01:00
|
|
|
assert plan.fixed_price is None
|
|
|
|
do_change_plan_status(plan, status)
|
|
|
|
elif status == CustomerPlan.ENDED:
|
2020-11-11 14:02:47 +01:00
|
|
|
assert plan.is_free_trial()
|
2020-12-10 18:15:09 +01:00
|
|
|
downgrade_now_without_creating_additional_invoices(user.realm)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-10 18:15:09 +01:00
|
|
|
|
2020-12-23 17:08:27 +01:00
|
|
|
if licenses is not None:
|
|
|
|
if plan.automanage_licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Unable to update licenses manually. Your plan is on automatic license management."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses == licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Your plan is already on {licenses} licenses in the current billing period."
|
|
|
|
).format(licenses=licenses)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses > licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2023-07-18 00:51:14 +02:00
|
|
|
_("You cannot decrease the licenses in the current billing period.")
|
2020-12-23 17:08:27 +01:00
|
|
|
)
|
2023-04-10 21:48:52 +02:00
|
|
|
validate_licenses(
|
|
|
|
plan.charge_automatically,
|
|
|
|
licenses,
|
|
|
|
get_latest_seat_count(user.realm),
|
|
|
|
plan.customer.exempt_from_license_number_check,
|
|
|
|
)
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan(plan, timezone_now(), licenses=licenses)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
if licenses_at_next_renewal is not None:
|
|
|
|
if plan.automanage_licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Unable to update licenses manually. Your plan is on automatic license management."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses_at_next_renewal == licenses_at_next_renewal:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Your plan is already scheduled to renew with {licenses_at_next_renewal} licenses."
|
|
|
|
).format(licenses_at_next_renewal=licenses_at_next_renewal)
|
|
|
|
)
|
2021-07-04 08:19:18 +02:00
|
|
|
validate_licenses(
|
|
|
|
plan.charge_automatically,
|
|
|
|
licenses_at_next_renewal,
|
|
|
|
get_latest_seat_count(user.realm),
|
2023-04-10 21:48:52 +02:00
|
|
|
plan.customer.exempt_from_license_number_check,
|
2021-07-04 08:19:18 +02:00
|
|
|
)
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan(
|
|
|
|
plan, timezone_now(), licenses_at_next_renewal=licenses_at_next_renewal
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-23 17:08:27 +01:00
|
|
|
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Nothing to change."))
|