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
|
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,
|
|
|
|
UpdatePlanRequest,
|
|
|
|
)
|
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
|
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-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-11-27 17:31:39 +01:00
|
|
|
# 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)
|
|
|
|
|
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 = "",
|
|
|
|
) -> HttpResponse: # nocoverage
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if billing_session.remote_realm.plan_type == RemoteRealm.PLAN_TYPE_COMMUNITY:
|
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()
|
|
|
|
if customer is not None and customer.sponsorship_pending:
|
|
|
|
# Don't redirect to sponsorship page if the remote realm is on a paid plan
|
|
|
|
if not billing_session.on_paid_plan():
|
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
|
|
|
|
|
|
|
|
if billing_session.remote_realm.plan_type == RemoteRealm.PLAN_TYPE_SELF_HOSTED:
|
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-06 05:56:20 +01:00
|
|
|
if customer is None or get_current_plan_by_customer(customer) is None:
|
2023-12-05 12:35:21 +01:00
|
|
|
return HttpResponseRedirect(reverse("remote_realm_upgrade_page", args=(realm_uuid,)))
|
2023-12-01 10:43:04 +01:00
|
|
|
|
|
|
|
main_context = billing_session.get_billing_page_context()
|
|
|
|
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 = "",
|
|
|
|
) -> HttpResponse: # nocoverage
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if billing_session.remote_server.plan_type == RemoteZulipServer.PLAN_TYPE_COMMUNITY:
|
|
|
|
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:
|
|
|
|
# Don't redirect to sponsorship page if the remote realm is on a paid plan
|
|
|
|
if not billing_session.on_paid_plan():
|
|
|
|
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
|
|
|
|
context["sponsorship_pending"] = True
|
|
|
|
|
|
|
|
if (
|
|
|
|
billing_session.remote_server.plan_type == RemoteZulipServer.PLAN_TYPE_SELF_HOSTED
|
|
|
|
or customer is None
|
2023-12-06 05:56:20 +01:00
|
|
|
or get_current_plan_by_customer(customer) is None
|
2023-12-01 10:43:04 +01:00
|
|
|
):
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"remote_server_upgrade_page",
|
|
|
|
kwargs={"server_uuid": billing_session.remote_server.uuid},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
main_context = billing_session.get_billing_page_context()
|
|
|
|
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),
|
|
|
|
) -> HttpResponse: # nocoverage
|
|
|
|
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),
|
|
|
|
) -> HttpResponse: # nocoverage
|
|
|
|
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)
|