Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

upgrade: Make purchase upgrade work for remove servers and realms.

Aman Agrawald0c0b11fbf5e
diff --git a/corporate/lib/stripe_event_handler.py b/corporate/lib/stripe_event_handler.py
index b10788c2081..1b842dfb48b 100644
--- a/corporate/lib/stripe_event_handler.py
+++ b/corporate/lib/stripe_event_handler.py
@@ -107,10 +107,6 @@ def handle_payment_intent_succeeded_event(
payment_intent.status = PaymentIntent.SUCCEEDED
payment_intent.save()
metadata: Dict[str, Any] = stripe_payment_intent.metadata
- assert payment_intent.customer.realm is not None
- user_id = metadata.get("user_id")
- assert user_id is not None
- user = get_active_user_profile_by_id_in_realm(user_id, payment_intent.customer.realm)
description = ""
charge: stripe.Charge
@@ -140,7 +136,9 @@ def handle_payment_intent_succeeded_event(
stripe.Invoice.finalize_invoice(stripe_invoice)
raise e
- billing_session = RealmBillingSession(user)
+ billing_session = get_billing_session_for_stripe_webhook(
+ payment_intent.customer, metadata.get("user_id")
+ )
billing_session.process_initial_upgrade(
CustomerPlan.TIER_CLOUD_STANDARD,
int(metadata["licenses"]),
diff --git a/corporate/urls.py b/corporate/urls.py
index edbade88745..885314cdc72 100644
--- a/corporate/urls.py
+++ b/corporate/urls.py
@@ -46,7 +46,9 @@ from corporate.views.sponsorship import (
)
from corporate.views.support import support_request
from corporate.views.upgrade import (
+ remote_realm_upgrade,
remote_realm_upgrade_page,
+ remote_server_upgrade,
remote_server_upgrade_page,
upgrade,
upgrade_page,
@@ -230,6 +232,8 @@ urlpatterns += [
),
path("json/realm/<realm_uuid>/billing/event/status", remote_realm_event_status),
path("json/server/<server_uuid>/billing/event/status", remote_server_event_status),
+ path("json/realm/<realm_uuid>/billing/upgrade", remote_realm_upgrade),
+ path("json/server/<server_uuid>/billing/upgrade", remote_server_upgrade),
]
urlpatterns += [
diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py
index d12d4558cc9..5ed43e232af 100644
--- a/corporate/views/upgrade.py
+++ b/corporate/views/upgrade.py
@@ -79,6 +79,96 @@ def upgrade(
raise BillingError(error_description, error_message)
+@authenticated_remote_realm_management_endpoint
+@has_request_variables
+def remote_realm_upgrade(
+ request: HttpRequest,
+ billing_session: RemoteRealmBillingSession,
+ 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)
+ ),
+ licenses: Optional[int] = REQ(json_validator=check_int, default=None),
+) -> HttpResponse: # nocoverage
+ try:
+ upgrade_request = UpgradeRequest(
+ billing_modality=billing_modality,
+ schedule=schedule,
+ signed_seat_count=signed_seat_count,
+ salt=salt,
+ license_management=license_management,
+ licenses=licenses,
+ )
+ data = billing_session.do_upgrade(upgrade_request)
+ return json_success(request, data)
+ except BillingError as e:
+ billing_logger.warning(
+ "BillingError during upgrade: %s. remote_realm=%s (%s), billing_modality=%s, "
+ "schedule=%s, license_management=%s, licenses=%s",
+ e.error_description,
+ billing_session.remote_realm.id,
+ billing_session.remote_realm.host,
+ billing_modality,
+ schedule,
+ license_management,
+ licenses,
+ )
+ raise e
+ except Exception:
+ billing_logger.exception("Uncaught exception in billing:", stack_info=True)
+ error_message = BillingError.CONTACT_SUPPORT.format(email=settings.ZULIP_ADMINISTRATOR)
+ error_description = "uncaught exception during upgrade"
+ raise BillingError(error_description, error_message)
+
+
+@authenticated_remote_server_management_endpoint
+@has_request_variables
+def remote_server_upgrade(
+ request: HttpRequest,
+ billing_session: RemoteServerBillingSession,
+ 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)
+ ),
+ licenses: Optional[int] = REQ(json_validator=check_int, default=None),
+) -> HttpResponse: # nocoverage
+ try:
+ upgrade_request = UpgradeRequest(
+ billing_modality=billing_modality,
+ schedule=schedule,
+ signed_seat_count=signed_seat_count,
+ salt=salt,
+ license_management=license_management,
+ licenses=licenses,
+ )
+ data = billing_session.do_upgrade(upgrade_request)
+ return json_success(request, data)
+ except BillingError as e:
+ billing_logger.warning(
+ "BillingError during upgrade: %s. remote_server=%s (%s), billing_modality=%s, "
+ "schedule=%s, license_management=%s, licenses=%s",
+ e.error_description,
+ billing_session.remote_server.id,
+ billing_session.remote_server.hostname,
+ billing_modality,
+ schedule,
+ license_management,
+ licenses,
+ )
+ raise e
+ except Exception:
+ billing_logger.exception("Uncaught exception in billing:", stack_info=True)
+ error_message = BillingError.CONTACT_SUPPORT.format(email=settings.ZULIP_ADMINISTRATOR)
+ error_description = "uncaught exception during upgrade"
+ raise BillingError(error_description, error_message)
+
+
@zulip_login_required
@has_request_variables
def upgrade_page(
diff --git a/web/src/billing/event_status.ts b/web/src/billing/event_status.ts
index e4519c599d0..293ab2e8f3a 100644
--- a/web/src/billing/event_status.ts
+++ b/web/src/billing/event_status.ts
@@ -81,7 +81,9 @@ async function stripe_checkout_session_status_check(stripe_session_id: string):
export async function stripe_payment_intent_status_check(
stripe_payment_intent_id: string,
): Promise<boolean> {
- const response: unknown = await $.get("/json/billing/event/status", {stripe_payment_intent_id});
+ const response: unknown = await $.get(`/json${billing_base_url}/billing/event/status`, {
+ stripe_payment_intent_id,
+ });
const response_schema = z.object({
payment_intent: z.object({
@@ -108,7 +110,7 @@ export async function stripe_payment_intent_status_check(
switch (response_data.payment_intent.status) {
case "succeeded":
if (response_data.payment_intent.event_handler!.status === "succeeded") {
- helpers.redirect_to_billing_with_successful_upgrade();
+ helpers.redirect_to_billing_with_successful_upgrade(billing_base_url);
return true;
}
if (response_data.payment_intent.event_handler!.status === "failed") {
diff --git a/web/src/billing/helpers.ts b/web/src/billing/helpers.ts
index 9f34bde76dd..ab336c7fdba 100644
--- a/web/src/billing/helpers.ts
+++ b/web/src/billing/helpers.ts
@@ -153,9 +153,10 @@ export function is_valid_input(elem: JQuery<HTMLFormElement>): boolean {
return elem[0].checkValidity();
}
-export function redirect_to_billing_with_successful_upgrade(): void {
+export function redirect_to_billing_with_successful_upgrade(billing_base_url: string): void {
window.location.replace(
- "/billing/?success_message=" +
+ billing_base_url +
+ "/billing/?success_message=" +
encodeURIComponent("Your organization has been upgraded to Zulip Cloud Standard."),
);
}
diff --git a/web/src/billing/upgrade.ts b/web/src/billing/upgrade.ts
index 313b4858d31..4a37c142d52 100644
--- a/web/src/billing/upgrade.ts
+++ b/web/src/billing/upgrade.ts
@@ -86,7 +86,7 @@ export const initialize = (): void => {
$("#org-upgrade-button-text").hide();
$("#org-upgrade-button .upgrade-button-loader").show();
helpers.create_ajax_request(
- "/json/billing/upgrade",
+ `/json${page_params.billing_base_url}/billing/upgrade`,
"autopay",
[],
"POST",
@@ -94,10 +94,12 @@ export const initialize = (): void => {
const response_data = upgrade_response_schema.parse(response);
if (response_data.stripe_payment_intent_id) {
window.location.replace(
- `/billing/event_status?stripe_payment_intent_id=${response_data.stripe_payment_intent_id}`,
+ `${page_params.billing_base_url}/billing/event_status?stripe_payment_intent_id=${response_data.stripe_payment_intent_id}`,
);
} else if (response_data.organization_upgrade_successful) {
- helpers.redirect_to_billing_with_successful_upgrade();
+ helpers.redirect_to_billing_with_successful_upgrade(
+ page_params.billing_base_url,
+ );
}
},
(xhr) => {