From cac4adac5f81b006fb4a7a39cc30a247c0e9f123 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Fri, 27 Sep 2024 20:12:33 +0200 Subject: [PATCH] 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. --- corporate/lib/stripe.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 661ff1fe61..a6d585a406 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -3722,25 +3722,22 @@ class BillingSession(ABC): end_date: datetime, ) -> None: 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( - stripe_customer_id=None, defaults={"stripe_customer_id": None} - ) + customer = self.update_or_create_customer() - # Servers on legacy plan which are scheduled to be upgraded have 2 plans. - # This plan will be 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 - # customer will move to the end of this plan. + # Customers on a legacy plan that is scheduled to be upgraded have 2 plans. + # 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 is used to track the new plan + # the customer will move to the end of this plan. legacy_plan_anchor = renewal_date legacy_plan_params = { "billing_cycle_anchor": legacy_plan_anchor, "status": CustomerPlan.ACTIVE, "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, "next_invoice_date": end_date, # 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. "price_per_license": 0, "billing_schedule": CustomerPlan.BILLING_SCHEDULE_ANNUAL,