Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
billing: Show sponsorship status on /sponsorship page.
Aman Agrawala3093fad9713
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex b2db3224e6f..2d5b6c6ff7d 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -22,7 +22,6 @@ from typing import (Tuple,TypeVar,)-from unittest import mockfrom unittest.mock import Mock, patchimport orjson@@ -438,6 +437,11 @@ class StripeTestCase(ZulipTestCase):self.next_month = datetime(2012, 2, 2, 3, 4, 5, tzinfo=timezone.utc)self.next_year = datetime(2013, 1, 2, 3, 4, 5, tzinfo=timezone.utc)+ # Make hamlet billing admin for testing.+ hamlet = self.example_user("hamlet")+ hamlet.is_billing_admin = True+ hamlet.save(update_fields=["is_billing_admin"])+def get_signed_seat_count_from_response(self, response: "TestHttpResponse") -> Optional[str]:match = re.search(r"name=\"signed_seat_count\" value=\"(.+)\"", response.content.decode())return match.group(1) if match else None@@ -2288,22 +2292,23 @@ class StripeTest(StripeTestCase):response = self.client_get("/upgrade/")self.assertEqual(response.status_code, 302)- self.assertEqual(response["Location"], "/billing/")+ self.assertEqual(response["Location"], "/sponsorship/")response = self.client_get("/billing/")- self.assert_in_success_response(- ["Your organization has requested sponsored or discounted hosting."], response- )+ self.assertEqual(response.status_code, 302)+ self.assertEqual(response["Location"], "/sponsorship/")++ response = self.client_get("/sponsorship/")self.assert_in_success_response([- 'Please <a href="mailto:[email protected]">contact Zulip support</a> if you have any questions or concerns.'+ "Your organization has requested sponsorship for a free or discounted Zulip Cloud Standard plan."],response,)- # Ensure that the other "contact support" footer is not displayed, since that would be- # duplicate.- self.assert_not_in_success_response(- ['<a href="mailto:[email protected]">Contact Zulip support</a> for billing history.'],+ self.assert_in_success_response(+ [+ 'Please feel free to <a href="mailto:[email protected]">contact Zulip support</a> with any questions or updates to your request.'+ ],response,)@@ -2317,13 +2322,9 @@ class StripeTest(StripeTestCase):user.realm.plan_type = Realm.PLAN_TYPE_STANDARD_FREEuser.realm.save()self.login_user(self.example_user("hamlet"))- response = self.client_get("/billing/")+ response = self.client_get("/sponsorship/")self.assert_in_success_response(- ["Your organization is fully sponsored and is on the <b>Zulip Cloud Standard</b>"],- response,- )- self.assert_in_success_response(- ['<a href="mailto:[email protected]">Contact Zulip support</a> for billing history.'],+ ["Zulip is sponsoring free Zulip Cloud Standard hosting for your organization."],response,)@@ -2331,13 +2332,22 @@ class StripeTest(StripeTestCase):user = self.example_user("iago")self.login_user(user)response = self.client_get("/billing/")- self.assertEqual(response.status_code, 302)- self.assertEqual("/upgrade/", response["Location"])+ not_admin_message = (+ "You must be an organization owner or a billing administrator to view this page."+ )+ self.assert_in_success_response([not_admin_message], response)user.realm.plan_type = Realm.PLAN_TYPE_STANDARD_FREEuser.realm.save()response = self.client_get("/billing/")- self.assertEqual(response.status_code, 200)+ self.assert_in_success_response([not_admin_message], response)++ # Billing page redirects to sponsorship page for standard free admins.+ user = self.example_user("hamlet")+ self.login_user(user)+ response = self.client_get("/billing/")+ self.assertEqual(response.status_code, 302)+ self.assertEqual("/sponsorship/", response["Location"])user.realm.plan_type = Realm.PLAN_TYPE_LIMITEDuser.realm.save()@@ -2347,7 +2357,7 @@ class StripeTest(StripeTestCase):self.assertEqual("/upgrade/", response["Location"])def test_upgrade_page_for_demo_organizations(self) -> None:- user = self.example_user("iago")+ user = self.example_user("hamlet")user.realm.demo_organization_scheduled_deletion_date = timezone_now() + timedelta(days=30)user.realm.save()self.login_user(user)@@ -2366,7 +2376,7 @@ class StripeTest(StripeTestCase):user.realm.save()response = self.client_get("/upgrade/")self.assertEqual(response.status_code, 302)- self.assertEqual(response["Location"], "/billing/")+ self.assertEqual(response["Location"], "/sponsorship/")user.realm.plan_type = Realm.PLAN_TYPE_LIMITEDuser.realm.save()@@ -3808,47 +3818,6 @@ class StripeTest(StripeTestCase):(invoice,) = stripe.Invoice.list(customer=stripe_customer_id)self.assertEqual(invoice.amount_due, 7200)- def test_request_sponsorship_available_on_upgrade_and_billing_pages(self) -> None:- """- Verifies that the Request sponsorship form is available on both the upgrade- page and the billing page for already subscribed customers.- """- realm = get_realm("zulip")- self.login("desdemona")- result = self.client_get("/upgrade/")- self.assert_in_success_response(["Request sponsorship"], 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.ANNUAL,- tier=CustomerPlan.STANDARD,- price_per_license=1000,- )- LicenseLedger.objects.create(- plan=plan,- is_renewal=True,- event_time=timezone_now(),- licenses=9,- licenses_at_next_renewal=9,- )-- mock_stripe_customer = mock.MagicMock()- mock_stripe_customer.email = "[email protected]"-- with mock.patch(- "corporate.views.billing_page.stripe_get_customer", return_value=mock_stripe_customer- ):- result = self.client_get("/billing/")- # Sanity assert to make sure we're testing the subscribed billing page.- self.assert_in_success_response(- ["Your current plan is <strong>Zulip Cloud Standard</strong>."], result- )-- self.assert_in_success_response(["Request sponsorship"], result)-@mock_stripe()def test_customer_has_credit_card_as_default_payment_method(self, *mocks: Mock) -> None:iago = self.example_user("iago")@@ -4046,10 +4015,6 @@ class RequiresBillingAccessTest(StripeTestCase):@overridedef setUp(self, *mocks: Mock) -> None:super().setUp()- hamlet = self.example_user("hamlet")- hamlet.is_billing_admin = True- hamlet.save(update_fields=["is_billing_admin"])-desdemona = self.example_user("desdemona")desdemona.role = UserProfile.ROLE_REALM_OWNERdesdemona.save(update_fields=["role"])diff --git a/corporate/views/billing_page.py b/corporate/views/billing_page.pyindex 1c1c71f165c..2c3b604d97e 100644--- a/corporate/views/billing_page.py+++ b/corporate/views/billing_page.py@@ -81,6 +81,14 @@ def sponsorship_request(request: HttpRequest) -> HttpResponse:user = request.userassert user.is_authenticatedcontext: Dict[str, Any] = {}++ customer = get_customer_by_realm(user.realm)+ if customer is not None and customer.sponsorship_pending:+ context["is_sponsorship_pending"] = True++ if user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:+ context["is_sponsored"] = True+add_sponsorship_info_to_context(context, user)return render(request, "corporate/sponsorship.html", context=context)@@ -93,33 +101,34 @@ def billing_home(user = request.userassert user.is_authenticated- customer = get_customer_by_realm(user.realm)context: Dict[str, Any] = {"admin_access": user.has_billing_access,"has_active_plan": False,}+ if not user.has_billing_access:+ return render(request, "corporate/billing.html", context=context)+if user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:context["is_sponsored"] = True- return render(request, "corporate/billing.html", context=context)+ return HttpResponseRedirect(reverse("sponsorship_request"))++ customer = get_customer_by_realm(user.realm)+ if (+ customer is not None and customer.sponsorship_pending+ ) or user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:+ return HttpResponseRedirect(reverse("sponsorship_request"))if customer is None:from corporate.views.upgrade import initial_upgradereturn HttpResponseRedirect(reverse(initial_upgrade))- if customer.sponsorship_pending:- context["sponsorship_pending"] = True- return render(request, "corporate/billing.html", context=context)-if not CustomerPlan.objects.filter(customer=customer).exists():from corporate.views.upgrade import initial_upgradereturn HttpResponseRedirect(reverse(initial_upgrade))- if not user.has_billing_access:- return render(request, "corporate/billing.html", context=context)-plan = get_current_plan_by_customer(customer)if plan is not None:now = timezone_now()@@ -172,7 +181,6 @@ def billing_home(CustomerPlan=CustomerPlan,onboarding=onboarding,)- add_sponsorship_info_to_context(context, user)return render(request, "corporate/billing.html", context=context)diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.pyindex 23157736720..bce41851466 100644--- a/corporate/views/upgrade.py+++ b/corporate/views/upgrade.py@@ -20,7 +20,6 @@ from corporate.lib.stripe import (get_latest_seat_count,get_price_per_license,is_free_trial_offer_enabled,- is_sponsored_realm,process_initial_upgrade,sign_string,unsign_string,@@ -34,7 +33,7 @@ from corporate.models import (get_current_plan_by_customer,get_customer_by_realm,)-from corporate.views.billing_page import add_sponsorship_info_to_context, billing_home+from corporate.views.billing_page import billing_homefrom zerver.actions.users import do_make_user_billing_adminfrom zerver.decorator import require_organization_member, zulip_login_requiredfrom zerver.lib.request import REQ, has_request_variables@@ -231,19 +230,18 @@ def initial_upgrade(if not settings.BILLING_ENABLED or user.is_guest:return render(request, "404.html", status=404)- billing_page_url = reverse(billing_home)-customer = get_customer_by_realm(user.realm)- if customer is not None and (- get_current_plan_by_customer(customer) is not None or customer.sponsorship_pending- ):+ if (+ customer is not None and customer.sponsorship_pending+ ) or user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:+ return HttpResponseRedirect(reverse("sponsorship_request"))++ billing_page_url = reverse(billing_home)+ if customer is not None and (get_current_plan_by_customer(customer) is not None or onboarding):if onboarding:billing_page_url = f"{billing_page_url}?onboarding=true"return HttpResponseRedirect(billing_page_url)- if is_sponsored_realm(user.realm):- return HttpResponseRedirect(billing_page_url)-percent_off = Decimal(0)if customer is not None and customer.default_discount is not None:percent_off = customer.default_discount@@ -275,7 +273,6 @@ def initial_upgrade(},"is_demo_organization": user.realm.demo_organization_scheduled_deletion_date is not None,}- add_sponsorship_info_to_context(context, user)response = render(request, "corporate/upgrade.html", context=context)return responsediff --git a/templates/corporate/billing.html b/templates/corporate/billing.htmlindex 7fbd93266b2..ac29e09db5a 100644--- a/templates/corporate/billing.html+++ b/templates/corporate/billing.html@@ -188,18 +188,8 @@</p></div>{% elif admin_access and not has_active_plan %}+ <!-- For sponsored or sponsorship pending organizations we already redirect to /sponsorship page. --><div class="tab-content">- {% if sponsorship_pending %}- <h3>Your organization has requested sponsored or discounted hosting.</h3>- <br />- Please <a href="mailto:[email protected]">contact Zulip support</a> if you have any questions or concerns.- {% elif is_sponsored %}- <center>- <p>- <h3>💚 Your organization is fully sponsored and is on the <b>Zulip Cloud Standard</b> plan.</h3>- </p>- </center>- {% else %}<center><p><h2>Your organization is on the <b>Zulip Free</b> plan.</h2>@@ -211,16 +201,12 @@</a></p></center>- {% endif %}</div>- {% if not sponsorship_pending %}- {# In the other case, we're displaying a different message about contacting support. #}<div class="support-link"><p><a href="mailto:[email protected]">Contact Zulip support</a> for billing history.</p></div>- {% endif %}{% else %}<p>You must be an organization owner or a billing administrator to view this page.diff --git a/templates/corporate/sponsorship.html b/templates/corporate/sponsorship.htmlindex f931668aa12..8829bc9f82c 100644--- a/templates/corporate/sponsorship.html+++ b/templates/corporate/sponsorship.html@@ -4,6 +4,30 @@{% set PAGE_TITLE = "💚 Request sponsorship" %}{% block portico_content %}++{% if is_sponsored %}++<div class="flex full-page thanks-page">+ <div class="center-block new-style">+ <h1>Zulip Cloud billing for {{realm_name}}</h1>+ <p> Zulip is sponsoring free Zulip Cloud Standard hosting for your organization. 🎉 </p>+ </div>+</div>++{% elif is_sponsorship_pending %}++<div class="flex full-page thanks-page">+ <div class="center-block new-style">+ <h1>Sponsorship request pending for {{realm_name}}</h1>+ <p> Your organization has requested sponsorship for a free or discounted Zulip Cloud Standard plan.</p>+ <p>+ Please feel free to <a href="mailto:[email protected]">contact Zulip support</a> with any questions or updates to your request.+ </p>+ </div>+</div>++{% else %}+<div class="register-account flex full-page sponsorship-page"><div class="center-block new-style"><div class="pitch">@@ -55,5 +79,6 @@</div></div></div>+{% endif %}{% endblock %}