event_status: Pass billing_base_url to calculate realm specific URLs.

This commit is contained in:
Aman Agrawal
2023-12-01 03:45:05 +00:00
committed by Tim Abbott
parent bb7b0b6731
commit 9889dc38fe
5 changed files with 33 additions and 28 deletions

View File

@@ -10,6 +10,7 @@ from corporate.views.event_status import (
event_status_page,
remote_realm_event_status,
remote_realm_event_status_page,
remote_server_event_status_page,
)
from corporate.views.portico import (
app_download_link_redirect,
@@ -199,6 +200,11 @@ urlpatterns += [
remote_realm_event_status_page,
name="remote_realm_event_status_page",
),
path(
"server/<server_uuid>/billing/event_status",
remote_server_event_status_page,
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),

View File

@@ -61,6 +61,7 @@ def event_status_page(
context = {
"stripe_session_id": stripe_session_id,
"stripe_payment_intent_id": stripe_payment_intent_id,
"billing_base_url": "",
}
return render(request, "corporate/event_status.html", context=context)
@@ -78,5 +79,24 @@ def remote_realm_event_status_page(
context = {
"stripe_session_id": stripe_session_id,
"stripe_payment_intent_id": stripe_payment_intent_id,
"billing_base_url": f"/realm/{realm_uuid}",
}
return render(request, "corporate/event_status.html", context=context)
@self_hosting_management_endpoint
@typed_endpoint
def remote_server_event_status_page(
request: HttpRequest,
*,
realm_uuid: str = "",
server_uuid: str = "",
stripe_session_id: str = "",
stripe_payment_intent_id: str = "",
) -> HttpResponse: # nocoverage
context = {
"stripe_session_id": stripe_session_id,
"stripe_payment_intent_id": stripe_payment_intent_id,
"billing_base_url": f"/server/{server_uuid}",
}
return render(request, "corporate/event_status.html", context=context)

View File

@@ -18,7 +18,7 @@
<div class="page-content">
<div class="main">
<br />
<div id="data" data-stripe-session-id="{{ stripe_session_id}}" data-stripe-payment-intent-id="{{ stripe_payment_intent_id }}"></div>
<div id="data" data-stripe-session-id="{{ stripe_session_id}}" data-stripe-payment-intent-id="{{ stripe_payment_intent_id }}" data-billing-base-url="{{ billing_base_url }}"></div>
<div class="alert alert-success" id="webhook-success"></div>
<div class="alert alert-danger" id="webhook-error"></div>
<div id="webhook-loading">

View File

@@ -5,6 +5,8 @@ import * as loading from "../loading";
import * as helpers from "./helpers";
const billing_base_url = $("#data").attr("data-billing-base-url")!;
const stripe_response_schema = z.object({
session: z.object({
type: z.string(),
@@ -45,6 +47,7 @@ function handle_session_complete_event(session: StripeSession): void {
case "card_update_from_upgrade_page":
redirect_to = helpers.get_upgrade_page_url(
session.is_manual_license_management_upgrade_session,
billing_base_url,
);
break;
}
@@ -52,7 +55,7 @@ function handle_session_complete_event(session: StripeSession): void {
}
async function stripe_checkout_session_status_check(stripe_session_id: string): Promise<boolean> {
const response: unknown = await $.get(helpers.get_event_status_url(), {
const response: unknown = await $.get(`/json${billing_base_url}/billing/event/status`, {
stripe_session_id,
});
const response_data = stripe_response_schema.parse(response);

View File

@@ -160,37 +160,13 @@ export function redirect_to_billing_with_successful_upgrade(): void {
);
}
export function get_realm_uuid_from_url(): string | undefined {
const url = new URL(window.location.href);
const path_segments = url.pathname.split("/");
const realm_index = path_segments.indexOf("realm");
if (realm_index !== -1 && realm_index < path_segments.length - 1) {
return path_segments[realm_index + 1];
}
return undefined;
}
export function get_event_status_url(): string {
const realm_uuid = get_realm_uuid_from_url();
if (realm_uuid) {
return `/json/realm/${realm_uuid}/billing/event/status`;
}
return "/json/billing/event/status";
}
export function get_upgrade_page_url(
is_manual_license_management_upgrade_session: boolean | undefined,
billing_base_url: string,
): string {
let redirect_to = "/upgrade";
if (is_manual_license_management_upgrade_session) {
redirect_to += "/?manual_license_management=true";
}
const realm_uuid = get_realm_uuid_from_url();
if (realm_uuid) {
return `/realm/${realm_uuid}` + redirect_to;
}
return redirect_to;
return billing_base_url + redirect_to;
}