billing: Keep stripe_customer_id when migrating to legacy plan.

The removal of the stripe_customer_id when creating a legacy plan
actually disconnects any existing information in Stripe about a
customer, who may have previously had a paid plan, so we don't
want that removal to happen. Instead we get or create the customer
associated with the billing session entity.

Also, updates some of the code comments in the function for legacy
plan migrations to be clearer.
This commit is contained in:
Lauryn Menard 2024-09-27 20:12:33 +02:00 committed by Tim Abbott
parent 6180725579
commit cac4adac5f
1 changed files with 7 additions and 10 deletions

View File

@ -3722,25 +3722,22 @@ class BillingSession(ABC):
end_date: datetime, end_date: datetime,
) -> None: ) -> None:
assert not isinstance(self, RealmBillingSession) assert not isinstance(self, RealmBillingSession)
# Set stripe_customer_id to None to avoid customer being charged without a payment method. customer = self.update_or_create_customer()
customer = self.update_or_create_customer(
stripe_customer_id=None, defaults={"stripe_customer_id": None}
)
# Servers on legacy plan which are scheduled to be upgraded have 2 plans. # Customers on a legacy plan that is scheduled to be upgraded have 2 plans.
# This plan will be used to track the current status of SWITCH_PLAN_TIER_AT_PLAN_END # This plan is used to track the current status of SWITCH_PLAN_TIER_AT_PLAN_END
# and will not charge the customer. The other plan will be used to track the new plan # and will not charge the customer. The other plan is used to track the new plan
# customer will move to the end of this plan. # the customer will move to the end of this plan.
legacy_plan_anchor = renewal_date legacy_plan_anchor = renewal_date
legacy_plan_params = { legacy_plan_params = {
"billing_cycle_anchor": legacy_plan_anchor, "billing_cycle_anchor": legacy_plan_anchor,
"status": CustomerPlan.ACTIVE, "status": CustomerPlan.ACTIVE,
"tier": CustomerPlan.TIER_SELF_HOSTED_LEGACY, "tier": CustomerPlan.TIER_SELF_HOSTED_LEGACY,
# End when the new plan starts. # end_date and next_invoice_date should always be the same for these plans.
"end_date": end_date, "end_date": end_date,
"next_invoice_date": end_date, "next_invoice_date": end_date,
# The primary mechanism for preventing charges under this # The primary mechanism for preventing charges under this
# plan is setting 'invoiced_through' to last ledger_entry below, # plan is setting 'invoiced_through' to the ledger_entry below,
# but setting a 0 price is useful defense in depth here. # but setting a 0 price is useful defense in depth here.
"price_per_license": 0, "price_per_license": 0,
"billing_schedule": CustomerPlan.BILLING_SCHEDULE_ANNUAL, "billing_schedule": CustomerPlan.BILLING_SCHEDULE_ANNUAL,