Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
support: Use process_support_view_request for plan modifications.
Lauryn Menard5135acd9e34e
diff --git a/analytics/tests/test_support_views.py b/analytics/tests/test_support_views.pyindex 092eb02ef86..0edf07fcfd6 100644--- a/analytics/tests/test_support_views.py+++ b/analytics/tests/test_support_views.py@@ -713,77 +713,84 @@ class TestSupportEndpoint(ZulipTestCase):["Subdomain reserved. Please choose a different one."], result)- def test_downgrade_realm(self) -> None:+ def test_modify_plan_for_downgrade_at_end_of_billing_cycle(self) -> None:+ realm = get_realm("zulip")cordelia = self.example_user("cordelia")self.login_user(cordelia)result = self.client_post(- "/activity/support", {"realm_id": f"{cordelia.realm_id}", "plan_type": "2"}+ "/activity/support",+ {"realm_id": f"{realm.id}", "modify_plan": "downgrade_at_billing_cycle_end"},)self.assertEqual(result.status_code, 302)self.assertEqual(result["Location"], "/login/")+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_12345")+ CustomerPlan.objects.create(+ customer=customer,+ status=CustomerPlan.ACTIVE,+ billing_cycle_anchor=timezone_now(),+ billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,+ tier=CustomerPlan.TIER_CLOUD_STANDARD,+ )+iago = self.example_user("iago")self.login_user(iago)- with mock.patch(- "analytics.views.support.RealmBillingSession.downgrade_at_the_end_of_billing_cycle"- ) as m:+ with self.assertLogs("corporate.stripe", "INFO") as m:result = self.client_post("/activity/support",{- "realm_id": f"{iago.realm_id}",+ "realm_id": f"{realm.id}","modify_plan": "downgrade_at_billing_cycle_end",},)- m.assert_called_once()self.assert_in_success_response(["zulip marked for downgrade at the end of billing cycle"], result)+ plan = get_current_plan_by_realm(realm)+ assert plan is not None+ self.assertEqual(plan.status, CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE)+ expected_log = f"INFO:corporate.stripe:Change plan status: Customer.id: {customer.id}, CustomerPlan.id: {plan.id}, status: {CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE}"+ self.assertEqual(m.output[0], expected_log)- with mock.patch(- "analytics.views.support.RealmBillingSession.downgrade_now_without_creating_additional_invoices"- ) as m:- result = self.client_post(- "/activity/support",- {- "realm_id": f"{iago.realm_id}",- "modify_plan": "downgrade_now_without_additional_licenses",- },- )- m.assert_called_once()- self.assert_in_success_response(- ["zulip downgraded without creating additional invoices"], result- )+ def test_modify_plan_for_downgrade_now_without_additional_licenses(self) -> None:+ realm = get_realm("zulip")+ cordelia = self.example_user("cordelia")+ self.login_user(cordelia)+ result = self.client_post(+ "/activity/support",+ {"realm_id": f"{realm.id}", "modify_plan": "downgrade_now_without_additional_licenses"},+ )+ self.assertEqual(result.status_code, 302)+ self.assertEqual(result["Location"], "/login/")- with mock.patch(- "analytics.views.support.RealmBillingSession.downgrade_now_without_creating_additional_invoices"- ) as m1:- with mock.patch(- "analytics.views.support.RealmBillingSession.void_all_open_invoices", return_value=1- ) as m2:- result = self.client_post(- "/activity/support",- {- "realm_id": f"{iago.realm_id}",- "modify_plan": "downgrade_now_void_open_invoices",- },- )- m1.assert_called_once()- m2.assert_called_once()- self.assert_in_success_response(- ["zulip downgraded and voided 1 open invoices"], result- )+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_12345")+ plan = CustomerPlan.objects.create(+ customer=customer,+ status=CustomerPlan.ACTIVE,+ billing_cycle_anchor=timezone_now(),+ billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,+ tier=CustomerPlan.TIER_CLOUD_STANDARD,+ )- with mock.patch("analytics.views.support.switch_realm_from_standard_to_plus_plan") as m:- result = self.client_post(- "/activity/support",- {- "realm_id": f"{iago.realm_id}",- "modify_plan": "upgrade_to_plus",- },- )- m.assert_called_once_with(get_realm("zulip"))- self.assert_in_success_response(["zulip upgraded to Plus"], result)+ iago = self.example_user("iago")+ self.login_user(iago)++ result = self.client_post(+ "/activity/support",+ {+ "realm_id": f"{iago.realm_id}",+ "modify_plan": "downgrade_now_without_additional_licenses",+ },+ )+ self.assert_in_success_response(+ ["zulip downgraded without creating additional invoices"], result+ )++ plan.refresh_from_db()+ self.assertEqual(plan.status, CustomerPlan.ENDED)+ realm.refresh_from_db()+ self.assertEqual(realm.plan_type, Realm.PLAN_TYPE_LIMITED)def test_scrub_realm(self) -> None:cordelia = self.example_user("cordelia")diff --git a/analytics/views/support.py b/analytics/views/support.pyindex a1584c34dc1..9ea031a8f00 100644--- a/analytics/views/support.py+++ b/analytics/views/support.py@@ -59,10 +59,7 @@ if settings.BILLING_ENABLED:SupportViewRequest,get_latest_seat_count,)- from corporate.lib.support import (- get_discount_for_realm,- switch_realm_from_standard_to_plus_plan,- )+ from corporate.lib.support import get_discount_for_realmfrom corporate.models import (Customer,CustomerPlan,@@ -130,7 +127,7 @@ VALID_MODIFY_PLAN_METHODS = ["downgrade_at_billing_cycle_end","downgrade_now_without_additional_licenses","downgrade_now_void_open_invoices",- "upgrade_to_plus",+ "upgrade_plan_tier",]VALID_STATUS_VALUES = [@@ -213,6 +210,13 @@ def support(support_type=SupportType.update_billing_modality,billing_modality=billing_modality,)+ elif modify_plan is not None:+ support_view_request = SupportViewRequest(+ support_type=SupportType.modify_plan,+ plan_modification=modify_plan,+ )+ if modify_plan == "upgrade_plan_tier":+ support_view_request["new_plan_tier"] = CustomerPlan.TIER_CLOUD_PLUSelif plan_type is not None:current_plan_type = realm.plan_typedo_change_realm_plan_type(realm, plan_type, acting_user=acting_user)@@ -246,29 +250,6 @@ def support(elif status == "deactivated":do_deactivate_realm(realm, acting_user=acting_user)context["success_message"] = f"{realm.string_id} deactivated."- elif modify_plan is not None:- billing_session = RealmBillingSession(- user=acting_user, realm=realm, support_session=True- )- if modify_plan == "downgrade_at_billing_cycle_end":- billing_session.downgrade_at_the_end_of_billing_cycle()- context[- "success_message"- ] = f"{realm.string_id} marked for downgrade at the end of billing cycle"- elif modify_plan == "downgrade_now_without_additional_licenses":- billing_session.downgrade_now_without_creating_additional_invoices()- context[- "success_message"- ] = f"{realm.string_id} downgraded without creating additional invoices"- elif modify_plan == "downgrade_now_void_open_invoices":- billing_session.downgrade_now_without_creating_additional_invoices()- voided_invoices_count = billing_session.void_all_open_invoices()- context[- "success_message"- ] = f"{realm.string_id} downgraded and voided {voided_invoices_count} open invoices"- elif modify_plan == "upgrade_to_plus":- switch_realm_from_standard_to_plus_plan(realm)- context["success_message"] = f"{realm.string_id} upgraded to Plus"elif scrub_realm:do_scrub_realm(realm, acting_user=acting_user)context["success_message"] = f"{realm.string_id} scrubbed."diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex f2f5a1cee16..13d591a2a04 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -527,6 +527,7 @@ class SupportType(Enum):update_sponsorship_status = 2attach_discount = 3update_billing_modality = 4+ modify_plan = 5class SupportViewRequest(TypedDict, total=False):@@ -534,6 +535,8 @@ class SupportViewRequest(TypedDict, total=False):sponsorship_status: Optional[bool]discount: Optional[Decimal]billing_modality: Optional[str]+ plan_modification: Optional[str]+ new_plan_tier: Optional[int]class AuditLogEventType(Enum):@@ -1859,7 +1862,7 @@ class BillingSession(ABC):plan.next_invoice_date = next_invoice_date(plan)plan.save(update_fields=["next_invoice_date"])- def do_change_plan_to_new_tier(self, new_plan_tier: int) -> None:+ def do_change_plan_to_new_tier(self, new_plan_tier: int) -> str:customer = self.get_customer()assert customer is not Nonecurrent_plan = get_current_plan_by_customer(customer)@@ -1897,13 +1900,14 @@ class BillingSession(ABC):new_plan = get_current_plan_by_customer(customer)assert new_plan is not None # for mypyself.invoice_plan(new_plan, plan_switch_time)- return+ return f"{self.billing_entity_display_name} upgraded to {new_plan.name}"# TODO: Implement downgrade that is a change from and to a paid plan# tier. This should keep the same billing cycle schedule and change# the plan when it's next invoiced vs immediately. Note this will need# new CustomerPlan.status value, e.g. SWITCH_PLAN_TIER_AT_END_OF_CYCLE.assert type_of_tier_change == PlanTierChangeType.DOWNGRADE # nocoverage+ return "" # nocoveragedef get_event_status(self, event_status_request: EventStatusRequest) -> Dict[str, Any]:customer = self.get_customer()@@ -2048,6 +2052,24 @@ class BillingSession(ABC):assert support_request["billing_modality"] in VALID_BILLING_MODALITY_VALUEScharge_automatically = support_request["billing_modality"] == "charge_automatically"success_message = self.update_billing_modality_of_current_plan(charge_automatically)+ elif support_type == SupportType.modify_plan:+ assert support_request["plan_modification"] is not None+ plan_modification = support_request["plan_modification"]+ if plan_modification == "downgrade_at_billing_cycle_end":+ self.downgrade_at_the_end_of_billing_cycle()+ success_message = f"{self.billing_entity_display_name} marked for downgrade at the end of billing cycle"+ elif plan_modification == "downgrade_now_without_additional_licenses":+ self.downgrade_now_without_creating_additional_invoices()+ success_message = f"{self.billing_entity_display_name} downgraded without creating additional invoices"+ elif plan_modification == "downgrade_now_void_open_invoices":+ self.downgrade_now_without_creating_additional_invoices()+ voided_invoices_count = self.void_all_open_invoices()+ success_message = f"{self.billing_entity_display_name} downgraded and voided {voided_invoices_count} open invoices"+ else:+ assert plan_modification == "upgrade_plan_tier"+ assert support_request["new_plan_tier"] is not None+ new_plan_tier = support_request["new_plan_tier"]+ success_message = self.do_change_plan_to_new_tier(new_plan_tier)return success_messagediff --git a/corporate/lib/support.py b/corporate/lib/support.pyindex 590b3111740..75abb35d56e 100644--- a/corporate/lib/support.py+++ b/corporate/lib/support.py@@ -5,8 +5,7 @@ from urllib.parse import urlencode, urljoin, urlunsplitfrom django.conf import settingsfrom django.urls import reverse-from corporate.lib.stripe import RealmBillingSession-from corporate.models import CustomerPlan, get_customer_by_realm+from corporate.models import get_customer_by_realmfrom zerver.models import Realm, get_realm@@ -24,8 +23,3 @@ def get_discount_for_realm(realm: Realm) -> Optional[Decimal]:if customer is not None:return customer.default_discountreturn None---def switch_realm_from_standard_to_plus_plan(realm: Realm) -> None:- billing_session = RealmBillingSession(realm=realm)- billing_session.do_change_plan_to_new_tier(new_plan_tier=CustomerPlan.TIER_CLOUD_PLUS)diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Customer.create.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Customer.create.1.jsonnew file mode 100644index 00000000000..e7f319bab08--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Customer.create.1.json@@ -0,0 +1,33 @@+{+ "address": null,+ "balance": 0,+ "created": 1701455052,+ "currency": null,+ "default_currency": null,+ "default_source": null,+ "delinquent": false,+ "description": "zulip (Zulip Dev)",+ "discount": null,+ "email": "[email protected]",+ "id": "cus_P6p16TWPgvBfKa",+ "invoice_prefix": "E0DA85A9",+ "invoice_settings": {+ "custom_fields": null,+ "default_payment_method": null,+ "footer": null,+ "rendering_options": null+ },+ "livemode": false,+ "metadata": {+ "realm_id": "2",+ "realm_str": "zulip"+ },+ "name": null,+ "next_invoice_sequence": 1,+ "object": "customer",+ "phone": null,+ "preferred_locales": [],+ "shipping": null,+ "tax_exempt": "none",+ "test_clock": null+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Event.list.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Event.list.1.jsonnew file mode 100644index 00000000000..ab067c98a6e--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Event.list.1.json@@ -0,0 +1,56 @@+{+ "data": [+ {+ "api_version": "2020-08-27",+ "created": 1701448145,+ "data": {+ "object": {+ "application": null,+ "automatic_payment_methods": null,+ "cancellation_reason": null,+ "client_secret": "seti_1OIZaPDEQaroqDjsbpgCPI6B_secret_P6nAgEWevQpumuMMctmE3tiaF5mfzca",+ "created": 1701448145,+ "customer": "cus_P6nAoyB9OpdnuO",+ "description": null,+ "flow_directions": null,+ "id": "seti_1OIZaPDEQaroqDjsbpgCPI6B",+ "last_setup_error": null,+ "latest_attempt": null,+ "livemode": false,+ "mandate": null,+ "metadata": {},+ "next_action": null,+ "object": "setup_intent",+ "on_behalf_of": null,+ "payment_method": null,+ "payment_method_configuration_details": null,+ "payment_method_options": {+ "card": {+ "mandate_options": null,+ "network": null,+ "request_three_d_secure": "automatic"+ }+ },+ "payment_method_types": [+ "card"+ ],+ "single_use_mandate": null,+ "status": "requires_payment_method",+ "usage": "off_session"+ }+ },+ "id": "evt_1OIZaPDEQaroqDjsbvtd1gXf",+ "livemode": false,+ "object": "event",+ "pending_webhooks": 0,+ "request": {+ "id": "req_SB8itx676JF4jd",+ "idempotency_key": "476d187f-0197-405b-8d91-4850118d974a"+ },+ "type": "setup_intent.created"+ }+ ],+ "has_more": true,+ "object": "list",+ "url": "/v1/events"+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.create.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.create.1.jsonnew file mode 100644index 00000000000..4d6a5820b03--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.create.1.json@@ -0,0 +1,158 @@+{+ "account_country": "US",+ "account_name": "Kandra Labs, Inc.",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "application_fee_amount": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "enabled": false,+ "status": null+ },+ "billing_reason": "manual",+ "charge": null,+ "collection_method": "send_invoice",+ "created": 1701455054,+ "currency": "usd",+ "custom_fields": null,+ "customer": "cus_P6p16TWPgvBfKa",+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": null,+ "discount": null,+ "discounts": [],+ "due_date": 1704047054,+ "effective_at": null,+ "ending_balance": null,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": null,+ "id": "in_1OIbNqDEQaroqDjsACG2UOX8",+ "invoice_pdf": null,+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "amount_excluding_tax": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "il_1OIbNpDEQaroqDjsR3NMyasf",+ "invoice_item": "ii_1OIbNpDEQaroqDjsbKt2EXl8",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "period": {+ "end": 1357095845,+ "start": 1325473445+ },+ "plan": null,+ "price": {+ "active": false,+ "billing_scheme": "per_unit",+ "created": 1698927319,+ "currency": "usd",+ "custom_unit_amount": null,+ "id": "price_1O7znvDEQaroqDjsLJjqNJy0",+ "livemode": false,+ "lookup_key": null,+ "metadata": {},+ "nickname": null,+ "object": "price",+ "product": "prod_OvY3rmiwsD4vFL",+ "recurring": null,+ "tax_behavior": "unspecified",+ "tiers_mode": null,+ "transform_quantity": null,+ "type": "one_time",+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "quantity": 123,+ "subscription": null,+ "tax_amounts": [],+ "tax_rates": [],+ "type": "invoiceitem",+ "unit_amount_excluding_tax": "8000"+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/in_1OIbNqDEQaroqDjsACG2UOX8/lines"+ },+ "livemode": false,+ "metadata": {},+ "next_payment_attempt": null,+ "number": null,+ "object": "invoice",+ "on_behalf_of": null,+ "paid": false,+ "paid_out_of_band": false,+ "payment_intent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1701455054,+ "period_start": 1701455054,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "quote": null,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "auto"+ }+ },+ "rendering_options": null,+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "draft",+ "status_transitions": {+ "finalized_at": null,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subscription": null,+ "subscription_details": {+ "metadata": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "tax": null,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_tax_amounts": [],+ "transfer_data": null,+ "webhooks_delivered_at": null+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.finalize_invoice.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.finalize_invoice.1.jsonnew file mode 100644index 00000000000..9d23749da50--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.finalize_invoice.1.json@@ -0,0 +1,158 @@+{+ "account_country": "US",+ "account_name": "Kandra Labs, Inc.",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "application_fee_amount": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "enabled": false,+ "status": null+ },+ "billing_reason": "manual",+ "charge": null,+ "collection_method": "send_invoice",+ "created": 1701455054,+ "currency": "usd",+ "custom_fields": null,+ "customer": "cus_P6p16TWPgvBfKa",+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": null,+ "discount": null,+ "discounts": [],+ "due_date": 1704047054,+ "effective_at": 1701455056,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODU30200gStRTLW6?s=ap",+ "id": "in_1OIbNqDEQaroqDjsACG2UOX8",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODU30200gStRTLW6/pdf?s=ap",+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "amount_excluding_tax": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "il_1OIbNpDEQaroqDjsR3NMyasf",+ "invoice_item": "ii_1OIbNpDEQaroqDjsbKt2EXl8",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "period": {+ "end": 1357095845,+ "start": 1325473445+ },+ "plan": null,+ "price": {+ "active": false,+ "billing_scheme": "per_unit",+ "created": 1698927319,+ "currency": "usd",+ "custom_unit_amount": null,+ "id": "price_1O7znvDEQaroqDjsLJjqNJy0",+ "livemode": false,+ "lookup_key": null,+ "metadata": {},+ "nickname": null,+ "object": "price",+ "product": "prod_OvY3rmiwsD4vFL",+ "recurring": null,+ "tax_behavior": "unspecified",+ "tiers_mode": null,+ "transform_quantity": null,+ "type": "one_time",+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "quantity": 123,+ "subscription": null,+ "tax_amounts": [],+ "tax_rates": [],+ "type": "invoiceitem",+ "unit_amount_excluding_tax": "8000"+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/in_1OIbNqDEQaroqDjsACG2UOX8/lines"+ },+ "livemode": false,+ "metadata": {},+ "next_payment_attempt": null,+ "number": "E0DA85A9-0001",+ "object": "invoice",+ "on_behalf_of": null,+ "paid": false,+ "paid_out_of_band": false,+ "payment_intent": "pi_3OIbNsDEQaroqDjs0Clfps2Q",+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1701455054,+ "period_start": 1701455054,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "quote": null,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ }+ },+ "rendering_options": null,+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "open",+ "status_transitions": {+ "finalized_at": 1701455056,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subscription": null,+ "subscription_details": {+ "metadata": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "tax": null,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_tax_amounts": [],+ "transfer_data": null,+ "webhooks_delivered_at": 1701455055+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.1.jsonnew file mode 100644index 00000000000..a2965c077d7--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.1.json@@ -0,0 +1,165 @@+{+ "data": [+ {+ "account_country": "US",+ "account_name": "Kandra Labs, Inc.",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "application_fee_amount": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "enabled": false,+ "status": null+ },+ "billing_reason": "manual",+ "charge": null,+ "collection_method": "send_invoice",+ "created": 1701455054,+ "currency": "usd",+ "custom_fields": null,+ "customer": "cus_P6p16TWPgvBfKa",+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": null,+ "discount": null,+ "discounts": [],+ "due_date": 1704047054,+ "effective_at": 1701455056,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODYx0200vVpneK0i?s=ap",+ "id": "in_1OIbNqDEQaroqDjsACG2UOX8",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODYx0200vVpneK0i/pdf?s=ap",+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "amount_excluding_tax": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "il_1OIbNpDEQaroqDjsR3NMyasf",+ "invoice_item": "ii_1OIbNpDEQaroqDjsbKt2EXl8",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "period": {+ "end": 1357095845,+ "start": 1325473445+ },+ "plan": null,+ "price": {+ "active": false,+ "billing_scheme": "per_unit",+ "created": 1698927319,+ "currency": "usd",+ "custom_unit_amount": null,+ "id": "price_1O7znvDEQaroqDjsLJjqNJy0",+ "livemode": false,+ "lookup_key": null,+ "metadata": {},+ "nickname": null,+ "object": "price",+ "product": "prod_OvY3rmiwsD4vFL",+ "recurring": null,+ "tax_behavior": "unspecified",+ "tiers_mode": null,+ "transform_quantity": null,+ "type": "one_time",+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "quantity": 123,+ "subscription": null,+ "tax_amounts": [],+ "tax_rates": [],+ "type": "invoiceitem",+ "unit_amount_excluding_tax": "8000"+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/in_1OIbNqDEQaroqDjsACG2UOX8/lines"+ },+ "livemode": false,+ "metadata": {},+ "next_payment_attempt": null,+ "number": "E0DA85A9-0001",+ "object": "invoice",+ "on_behalf_of": null,+ "paid": false,+ "paid_out_of_band": false,+ "payment_intent": "pi_3OIbNsDEQaroqDjs0Clfps2Q",+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1701455054,+ "period_start": 1701455054,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "quote": null,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ }+ },+ "rendering_options": null,+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "open",+ "status_transitions": {+ "finalized_at": 1701455056,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subscription": null,+ "subscription_details": {+ "metadata": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "tax": null,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_tax_amounts": [],+ "transfer_data": null,+ "webhooks_delivered_at": 1701455055+ }+ ],+ "has_more": false,+ "object": "list",+ "url": "/v1/invoices"+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.2.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.2.jsonnew file mode 100644index 00000000000..e39960ab725--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.list.2.json@@ -0,0 +1,6 @@+{+ "data": [],+ "has_more": false,+ "object": "list",+ "url": "/v1/invoices"+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.void_invoice.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.void_invoice.1.jsonnew file mode 100644index 00000000000..8e162a1fb6b--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--Invoice.void_invoice.1.json@@ -0,0 +1,158 @@+{+ "account_country": "US",+ "account_name": "Kandra Labs, Inc.",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "application_fee_amount": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": false,+ "automatic_tax": {+ "enabled": false,+ "status": null+ },+ "billing_reason": "manual",+ "charge": null,+ "collection_method": "send_invoice",+ "created": 1701455054,+ "currency": "usd",+ "custom_fields": null,+ "customer": "cus_P6p16TWPgvBfKa",+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": null,+ "discount": null,+ "discounts": [],+ "due_date": 1704047054,+ "effective_at": 1701455056,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODYx0200vVpneK0i?s=ap",+ "id": "in_1OIbNqDEQaroqDjsACG2UOX8",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_17vTkwDEQaroqDjs/test_YWNjdF8xN3ZUa3dERVFhcm9xRGpzLF9QNnAxcW1aNm9ZOHNVdjcwdFNWUFFhNkpseE9OcDBVLDkxOTk1ODYx0200vVpneK0i/pdf?s=ap",+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "amount_excluding_tax": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "il_1OIbNpDEQaroqDjsR3NMyasf",+ "invoice_item": "ii_1OIbNpDEQaroqDjsbKt2EXl8",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "period": {+ "end": 1357095845,+ "start": 1325473445+ },+ "plan": null,+ "price": {+ "active": false,+ "billing_scheme": "per_unit",+ "created": 1698927319,+ "currency": "usd",+ "custom_unit_amount": null,+ "id": "price_1O7znvDEQaroqDjsLJjqNJy0",+ "livemode": false,+ "lookup_key": null,+ "metadata": {},+ "nickname": null,+ "object": "price",+ "product": "prod_OvY3rmiwsD4vFL",+ "recurring": null,+ "tax_behavior": "unspecified",+ "tiers_mode": null,+ "transform_quantity": null,+ "type": "one_time",+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "quantity": 123,+ "subscription": null,+ "tax_amounts": [],+ "tax_rates": [],+ "type": "invoiceitem",+ "unit_amount_excluding_tax": "8000"+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/in_1OIbNqDEQaroqDjsACG2UOX8/lines"+ },+ "livemode": false,+ "metadata": {},+ "next_payment_attempt": null,+ "number": "E0DA85A9-0001",+ "object": "invoice",+ "on_behalf_of": null,+ "paid": false,+ "paid_out_of_band": false,+ "payment_intent": "pi_3OIbNsDEQaroqDjs0Clfps2Q",+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1701455054,+ "period_start": 1701455054,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "quote": null,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ }+ },+ "rendering_options": null,+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "void",+ "status_transitions": {+ "finalized_at": 1701455056,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": 1701455061+ },+ "subscription": null,+ "subscription_details": {+ "metadata": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "tax": null,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_tax_amounts": [],+ "transfer_data": null,+ "webhooks_delivered_at": 1701455055+}diff --git a/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--InvoiceItem.create.1.json b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--InvoiceItem.create.1.jsonnew file mode 100644index 00000000000..5f7fc85cb8f--- /dev/null+++ b/corporate/tests/stripe_fixtures/downgrade_realm_and_void_open_invoices--InvoiceItem.create.1.json@@ -0,0 +1,47 @@+{+ "amount": 984000,+ "currency": "usd",+ "customer": "cus_P6p16TWPgvBfKa",+ "date": 1701455053,+ "description": "Zulip Cloud Standard",+ "discountable": false,+ "discounts": [],+ "id": "ii_1OIbNpDEQaroqDjsbKt2EXl8",+ "invoice": null,+ "livemode": false,+ "metadata": {},+ "object": "invoiceitem",+ "period": {+ "end": 1357095845,+ "start": 1325473445+ },+ "plan": null,+ "price": {+ "active": false,+ "billing_scheme": "per_unit",+ "created": 1698927319,+ "currency": "usd",+ "custom_unit_amount": null,+ "id": "price_1O7znvDEQaroqDjsLJjqNJy0",+ "livemode": false,+ "lookup_key": null,+ "metadata": {},+ "nickname": null,+ "object": "price",+ "product": "prod_OvY3rmiwsD4vFL",+ "recurring": null,+ "tax_behavior": "unspecified",+ "tiers_mode": null,+ "transform_quantity": null,+ "type": "one_time",+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "quantity": 123,+ "subscription": null,+ "tax_rates": [],+ "test_clock": null,+ "unit_amount": 8000,+ "unit_amount_decimal": "8000"+}diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex b698987aea3..3934b6d9b47 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -50,6 +50,8 @@ from corporate.lib.stripe import (RealmBillingSession,RemoteServerBillingSession,StripeCardError,+ SupportType,+ SupportViewRequest,add_months,catch_stripe_errors,compute_plan_parameters,@@ -74,7 +76,7 @@ from corporate.lib.stripe import (update_license_ledger_for_manual_plan,update_license_ledger_if_needed,)-from corporate.lib.support import get_discount_for_realm, switch_realm_from_standard_to_plus_plan+from corporate.lib.support import get_discount_for_realmfrom corporate.models import (Customer,CustomerPlan,@@ -5298,8 +5300,43 @@ class TestSupportBillingHelpers(StripeTestCase):assert original_plan is not Noneself.assertEqual(original_plan.tier, CustomerPlan.TIER_CLOUD_STANDARD)- switch_realm_from_standard_to_plus_plan(user.realm)+ support_admin = self.example_user("iago")+ billing_session = RealmBillingSession(+ user=support_admin, realm=user.realm, support_session=True+ )+ support_request = SupportViewRequest(+ support_type=SupportType.modify_plan,+ plan_modification="upgrade_plan_tier",+ new_plan_tier=CustomerPlan.TIER_CLOUD_PLUS,+ )+ success_message = billing_session.process_support_view_request(support_request)+ self.assertEqual(success_message, "zulip upgraded to Zulip Plus")customer.refresh_from_db()new_plan = get_current_plan_by_customer(customer)assert new_plan is not Noneself.assertEqual(new_plan.tier, CustomerPlan.TIER_CLOUD_PLUS)++ @mock_stripe()+ def test_downgrade_realm_and_void_open_invoices(self, *mocks: Mock) -> None:+ user = self.example_user("hamlet")+ self.login_user(user)+ with time_machine.travel(self.now, tick=False):+ self.upgrade(invoice=True)+ customer = get_customer_by_realm(user.realm)+ assert customer is not None+ original_plan = get_current_plan_by_customer(customer)+ assert original_plan is not None+ self.assertEqual(original_plan.status, CustomerPlan.ACTIVE)++ support_admin = self.example_user("iago")+ billing_session = RealmBillingSession(+ user=support_admin, realm=user.realm, support_session=True+ )+ support_request = SupportViewRequest(+ support_type=SupportType.modify_plan,+ plan_modification="downgrade_now_void_open_invoices",+ )+ success_message = billing_session.process_support_view_request(support_request)+ self.assertEqual(success_message, "zulip downgraded and voided 1 open invoices")+ original_plan.refresh_from_db()+ self.assertEqual(original_plan.status, CustomerPlan.ENDED)diff --git a/templates/analytics/realm_details.html b/templates/analytics/realm_details.htmlindex 1114e7f4f8c..1b7481e5ec1 100644--- a/templates/analytics/realm_details.html+++ b/templates/analytics/realm_details.html@@ -145,7 +145,7 @@<option value="downgrade_at_billing_cycle_end">Downgrade at the end of current billing cycle</option><option value="downgrade_now_without_additional_licenses">Downgrade now without creating additional invoices</option><option value="downgrade_now_void_open_invoices">Downgrade now and void open invoices</option>- <option value="upgrade_to_plus">Upgrade to the Plus plan</option>+ <option value="upgrade_plan_tier">Upgrade to the Plus plan</option></select><button type="submit" class="btn btn-default support-submit-button">Modify</button></form>