mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 16:14:02 +00:00
stripe: Only create flat discounts for renewal invoices.
If we only created invoice for additional licenses, we don't need to add discount to the invoice. Changes haven't been tested in this commit. It is just for easy verification of changes.
This commit is contained in:
@@ -3216,8 +3216,8 @@ class BillingSession(ABC):
|
|||||||
licenses_base = plan.invoiced_through.licenses
|
licenses_base = plan.invoiced_through.licenses
|
||||||
invoiced_through_id = plan.invoiced_through.id
|
invoiced_through_id = plan.invoiced_through.id
|
||||||
|
|
||||||
invoice_item_created = False
|
# Track if we added renewal invoice item which is possibly eligible for discount.
|
||||||
invoice_period: stripe.InvoiceItem.CreateParamsPeriod | None = None
|
renewal_invoice_period: stripe.InvoiceItem.CreateParamsPeriod | None = None
|
||||||
for ledger_entry in LicenseLedger.objects.filter(
|
for ledger_entry in LicenseLedger.objects.filter(
|
||||||
plan=plan, id__gt=invoiced_through_id, event_time__lte=event_time
|
plan=plan, id__gt=invoiced_through_id, event_time__lte=event_time
|
||||||
).order_by("id"):
|
).order_by("id"):
|
||||||
@@ -3273,12 +3273,16 @@ class BillingSession(ABC):
|
|||||||
plan.invoiced_through = ledger_entry
|
plan.invoiced_through = ledger_entry
|
||||||
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_STARTED
|
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_STARTED
|
||||||
plan.save(update_fields=["invoicing_status", "invoiced_through"])
|
plan.save(update_fields=["invoicing_status", "invoiced_through"])
|
||||||
invoice_period = {
|
invoice_period = stripe.InvoiceItem.CreateParamsPeriod(
|
||||||
"start": datetime_to_timestamp(ledger_entry.event_time),
|
start=datetime_to_timestamp(ledger_entry.event_time),
|
||||||
"end": datetime_to_timestamp(
|
end=datetime_to_timestamp(
|
||||||
get_plan_renewal_or_end_date(plan, ledger_entry.event_time)
|
get_plan_renewal_or_end_date(plan, ledger_entry.event_time)
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
if ledger_entry.is_renewal:
|
||||||
|
renewal_invoice_period = invoice_period
|
||||||
|
|
||||||
stripe.InvoiceItem.create(
|
stripe.InvoiceItem.create(
|
||||||
currency="usd",
|
currency="usd",
|
||||||
customer=plan.customer.stripe_customer_id,
|
customer=plan.customer.stripe_customer_id,
|
||||||
@@ -3288,14 +3292,12 @@ class BillingSession(ABC):
|
|||||||
idempotency_key=get_idempotency_key(ledger_entry),
|
idempotency_key=get_idempotency_key(ledger_entry),
|
||||||
**price_args,
|
**price_args,
|
||||||
)
|
)
|
||||||
invoice_item_created = True
|
|
||||||
plan.invoiced_through = ledger_entry
|
plan.invoiced_through = ledger_entry
|
||||||
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_DONE
|
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_DONE
|
||||||
plan.save(update_fields=["invoicing_status", "invoiced_through"])
|
plan.save(update_fields=["invoicing_status", "invoiced_through"])
|
||||||
licenses_base = ledger_entry.licenses
|
licenses_base = ledger_entry.licenses
|
||||||
|
|
||||||
if invoice_item_created:
|
if renewal_invoice_period is not None:
|
||||||
assert invoice_period is not None
|
|
||||||
flat_discount, flat_discounted_months = self.get_flat_discount_info(plan.customer)
|
flat_discount, flat_discounted_months = self.get_flat_discount_info(plan.customer)
|
||||||
if plan.fixed_price is None and flat_discounted_months > 0:
|
if plan.fixed_price is None and flat_discounted_months > 0:
|
||||||
num_months = (
|
num_months = (
|
||||||
@@ -3311,7 +3313,7 @@ class BillingSession(ABC):
|
|||||||
description=f"${cents_to_dollar_string(flat_discount)}/month new customer discount",
|
description=f"${cents_to_dollar_string(flat_discount)}/month new customer discount",
|
||||||
# Negative value to apply discount.
|
# Negative value to apply discount.
|
||||||
amount=(-1 * discount),
|
amount=(-1 * discount),
|
||||||
period=invoice_period,
|
period=renewal_invoice_period,
|
||||||
)
|
)
|
||||||
|
|
||||||
if plan.charge_automatically:
|
if plan.charge_automatically:
|
||||||
|
Reference in New Issue
Block a user