sponsorship: Populate sponsorship page with correct context.

Fixes sponsorship page to work for remote realm and server.
This commit is contained in:
Aman Agrawal
2023-12-05 06:41:34 +00:00
committed by Tim Abbott
parent 044cb820f8
commit 49908ba166
6 changed files with 55 additions and 19 deletions

View File

@@ -2162,8 +2162,28 @@ class BillingSession(ABC):
raise JsonableError(_("Pass stripe_session_id or stripe_payment_intent_id"))
def get_sponsorship_request_context(self) -> Optional[Dict[str, Any]]:
context: Dict[str, Any] = {}
customer = self.get_customer()
is_remotely_hosted = isinstance(
self, (RemoteRealmBillingSession, RemoteServerBillingSession)
)
# We only support sponsorships for these plans.
sponsored_plan_name = CustomerPlan.name_from_tier(CustomerPlan.TIER_CLOUD_STANDARD)
if is_remotely_hosted:
sponsored_plan_name = CustomerPlan.name_from_tier(
CustomerPlan.TIER_SELF_HOSTED_COMMUNITY
)
plan_name = "Zulip Cloud Free"
if is_remotely_hosted:
plan_name = "Self-managed"
context: Dict[str, Any] = {
"billing_base_url": self.billing_base_url,
"is_remotely_hosted": is_remotely_hosted,
"sponsored_plan_name": sponsored_plan_name,
"plan_name": plan_name,
}
if customer is not None and customer.sponsorship_pending:
if self.on_paid_plan():
@@ -2173,18 +2193,13 @@ class BillingSession(ABC):
if self.is_sponsored():
context["is_sponsored"] = True
context["plan_name"] = sponsored_plan_name
if customer is not None:
plan = get_current_plan_by_customer(customer)
if plan is not None:
context["plan_name"] = plan.name
context["free_trial"] = plan.is_free_trial()
elif self.is_sponsored():
# We don't create CustomerPlan objects for fully sponsored realms via support page.
context["plan_name"] = "Zulip Cloud Standard"
else:
# TODO: Don't hardcode this plan name.
context["plan_name"] = "Zulip Cloud Free"
self.add_sponsorship_info_to_context(context)
return context
@@ -2637,6 +2652,7 @@ class RealmBillingSession(BillingSession):
key=sponsorship_org_type_key_helper,
),
)
context["org_name"] = self.realm.name
@override
def get_sponsorship_request_session_specific_context(
@@ -2924,6 +2940,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage
key=sponsorship_org_type_key_helper,
),
)
context["org_name"] = self.remote_realm.host
@override
def get_sponsorship_request_session_specific_context(
@@ -3224,6 +3241,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage
key=sponsorship_org_type_key_helper,
),
)
context["org_name"] = self.remote_server.hostname
@override
def get_sponsorship_request_session_specific_context(

View File

@@ -299,6 +299,7 @@ class CustomerPlan(models.Model):
CustomerPlan.TIER_CLOUD_ENTERPRISE: "Zulip Enterprise",
CustomerPlan.TIER_SELF_HOSTED_LEGACY: "Self-managed",
CustomerPlan.TIER_SELF_HOSTED_BUSINESS: "Zulip Business",
CustomerPlan.TIER_SELF_HOSTED_COMMUNITY: "Community",
}[tier]
@property

View File

@@ -233,8 +233,8 @@ urlpatterns += [
name="remote_server_event_status_page",
),
# Remote variants of above API endpoints.
path("json/realm/<realm_uuid>/sponsorship", remote_realm_sponsorship),
path("json/server/<server_uuid>/sponsorship", remote_server_sponsorship),
path("json/realm/<realm_uuid>/billing/sponsorship", remote_realm_sponsorship),
path("json/server/<server_uuid>/billing/sponsorship", remote_server_sponsorship),
path(
"json/realm/<realm_uuid>/billing/session/start_card_update_session",
start_card_update_stripe_session_for_remote_realm,

View File

@@ -37,7 +37,9 @@ def remote_realm_sponsorship_page(
) -> HttpResponse: # nocoverage
context = billing_session.get_sponsorship_request_context()
if context is None:
return HttpResponseRedirect(reverse("remote_realm_billing_page"))
return HttpResponseRedirect(
reverse("remote_realm_billing_page", args=(billing_session.remote_realm.uuid,))
)
return render(request, "corporate/sponsorship.html", context=context)
@@ -49,7 +51,9 @@ def remote_server_sponsorship_page(
) -> HttpResponse: # nocoverage
context = billing_session.get_sponsorship_request_context()
if context is None:
return HttpResponseRedirect(reverse("remote_server_billing_page"))
return HttpResponseRedirect(
reverse("remote_server_billing_page", args=(billing_session.remote_server.uuid,))
)
return render(request, "corporate/sponsorship.html", context=context)

View File

@@ -11,13 +11,20 @@
<div class="center-block new-style">
<div class="alert alert-success sponsorship-page-success" id="sponsorship-status-success-message-top">
{% if is_sponsored %}
Zulip is sponsoring a free <a href="/plans/">Zulip Cloud Standard</a> plan for this organization. 🎉
Zulip is sponsoring a free <a href="{{ billing_base_url }}/plans/">{{ sponsored_plan_name }}</a> plan for this organization. 🎉
{% else %}
This organization has requested sponsorship for a <a href="/plans/">Zulip Cloud Standard</a> plan. <a href="mailto:support@zulip.com">Contact Zulip support</a> with any questions or updates.
This organization has requested sponsorship for a <a href="{{ billing_base_url }}/plans/">{{ sponsored_plan_name }}</a> plan. <a href="mailto:support@zulip.com">Contact Zulip support</a> with any questions or updates.
{% endif %}
</div>
<div class="pitch">
<h1>Zulip Cloud billing for {{realm_name}}</h1>
<h1>
Zulip
{% if is_remotely_hosted %}
{% else %}
Cloud
{% endif %}
billing for {{ org_name }}
</h1>
</div>
<div class="white-box">
<div id="sponsorship-status-page-details">
@@ -49,16 +56,22 @@
<div class="register-account flex full-page sponsorship-page">
<div class="center-block new-style">
<div class="pitch">
<h1>Request Zulip Cloud sponsorship</h1>
<h1>
Request Zulip
{% if is_remotely_hosted %}
{% else %}
Cloud
{% endif %} sponsorship
</h1>
</div>
<div class="white-box">
<div id="sponsorship-error" class="alert alert-danger"></div>
<div id="sponsorship-input-section">
<form id="sponsorship-form" method="post">
<form id="sponsorship-form" method="post" data-billing-base-url="{{ billing_base_url }}">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" />
<div class="input-box sponsorship-form-field no-validation">
<label for="org-name" class="inline-block label-title">Organization</label>
<div id="org-name" class="not-editable-realm-field">{{ realm_name }}</div>
<div id="org-name" class="not-editable-realm-field">{{ org_name }}</div>
</div>
<div class="input-box sponsorship-form-field">
<div class="inline-block relative">

View File

@@ -53,10 +53,10 @@ function create_ajax_request(): void {
return;
}
const billing_base_url = $form.attr("data-billing-base-url") ?? "";
void $.ajax({
type: "post",
// TODO: This needs to be conditional on billing session type
url: "/json/billing/sponsorship",
url: `/json${billing_base_url}/billing/sponsorship`,
data,
success() {
window.location.reload();