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.

stripe: Add 'do_initial_upgrade' method to the 'BillingSession' class.

Prakhar Pratyush26c5149d3148
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 7d511bb534d..f0804378b4e 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -446,6 +446,12 @@ class UpgradeRequest:
licenses: Optional[int]
+@dataclass
+class InitialUpgradeRequest:
+ onboarding: bool
+ manual_license_management: bool
+
+
class AuditLogEventType(Enum):
STRIPE_CUSTOMER_CREATED = 1
STRIPE_CARD_CHANGED = 2
@@ -525,6 +531,14 @@ class BillingSession(ABC):
def approve_sponsorship(self) -> None:
pass
+ @abstractmethod
+ def is_sponsored_or_pending(self, customer: Optional[Customer]) -> bool:
+ pass
+
+ @abstractmethod
+ def update_context_initial_upgrade(self, context: Dict[str, Any]) -> None:
+ pass
+
@catch_stripe_errors
def create_stripe_customer(self) -> Customer:
stripe_customer_data = self.get_data_for_stripe_customer()
@@ -1091,6 +1105,55 @@ class BillingSession(ABC):
}
return context
+ def do_initial_upgrade(
+ self, initial_upgrade_request: InitialUpgradeRequest
+ ) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
+ customer = self.get_customer()
+
+ if self.is_sponsored_or_pending(customer):
+ return reverse("sponsorship_request"), None
+
+ onboarding = initial_upgrade_request.onboarding
+ 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 billing_page_url, None
+
+ percent_off = Decimal(0)
+ if customer is not None and customer.default_discount is not None:
+ percent_off = customer.default_discount
+
+ exempt_from_license_number_check = (
+ customer is not None and customer.exempt_from_license_number_check
+ )
+
+ seat_count = self.current_count_for_billed_licenses()
+ signed_seat_count, salt = sign_string(str(seat_count))
+ context: Dict[str, Any] = {
+ "seat_count": seat_count,
+ "signed_seat_count": signed_seat_count,
+ "salt": salt,
+ "min_invoiced_licenses": max(seat_count, MIN_INVOICED_LICENSES),
+ "default_invoice_days_until_due": DEFAULT_INVOICE_DAYS_UNTIL_DUE,
+ "exempt_from_license_number_check": exempt_from_license_number_check,
+ "plan": "Zulip Cloud Standard",
+ "free_trial_days": settings.FREE_TRIAL_DAYS,
+ "onboarding": onboarding,
+ "page_params": {
+ "seat_count": seat_count,
+ "annual_price": 8000,
+ "monthly_price": 800,
+ "percent_off": float(percent_off),
+ },
+ "manual_license_management": initial_upgrade_request.manual_license_management,
+ }
+ self.update_context_initial_upgrade(context)
+
+ return None, context
+
class RealmBillingSession(BillingSession):
def __init__(self, user: Optional[UserProfile] = None, realm: Optional[Realm] = None) -> None:
@@ -1293,6 +1356,28 @@ class RealmBillingSession(BillingSession):
)
internal_send_private_message(notification_bot, user, message)
+ @override
+ def is_sponsored_or_pending(self, customer: Optional[Customer]) -> bool:
+ if (
+ customer is not None and customer.sponsorship_pending
+ ) or self.realm.plan_type == self.realm.PLAN_TYPE_STANDARD_FREE:
+ return True
+ return False
+
+ @override
+ def update_context_initial_upgrade(self, context: Dict[str, Any]) -> None:
+ assert self.user is not None
+ data = {
+ "realm": self.realm,
+ "email": self.user.delivery_email,
+ "is_demo_organization": self.realm.demo_organization_scheduled_deletion_date
+ is not None,
+ }
+ context.update(data)
+ context["page_params"][
+ "demo_organization_scheduled_deletion_date"
+ ] = self.realm.demo_organization_scheduled_deletion_date
+
class RemoteRealmBillingSession(BillingSession): # nocoverage
def __init__(
@@ -1625,6 +1710,16 @@ class RemoteServerBillingSession(BillingSession): # nocoverage
plan.status = CustomerPlan.ENDED
plan.save(update_fields=["status"])
+ @override
+ def is_sponsored_or_pending(self, customer: Optional[Customer]) -> bool:
+ # TBD
+ return False
+
+ @override
+ def update_context_initial_upgrade(self, context: Dict[str, Any]) -> None:
+ # TBD
+ pass
+
def stripe_customer_has_credit_card_as_default_payment_method(
stripe_customer: stripe.Customer,
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 8cd9e4e2640..b77449aa354 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -1448,10 +1448,7 @@ class StripeTest(StripeTestCase):
new_seat_count = 23
# Change the seat count while the user is going through the upgrade flow
with patch("corporate.lib.stripe.get_latest_seat_count", return_value=new_seat_count):
- with patch(
- "corporate.views.upgrade.get_latest_seat_count", return_value=self.seat_count
- ):
- self.upgrade()
+ self.upgrade()
customer = Customer.objects.first()
assert customer is not None
stripe_customer_id: str = assert_is_not_none(customer.stripe_customer_id)
@@ -1714,8 +1711,7 @@ class StripeTest(StripeTestCase):
# Try again, with a valid card, after they added a few users
with patch("corporate.lib.stripe.get_latest_seat_count", return_value=23):
- with patch("corporate.views.upgrade.get_latest_seat_count", return_value=23):
- response = self.upgrade()
+ response = self.upgrade()
[second_payment_intent, _] = PaymentIntent.objects.all().order_by("-id")
assert second_payment_intent.stripe_payment_intent_id is not None
response_dict = self.assert_json_success(response)
diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py
index 38fcef7b0d1..197a6dcef5b 100644
--- a/corporate/views/upgrade.py
+++ b/corporate/views/upgrade.py
@@ -1,33 +1,23 @@
import logging
-from decimal import Decimal
-from typing import Any, Dict, Optional
+from typing import Optional
from django import forms
from django.conf import settings
from django.db import transaction
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render
-from django.urls import reverse
from corporate.lib.stripe import (
- DEFAULT_INVOICE_DAYS_UNTIL_DUE,
- MIN_INVOICED_LICENSES,
VALID_BILLING_MODALITY_VALUES,
VALID_BILLING_SCHEDULE_VALUES,
VALID_LICENSE_MANAGEMENT_VALUES,
BillingError,
+ InitialUpgradeRequest,
RealmBillingSession,
UpgradeRequest,
- get_latest_seat_count,
- sign_string,
)
from corporate.lib.support import get_support_url
-from corporate.models import (
- ZulipSponsorshipRequest,
- get_current_plan_by_customer,
- get_customer_by_realm,
-)
-from corporate.views.billing_page import billing_home
+from corporate.models import ZulipSponsorshipRequest
from zerver.actions.users import do_change_is_billing_admin
from zerver.decorator import require_organization_member, zulip_login_required
from zerver.lib.request import REQ, has_request_variables
@@ -101,50 +91,15 @@ def initial_upgrade(
if not settings.BILLING_ENABLED or user.is_guest:
return render(request, "404.html", status=404)
- 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"))
-
- 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)
-
- percent_off = Decimal(0)
- if customer is not None and customer.default_discount is not None:
- percent_off = customer.default_discount
-
- exempt_from_license_number_check = (
- customer is not None and customer.exempt_from_license_number_check
+ initial_upgrade_request = InitialUpgradeRequest(
+ onboarding=onboarding,
+ manual_license_management=manual_license_management,
)
+ billing_session = RealmBillingSession(user)
+ redirect_url, context = billing_session.do_initial_upgrade(initial_upgrade_request)
- seat_count = get_latest_seat_count(user.realm)
- signed_seat_count, salt = sign_string(str(seat_count))
- context: Dict[str, Any] = {
- "realm": user.realm,
- "email": user.delivery_email,
- "seat_count": seat_count,
- "signed_seat_count": signed_seat_count,
- "salt": salt,
- "min_invoiced_licenses": max(seat_count, MIN_INVOICED_LICENSES),
- "default_invoice_days_until_due": DEFAULT_INVOICE_DAYS_UNTIL_DUE,
- "exempt_from_license_number_check": exempt_from_license_number_check,
- "plan": "Zulip Cloud Standard",
- "free_trial_days": settings.FREE_TRIAL_DAYS,
- "onboarding": onboarding,
- "page_params": {
- "seat_count": seat_count,
- "annual_price": 8000,
- "monthly_price": 800,
- "percent_off": float(percent_off),
- "demo_organization_scheduled_deletion_date": user.realm.demo_organization_scheduled_deletion_date,
- },
- "is_demo_organization": user.realm.demo_organization_scheduled_deletion_date is not None,
- "manual_license_management": manual_license_management,
- }
+ if redirect_url:
+ return HttpResponseRedirect(redirect_url)
response = render(request, "corporate/upgrade.html", context=context)
return response