diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 44737e42b5..f241c15a63 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -9,6 +9,7 @@ from decimal import Decimal from enum import Enum from functools import wraps from typing import Any, Callable, Dict, Generator, Optional, Tuple, TypedDict, TypeVar, Union +from urllib.parse import urlencode, urljoin import stripe from django import forms @@ -44,12 +45,14 @@ from zerver.lib.send_email import ( send_email_to_billing_admins_and_realm_owners, ) from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime +from zerver.lib.url_encoding import append_url_query_string from zerver.lib.utils import assert_is_not_none from zerver.models import ( Realm, RealmAuditLog, UserProfile, get_org_type_display_name, + get_realm, get_system_bot, ) from zilencer.models import ( @@ -374,6 +377,14 @@ def payment_method_string(stripe_customer: stripe.Customer) -> str: ) # nocoverage +def build_support_url(support_view: str, query_text: str) -> str: + support_realm_url = get_realm(settings.STAFF_SUBDOMAIN).uri + support_url = urljoin(support_realm_url, reverse(support_view)) + query = urlencode({"q": query_text}) + support_url = append_url_query_string(support_url, query) + return support_url + + class BillingError(JsonableError): data_fields = ["error_description"] # error messages @@ -2408,10 +2419,7 @@ class RealmBillingSession(BillingSession): @override def support_url(self) -> str: - # TODO: Refactor to not create an import cycle. - from corporate.lib.support import get_support_url - - return get_support_url(self.realm) + return build_support_url("support", self.realm.string_id) @override def get_customer(self) -> Optional[Customer]: @@ -2706,7 +2714,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage @override def support_url(self) -> str: - return "TODO:not-implemented" + return build_support_url("remote_servers_support", self.remote_realm.server.hostname) @override def get_customer(self) -> Optional[Customer]: @@ -2998,7 +3006,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage @override def support_url(self) -> str: - return "TODO:not-implemented" + return build_support_url("remote_servers_support", self.remote_server.hostname) @override def get_customer(self) -> Optional[Customer]: diff --git a/corporate/lib/support.py b/corporate/lib/support.py index 4653703141..d41e8c10cb 100644 --- a/corporate/lib/support.py +++ b/corporate/lib/support.py @@ -23,7 +23,7 @@ class PlanData: warning: Optional[str] = None -def get_support_url(realm: Realm) -> str: +def get_realm_support_url(realm: Realm) -> str: support_realm_uri = get_realm(settings.STAFF_SUBDOMAIN).uri support_url = urljoin( support_realm_uri, diff --git a/corporate/views/support.py b/corporate/views/support.py index 316c14b382..b3a5d8ef88 100644 --- a/corporate/views/support.py +++ b/corporate/views/support.py @@ -2,7 +2,7 @@ from django import forms from django.http import HttpRequest, HttpResponse from django.shortcuts import render -from corporate.lib.support import get_support_url +from corporate.lib.support import get_realm_support_url from zerver.decorator import zulip_login_required from zerver.lib.request import has_request_variables from zerver.lib.send_email import FromAddress, send_email @@ -38,7 +38,7 @@ def support_request(request: HttpRequest) -> HttpResponse: "realm_string_id": user.realm.string_id, "request_subject": form.cleaned_data["request_subject"], "request_message": form.cleaned_data["request_message"], - "support_url": get_support_url(user.realm), + "support_url": get_realm_support_url(user.realm), "user_role": user.get_role_name(), } # Sent to the server's support team, so this email is not user-facing. diff --git a/zerver/actions/create_realm.py b/zerver/actions/create_realm.py index 377223974d..deeb6ca342 100644 --- a/zerver/actions/create_realm.py +++ b/zerver/actions/create_realm.py @@ -38,7 +38,7 @@ from zerver.models import ( from zproject.backends import all_implemented_backend_names if settings.CORPORATE_ENABLED: - from corporate.lib.support import get_support_url + from corporate.lib.support import get_realm_support_url def do_change_realm_subdomain( @@ -310,7 +310,7 @@ def do_create_realm( admin_realm = get_realm(settings.SYSTEM_BOT_REALM) sender = get_system_bot(settings.NOTIFICATION_BOT, admin_realm.id) - support_url = get_support_url(realm) + support_url = get_realm_support_url(realm) organization_type = get_org_type_display_name(realm.org_type) message = "[{name}]({support_link}) ([{subdomain}]({realm_link})). Organization type: {type}".format(