diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index e4fc350e67..76396f62a1 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -83,7 +83,7 @@ def next_month(billing_cycle_anchor: datetime, dt: datetime) -> datetime: 'billing_cycle_anchor: %s, dt: %s' % (billing_cycle_anchor, dt)) # TODO take downgrade into account -def next_renewal_date(plan: CustomerPlan, event_time: datetime) -> datetime: +def start_of_next_billing_cycle(plan: CustomerPlan, event_time: datetime) -> datetime: months_per_period = { CustomerPlan.ANNUAL: 12, CustomerPlan.MONTHLY: 1, @@ -220,7 +220,7 @@ def add_plan_renewal_to_license_ledger_if_needed(plan: CustomerPlan, event_time: last_ledger_entry = LicenseLedger.objects.filter(plan=plan).order_by('-id').first() last_renewal = LicenseLedger.objects.filter(plan=plan, is_renewal=True) \ .order_by('-id').first().event_time - plan_renewal_date = next_renewal_date(plan, last_renewal) + plan_renewal_date = start_of_next_billing_cycle(plan, last_renewal) if plan_renewal_date <= event_time: return LicenseLedger.objects.create( plan=plan, is_renewal=True, event_time=plan_renewal_date, @@ -403,7 +403,7 @@ def invoice_plan(plan: CustomerPlan, event_time: datetime) -> None: last_renewal = LicenseLedger.objects.filter( plan=plan, is_renewal=True, event_time__lte=ledger_entry.event_time) \ .order_by('-id').first().event_time - period_end = next_renewal_date(plan, ledger_entry.event_time) + period_end = start_of_next_billing_cycle(plan, ledger_entry.event_time) proration_fraction = (period_end - ledger_entry.event_time) / (period_end - last_renewal) price_args = {'unit_amount': int(plan.price_per_license * proration_fraction + .5), 'quantity': ledger_entry.licenses - licenses_base} @@ -423,7 +423,8 @@ def invoice_plan(plan: CustomerPlan, event_time: datetime) -> None: description=description, discountable=False, period = {'start': datetime_to_timestamp(ledger_entry.event_time), - 'end': datetime_to_timestamp(next_renewal_date(plan, ledger_entry.event_time))}, + 'end': datetime_to_timestamp( + start_of_next_billing_cycle(plan, ledger_entry.event_time))}, idempotency_key=idempotency_key, **price_args) invoice_item_created = True diff --git a/corporate/views.py b/corporate/views.py index 8add45a0d5..52ec67f4d9 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -21,7 +21,7 @@ from corporate.lib.stripe import STRIPE_PUBLISHABLE_KEY, \ process_initial_upgrade, sign_string, \ unsign_string, BillingError, process_downgrade, do_replace_payment_source, \ MIN_INVOICED_LICENSES, DEFAULT_INVOICE_DAYS_UNTIL_DUE, \ - next_renewal_date, renewal_amount, \ + start_of_next_billing_cycle, renewal_amount, \ add_plan_renewal_to_license_ledger_if_needed from corporate.models import Customer, CustomerPlan, \ get_active_plan @@ -172,7 +172,7 @@ def billing_home(request: HttpRequest) -> HttpResponse: licenses = last_ledger_entry.licenses licenses_used = get_seat_count(user.realm) # Should do this in javascript, using the user's timezone - renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(dt=next_renewal_date(plan, now)) + renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(dt=start_of_next_billing_cycle(plan, now)) renewal_cents = renewal_amount(plan, now) # TODO: this is the case where the plan doesn't automatically renew if renewal_cents is None: # nocoverage