stripe: Fix legacy server upgrade to business plan.

I had pushed a similar change in #28017 but seems to have been lost.
This commit is contained in:
Aman Agrawal 2023-12-05 06:39:21 +00:00 committed by Tim Abbott
parent 8165f718d6
commit 044cb820f8
2 changed files with 16 additions and 7 deletions

View File

@ -408,6 +408,14 @@ class UpgradeWithExistingPlanError(BillingError):
)
class InvalidPlanUpgradeError(BillingError): # nocoverage
def __init__(self, message: str) -> None:
super().__init__(
"invalid plan upgrade",
message,
)
class InvalidBillingScheduleError(Exception):
def __init__(self, billing_schedule: int) -> None:
self.message = f"Unknown billing_schedule: {billing_schedule}"
@ -1067,7 +1075,7 @@ class BillingSession(ABC):
assert plan is not None
type_of_plan_change = self.get_type_of_plan_tier_change(plan.tier, new_plan_tier)
if type_of_plan_change != PlanTierChangeType.UPGRADE:
raise BillingError(
raise InvalidPlanUpgradeError(
f"Cannot upgrade from {plan.name} to {CustomerPlan.name_from_tier(new_plan_tier)}"
)

View File

@ -6,11 +6,11 @@ from django.conf import settings
from corporate.lib.stripe import (
BillingError,
InvalidPlanUpgradeError,
RealmBillingSession,
RemoteRealmBillingSession,
RemoteServerBillingSession,
UpgradeWithExistingPlanError,
ensure_customer_does_not_have_active_plan,
)
from corporate.models import Customer, CustomerPlan, Event, PaymentIntent, Session
from zerver.models import get_active_user_profile_by_id_in_realm
@ -123,10 +123,13 @@ def handle_payment_intent_succeeded_event(
description=description,
discountable=False,
)
billing_session = get_billing_session_for_stripe_webhook(
payment_intent.customer, metadata.get("user_id")
)
plan_tier = int(metadata["plan_tier"])
try:
ensure_customer_does_not_have_active_plan(payment_intent.customer)
except UpgradeWithExistingPlanError as e:
billing_session.ensure_current_plan_is_upgradable(payment_intent.customer, plan_tier)
except (UpgradeWithExistingPlanError, InvalidPlanUpgradeError) as e:
stripe_invoice = stripe.Invoice.create(
auto_advance=True,
collection_method="charge_automatically",
@ -138,9 +141,6 @@ def handle_payment_intent_succeeded_event(
stripe.Invoice.finalize_invoice(stripe_invoice)
raise e
billing_session = get_billing_session_for_stripe_webhook(
payment_intent.customer, metadata.get("user_id")
)
billing_session.process_initial_upgrade(
plan_tier,
int(metadata["licenses"]),
@ -148,4 +148,5 @@ def handle_payment_intent_succeeded_event(
int(metadata["billing_schedule"]),
True,
False,
billing_session.get_remote_server_legacy_plan(payment_intent.customer),
)