2018-09-25 12:24:11 +02:00
|
|
|
import logging
|
2023-11-20 08:40:09 +01:00
|
|
|
from typing import Optional
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
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
|
2023-11-24 09:12:17 +01:00
|
|
|
from pydantic import Json
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2023-11-26 18:34:33 +01:00
|
|
|
from corporate.lib.decorator import authenticated_remote_realm_management_endpoint
|
2020-06-11 00:54:34 +02:00
|
|
|
from corporate.lib.stripe import (
|
2023-11-14 11:59:48 +01:00
|
|
|
VALID_BILLING_MODALITY_VALUES,
|
|
|
|
VALID_BILLING_SCHEDULE_VALUES,
|
|
|
|
VALID_LICENSE_MANAGEMENT_VALUES,
|
2020-06-11 00:54:34 +02:00
|
|
|
BillingError,
|
2023-11-20 08:40:09 +01:00
|
|
|
InitialUpgradeRequest,
|
2023-10-26 14:11:43 +02:00
|
|
|
RealmBillingSession,
|
2023-11-24 09:12:17 +01:00
|
|
|
RemoteRealmBillingSession,
|
2023-11-14 11:59:48 +01:00
|
|
|
UpgradeRequest,
|
2020-06-11 00:54:34 +02:00
|
|
|
)
|
2023-11-30 01:48:46 +01:00
|
|
|
from corporate.models import CustomerPlan
|
2023-11-26 18:29:51 +01:00
|
|
|
from zerver.decorator import require_organization_member, 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-11-24 09:12:17 +01:00
|
|
|
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
|
2021-07-29 19:01:59 +02:00
|
|
|
from zerver.lib.validator import check_bool, check_int, check_string_in
|
2023-11-30 01:48:46 +01:00
|
|
|
from zerver.models import UserProfile
|
2023-11-26 18:34:33 +01:00
|
|
|
from zilencer.models import RemoteRealm
|
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
|
|
|
|
2018-12-22 05:29:25 +01:00
|
|
|
|
2020-07-15 22:18:32 +02:00
|
|
|
@require_organization_member
|
2018-12-07 18:43:22 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def upgrade(
|
|
|
|
request: HttpRequest,
|
|
|
|
user: UserProfile,
|
2021-04-09 12:43:44 +02:00
|
|
|
billing_modality: str = REQ(str_validator=check_string_in(VALID_BILLING_MODALITY_VALUES)),
|
|
|
|
schedule: str = REQ(str_validator=check_string_in(VALID_BILLING_SCHEDULE_VALUES)),
|
|
|
|
signed_seat_count: str = REQ(),
|
|
|
|
salt: str = REQ(),
|
|
|
|
license_management: Optional[str] = REQ(
|
|
|
|
default=None, str_validator=check_string_in(VALID_LICENSE_MANAGEMENT_VALUES)
|
|
|
|
),
|
2021-04-07 22:00:44 +02:00
|
|
|
licenses: Optional[int] = REQ(json_validator=check_int, default=None),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-12-07 18:43:22 +01:00
|
|
|
try:
|
2023-11-14 11:59:48 +01:00
|
|
|
upgrade_request = UpgradeRequest(
|
|
|
|
billing_modality=billing_modality,
|
|
|
|
schedule=schedule,
|
|
|
|
signed_seat_count=signed_seat_count,
|
|
|
|
salt=salt,
|
|
|
|
license_management=license_management,
|
|
|
|
licenses=licenses,
|
2023-04-10 21:48:52 +02:00
|
|
|
)
|
2023-11-13 07:55:57 +01:00
|
|
|
billing_session = RealmBillingSession(user)
|
2023-11-14 11:59:48 +01:00
|
|
|
data = billing_session.do_upgrade(upgrade_request)
|
|
|
|
return json_success(request, data)
|
2021-08-29 15:33:29 +02:00
|
|
|
except BillingError as e:
|
|
|
|
billing_logger.warning(
|
|
|
|
"BillingError during upgrade: %s. user=%s, realm=%s (%s), billing_modality=%s, "
|
|
|
|
"schedule=%s, license_management=%s, licenses=%s",
|
|
|
|
e.error_description,
|
|
|
|
user.id,
|
|
|
|
user.realm.id,
|
|
|
|
user.realm.string_id,
|
|
|
|
billing_modality,
|
|
|
|
schedule,
|
|
|
|
license_management,
|
|
|
|
licenses,
|
|
|
|
)
|
|
|
|
raise e
|
2020-06-12 01:35:37 +02:00
|
|
|
except Exception:
|
2020-08-11 03:19:00 +02:00
|
|
|
billing_logger.exception("Uncaught exception in billing:", stack_info=True)
|
2020-10-17 03:42:50 +02:00
|
|
|
error_message = BillingError.CONTACT_SUPPORT.format(email=settings.ZULIP_ADMINISTRATOR)
|
2018-12-07 18:43:22 +01:00
|
|
|
error_description = "uncaught exception during upgrade"
|
2021-07-04 08:19:18 +02:00
|
|
|
raise BillingError(error_description, error_message)
|
2018-12-07 18:43:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
@zulip_login_required
|
2021-07-29 19:01:59 +02:00
|
|
|
@has_request_variables
|
2023-11-23 09:31:46 +01:00
|
|
|
def upgrade_page(
|
2023-11-10 10:59:28 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
manual_license_management: bool = REQ(default=False, json_validator=check_bool),
|
2021-07-29 19:01:59 +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-06-09 12:24:32 +02:00
|
|
|
|
2020-07-15 22:18:32 +02:00
|
|
|
if not settings.BILLING_ENABLED or user.is_guest:
|
|
|
|
return render(request, "404.html", status=404)
|
|
|
|
|
2023-11-20 08:40:09 +01:00
|
|
|
initial_upgrade_request = InitialUpgradeRequest(
|
|
|
|
manual_license_management=manual_license_management,
|
2023-11-22 07:36:24 +01:00
|
|
|
tier=CustomerPlan.STANDARD,
|
2023-04-10 21:48:52 +02:00
|
|
|
)
|
2023-11-20 08:40:09 +01:00
|
|
|
billing_session = RealmBillingSession(user)
|
2023-11-20 20:32:29 +01:00
|
|
|
redirect_url, context = billing_session.get_initial_upgrade_context(initial_upgrade_request)
|
2023-04-10 21:48:52 +02:00
|
|
|
|
2023-11-20 08:40:09 +01:00
|
|
|
if redirect_url:
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
2023-02-13 20:40:51 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
response = render(request, "corporate/upgrade.html", context=context)
|
2018-09-25 12:24:11 +02:00
|
|
|
return response
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-11-26 18:34:33 +01:00
|
|
|
@authenticated_remote_realm_management_endpoint
|
2023-11-24 09:12:17 +01:00
|
|
|
@typed_endpoint
|
|
|
|
def remote_realm_upgrade_page(
|
|
|
|
request: HttpRequest,
|
2023-11-26 18:34:33 +01:00
|
|
|
remote_realm: RemoteRealm,
|
2023-11-24 09:12:17 +01:00
|
|
|
*,
|
|
|
|
realm_uuid: PathOnly[str],
|
|
|
|
manual_license_management: Json[bool] = False,
|
|
|
|
) -> HttpResponse: # nocoverage
|
|
|
|
initial_upgrade_request = InitialUpgradeRequest(
|
|
|
|
manual_license_management=manual_license_management,
|
|
|
|
tier=CustomerPlan.STANDARD,
|
|
|
|
)
|
|
|
|
billing_session = RemoteRealmBillingSession(remote_realm)
|
|
|
|
redirect_url, context = billing_session.get_initial_upgrade_context(initial_upgrade_request)
|
|
|
|
|
|
|
|
if redirect_url:
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
|
|
|
|
|
|
|
response = render(request, "corporate/upgrade.html", context=context)
|
|
|
|
return response
|