From 03c71dad17c555ffa38947b32b861cdf69c98455 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Thu, 24 Jan 2019 17:14:07 -0800 Subject: [PATCH] billing: Fix misuses of CustomerPlan.discount. --- corporate/lib/stripe.py | 22 +++++++--------------- corporate/models.py | 3 ++- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 5d6b277534..f913930382 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -94,17 +94,12 @@ def next_renewal_date(plan: CustomerPlan) -> datetime: def renewal_amount(plan: CustomerPlan) -> Optional[int]: # nocoverage: TODO if plan.fixed_price is not None: - basis = plan.fixed_price - else: - last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, timezone_now()) - if last_ledger_entry.licenses_at_next_renewal is None: - return None - assert(plan.price_per_license is not None) # for mypy - basis = plan.price_per_license * last_ledger_entry.licenses_at_next_renewal - if plan.discount is None: - return basis - # TODO: figure out right thing to do with Decimal - return int(float(basis * (100 - plan.discount) / 100) + .00001) + return plan.fixed_price + last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, timezone_now()) + if last_ledger_entry.licenses_at_next_renewal is None: + return None + assert(plan.price_per_license is not None) # for mypy + return plan.price_per_license * last_ledger_entry.licenses_at_next_renewal class BillingError(Exception): # error messages @@ -313,13 +308,10 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_license realm=realm, acting_user=user, event_time=billing_cycle_anchor, event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED, extra_data=ujson.dumps(plan_params)) - description = 'Zulip Standard' - if customer.default_discount is not None: # nocoverage: TODO - description += ' (%s%% off)' % (customer.default_discount,) stripe.InvoiceItem.create( currency='usd', customer=customer.stripe_customer_id, - description=description, + description='Zulip Standard', discountable=False, period = {'start': datetime_to_timestamp(billing_cycle_anchor), 'end': datetime_to_timestamp(period_end)}, diff --git a/corporate/models.py b/corporate/models.py index 169006659a..1447bff9a3 100644 --- a/corporate/models.py +++ b/corporate/models.py @@ -12,6 +12,7 @@ class Customer(models.Model): stripe_customer_id = models.CharField(max_length=255, unique=True) # type: str # Deprecated .. delete once everyone is migrated to new billing system has_billing_relationship = models.BooleanField(default=False) # type: bool + # A percentage, like 85. default_discount = models.DecimalField(decimal_places=4, max_digits=7, null=True) # type: Optional[Decimal] def __str__(self) -> str: @@ -30,7 +31,7 @@ class CustomerPlan(models.Model): price_per_license = models.IntegerField(null=True) # type: Optional[int] fixed_price = models.IntegerField(null=True) # type: Optional[int] - # A percentage, like 85 + # Discount that was applied. For display purposes only. discount = models.DecimalField(decimal_places=4, max_digits=6, null=True) # type: Optional[Decimal] billing_cycle_anchor = models.DateTimeField() # type: datetime.datetime