mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	stripe: Fix customer charged for upgrade during free trial.
I accidentally free trials for both cloud and self hosted enabled while testing, hence didn't catch it. This mostly involves fixing `is_free_trial_offer_enabled` to return the correct value and providing it the correct input.
This commit is contained in:
		@@ -1138,6 +1138,7 @@ class BillingSession(ABC):
 | 
			
		||||
        remote_server_legacy_plan: Optional[CustomerPlan] = None,
 | 
			
		||||
        should_schedule_upgrade_for_legacy_remote_server: bool = False,
 | 
			
		||||
    ) -> None:
 | 
			
		||||
        is_self_hosted_billing = not isinstance(self, RealmBillingSession)
 | 
			
		||||
        customer = self.update_or_create_stripe_customer()
 | 
			
		||||
        assert customer.stripe_customer_id is not None  # for mypy
 | 
			
		||||
        self.ensure_current_plan_is_upgradable(customer, plan_tier)
 | 
			
		||||
@@ -1158,6 +1159,7 @@ class BillingSession(ABC):
 | 
			
		||||
            customer.default_discount,
 | 
			
		||||
            free_trial,
 | 
			
		||||
            billing_cycle_anchor,
 | 
			
		||||
            is_self_hosted_billing,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        # TODO: The correctness of this relies on user creation, deactivation, etc being
 | 
			
		||||
@@ -1323,7 +1325,8 @@ class BillingSession(ABC):
 | 
			
		||||
            "monthly": CustomerPlan.BILLING_SCHEDULE_MONTHLY,
 | 
			
		||||
        }[schedule]
 | 
			
		||||
        data: Dict[str, Any] = {}
 | 
			
		||||
        free_trial = is_free_trial_offer_enabled()
 | 
			
		||||
        is_self_hosted_billing = not isinstance(self, RealmBillingSession)
 | 
			
		||||
        free_trial = is_free_trial_offer_enabled(is_self_hosted_billing)
 | 
			
		||||
        remote_server_legacy_plan = self.get_remote_server_legacy_plan(customer)
 | 
			
		||||
        should_schedule_upgrade_for_legacy_remote_server = (
 | 
			
		||||
            remote_server_legacy_plan is not None
 | 
			
		||||
@@ -1341,7 +1344,7 @@ class BillingSession(ABC):
 | 
			
		||||
                automanage_licenses,
 | 
			
		||||
                billing_schedule,
 | 
			
		||||
                charge_automatically,
 | 
			
		||||
                is_free_trial_offer_enabled(),
 | 
			
		||||
                free_trial,
 | 
			
		||||
                remote_server_legacy_plan,
 | 
			
		||||
                should_schedule_upgrade_for_legacy_remote_server,
 | 
			
		||||
            )
 | 
			
		||||
@@ -3641,8 +3644,8 @@ def get_free_trial_days(is_self_hosted_billing: bool = False) -> Optional[int]:
 | 
			
		||||
    return settings.CLOUD_FREE_TRIAL_DAYS
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def is_free_trial_offer_enabled() -> bool:
 | 
			
		||||
    return settings.CLOUD_FREE_TRIAL_DAYS not in (None, 0)
 | 
			
		||||
def is_free_trial_offer_enabled(is_self_hosted_billing: bool) -> bool:
 | 
			
		||||
    return get_free_trial_days(is_self_hosted_billing) not in (None, 0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def ensure_customer_does_not_have_active_plan(customer: Customer) -> None:
 | 
			
		||||
 
 | 
			
		||||
@@ -599,7 +599,7 @@ class StripeTestCase(ZulipTestCase):
 | 
			
		||||
            # Return early if the upgrade request failed.
 | 
			
		||||
            return upgrade_json_response
 | 
			
		||||
 | 
			
		||||
        if invoice or not talk_to_stripe or is_free_trial_offer_enabled():
 | 
			
		||||
        if invoice or not talk_to_stripe or is_free_trial_offer_enabled(False):
 | 
			
		||||
            # Upgrade already happened for free trial or invoice realms.
 | 
			
		||||
            return upgrade_json_response
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -392,7 +392,7 @@ def login_or_register_remote_user(request: HttpRequest, result: ExternalAuthResu
 | 
			
		||||
    if is_realm_creation is not None and settings.BILLING_ENABLED:
 | 
			
		||||
        from corporate.lib.stripe import is_free_trial_offer_enabled
 | 
			
		||||
 | 
			
		||||
        if is_free_trial_offer_enabled():
 | 
			
		||||
        if is_free_trial_offer_enabled(False):
 | 
			
		||||
            redirect_to = reverse("upgrade_page")
 | 
			
		||||
 | 
			
		||||
    redirect_to = get_safe_redirect_to(redirect_to, user_profile.realm.uri)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user