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: Move `setup_upgrade_checkout_session_and...` to BillingSession.

Prakhar Pratyush31f048d05458
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 902a7130952..70cd46248a8 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -552,6 +552,50 @@ class BillingSession(ABC):
extra_data={"charge_automatically": charge_automatically},
)
+ def setup_upgrade_checkout_session_and_payment_intent(
+ self,
+ plan_tier: int,
+ seat_count: int,
+ licenses: int,
+ license_management: str,
+ billing_schedule: int,
+ billing_modality: str,
+ onboarding: bool,
+ ) -> stripe.checkout.Session:
+ customer = self.update_or_create_stripe_customer()
+ assert customer is not None # for mypy
+ free_trial = is_free_trial_offer_enabled()
+ price_per_license = get_price_per_license(
+ plan_tier, billing_schedule, customer.default_discount
+ )
+ general_metadata = {
+ "billing_modality": billing_modality,
+ "billing_schedule": billing_schedule,
+ "licenses": licenses,
+ "license_management": license_management,
+ "price_per_license": price_per_license,
+ "seat_count": seat_count,
+ "type": "upgrade",
+ }
+ updated_metadata = self.update_data_for_checkout_session_and_payment_intent(
+ general_metadata
+ )
+ if free_trial:
+ if onboarding:
+ session_type = Session.FREE_TRIAL_UPGRADE_FROM_ONBOARDING_PAGE
+ else:
+ session_type = Session.FREE_TRIAL_UPGRADE_FROM_BILLING_PAGE
+ payment_intent = None
+ else:
+ session_type = Session.UPGRADE_FROM_BILLING_PAGE
+ payment_intent = self.create_stripe_payment_intent(
+ price_per_license, licenses, updated_metadata
+ )
+ stripe_session = self.create_stripe_checkout_session(
+ updated_metadata, session_type, payment_intent
+ )
+ return stripe_session
+
class RealmBillingSession(BillingSession):
def __init__(self, user: UserProfile, realm: Optional[Realm] = None) -> None:
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 55948fa061c..96e7811ca1c 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -1984,7 +1984,7 @@ class StripeTest(StripeTestCase):
stripe_session.id = "stripe_session_id"
stripe_session.url = "stripe_session_url"
with patch(
- "corporate.views.upgrade.setup_upgrade_checkout_session_and_payment_intent",
+ "corporate.lib.stripe.BillingSession.setup_upgrade_checkout_session_and_payment_intent",
return_value=stripe_session,
):
response = self.upgrade(
@@ -2039,7 +2039,7 @@ class StripeTest(StripeTestCase):
hamlet = self.example_user("hamlet")
self.login_user(hamlet)
with patch(
- "corporate.views.upgrade.setup_upgrade_checkout_session_and_payment_intent",
+ "corporate.lib.stripe.BillingSession.setup_upgrade_checkout_session_and_payment_intent",
side_effect=Exception,
), self.assertLogs("corporate.stripe", "WARNING") as m:
response = self.upgrade(talk_to_stripe=False)
diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py
index bce41851466..2ae6304dbac 100644
--- a/corporate/views/upgrade.py
+++ b/corporate/views/upgrade.py
@@ -2,7 +2,6 @@ import logging
from decimal import Decimal
from typing import Any, Dict, Optional
-import stripe
from django import forms
from django.conf import settings
from django.core import signing
@@ -18,7 +17,6 @@ from corporate.lib.stripe import (
RealmBillingSession,
ensure_customer_does_not_have_active_plan,
get_latest_seat_count,
- get_price_per_license,
is_free_trial_offer_enabled,
process_initial_upgrade,
sign_string,
@@ -28,7 +26,6 @@ from corporate.lib.stripe import (
from corporate.lib.support import get_support_url
from corporate.models import (
CustomerPlan,
- Session,
ZulipSponsorshipRequest,
get_current_plan_by_customer,
get_customer_by_realm,
@@ -78,53 +75,6 @@ def check_upgrade_parameters(
)
-def setup_upgrade_checkout_session_and_payment_intent(
- user: UserProfile,
- plan_tier: int,
- seat_count: int,
- licenses: int,
- license_management: str,
- billing_schedule: int,
- billing_modality: str,
- onboarding: bool,
-) -> stripe.checkout.Session:
- billing_session = RealmBillingSession(user)
- customer = billing_session.update_or_create_stripe_customer()
- assert customer is not None # for mypy
- free_trial = is_free_trial_offer_enabled()
- price_per_license = get_price_per_license(
- plan_tier, billing_schedule, customer.default_discount
- )
- general_metadata = {
- "billing_modality": billing_modality,
- "billing_schedule": billing_schedule,
- "licenses": licenses,
- "license_management": license_management,
- "price_per_license": price_per_license,
- "seat_count": seat_count,
- "type": "upgrade",
- }
- updated_metadata = billing_session.update_data_for_checkout_session_and_payment_intent(
- general_metadata
- )
- if free_trial:
- if onboarding:
- session_type = Session.FREE_TRIAL_UPGRADE_FROM_ONBOARDING_PAGE
- else:
- session_type = Session.FREE_TRIAL_UPGRADE_FROM_BILLING_PAGE
- payment_intent = None
- else:
- session_type = Session.UPGRADE_FROM_BILLING_PAGE
- payment_intent = billing_session.create_stripe_payment_intent(
- price_per_license, licenses, updated_metadata
- )
-
- stripe_session = billing_session.create_stripe_checkout_session(
- updated_metadata, session_type, payment_intent
- )
- return stripe_session
-
-
@require_organization_member
@has_request_variables
def upgrade(
@@ -170,15 +120,17 @@ def upgrade(
schedule
]
if charge_automatically:
- stripe_checkout_session = setup_upgrade_checkout_session_and_payment_intent(
- user,
- CustomerPlan.STANDARD,
- seat_count,
- licenses,
- license_management,
- billing_schedule,
- billing_modality,
- onboarding,
+ billing_session = RealmBillingSession(user)
+ stripe_checkout_session = (
+ billing_session.setup_upgrade_checkout_session_and_payment_intent(
+ CustomerPlan.STANDARD,
+ seat_count,
+ licenses,
+ license_management,
+ billing_schedule,
+ billing_modality,
+ onboarding,
+ )
)
return json_success(
request,