diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index ab0945e924..5217eb188a 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -2319,6 +2319,16 @@ class BillingSession(ABC): microsecond=0 ) last_renewal_ledger_entry.save(update_fields=["event_time"]) + # Since we are skipping over processing license ledger + # entries / RealmAuditLogs that are after `last_renewal_ledger_entry`, + # we need to update `licenses_at_next_renewal` to reflect the current value. + # It is okay to skip over them here since they were in free trial. + licenses_at_next_renewal = max( + self.get_billable_licenses_for_customer( + plan.customer, plan.tier, licenses_at_next_renewal + ), + licenses_at_next_renewal, + ) else: # We end the free trial since customer hasn't paid. plan.status = CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL