stripe: Fix free trial pay by invoice customer billed twice.

This was a result of us moving `billing_cycle_anchor` ahead in time
of the `LicenseLedger` entry the customer paid for. Thus, this
confused our logic in thinking that customer hasn't paid for
the current billing cycle.
This commit is contained in:
Aman Agrawal
2025-07-10 19:34:04 +05:30
committed by Tim Abbott
parent ac0cc78327
commit c105bcc322
9 changed files with 82 additions and 37 deletions

View File

@@ -2301,6 +2301,24 @@ class BillingSession(ABC):
# This will create invoice for any additional licenses that user has at the time of
# switching from free trial to paid plan since they already paid for the plan's this billing cycle.
is_renewal = False
# Since we need to move the `billing_cycle_anchor` forward below, we also
# need to update the `event_time` of the last renewal ledger entry to avoid
# our logic from thinking that licenses for the current billing cycle hasn't
# been paid for.
last_renewal_ledger_entry = (
LicenseLedger.objects.filter(
plan=plan,
is_renewal=True,
)
.order_by("-id")
.first()
)
assert last_renewal_ledger_entry is not None
last_renewal_ledger_entry.event_time = next_billing_cycle.replace(
microsecond=0
)
last_renewal_ledger_entry.save(update_fields=["event_time"])
else:
# We end the free trial since customer hasn't paid.
plan.status = CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL