2018-12-15 09:33:25 +01:00
|
|
|
from datetime import datetime
|
2018-12-12 19:41:03 +01:00
|
|
|
from decimal import Decimal
|
2018-01-30 21:03:59 +01:00
|
|
|
from functools import wraps
|
2018-01-30 20:49:25 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2018-12-15 09:33:25 +01:00
|
|
|
from typing import Any, Callable, Dict, Optional, TypeVar, Tuple, cast
|
2018-06-28 00:48:51 +02:00
|
|
|
import ujson
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2018-06-28 00:48:51 +02:00
|
|
|
from django.db import transaction
|
2018-01-30 21:03:59 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-08-14 03:33:31 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-07-13 17:34:39 +02:00
|
|
|
from django.core.signing import Signer
|
2018-01-30 20:49:25 +01:00
|
|
|
import stripe
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-01-30 20:49:25 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
2018-07-13 17:34:39 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.models import Realm, UserProfile, RealmAuditLog
|
2018-12-28 07:20:30 +01:00
|
|
|
from corporate.models import Customer, CustomerPlan, LicenseLedger, \
|
|
|
|
get_active_plan
|
2018-01-30 20:49:25 +01:00
|
|
|
from zproject.settings import get_secret
|
|
|
|
|
|
|
|
STRIPE_PUBLISHABLE_KEY = get_secret('stripe_publishable_key')
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe.api_key = get_secret('stripe_secret_key')
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
BILLING_LOG_PATH = os.path.join('/var/log/zulip'
|
|
|
|
if not settings.DEVELOPMENT
|
|
|
|
else settings.DEVELOPMENT_LOG_DIRECTORY,
|
|
|
|
'billing.log')
|
2018-09-25 12:33:30 +02:00
|
|
|
billing_logger = logging.getLogger('corporate.stripe')
|
2018-01-30 20:49:25 +01:00
|
|
|
log_to_file(billing_logger, BILLING_LOG_PATH)
|
|
|
|
log_to_file(logging.getLogger('stripe'), BILLING_LOG_PATH)
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
CallableT = TypeVar('CallableT', bound=Callable[..., Any])
|
|
|
|
|
2018-12-22 01:43:44 +01:00
|
|
|
MIN_INVOICED_LICENSES = 30
|
2018-09-08 00:49:54 +02:00
|
|
|
DEFAULT_INVOICE_DAYS_UNTIL_DUE = 30
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
def get_seat_count(realm: Realm) -> int:
|
|
|
|
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count()
|
|
|
|
|
2018-07-13 17:34:39 +02:00
|
|
|
def sign_string(string: str) -> Tuple[str, str]:
|
|
|
|
salt = generate_random_token(64)
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.sign(string), salt
|
|
|
|
|
|
|
|
def unsign_string(signed_string: str, salt: str) -> str:
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.unsign(signed_string)
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Be extremely careful changing this function. Historical billing periods
|
|
|
|
# are not stored anywhere, and are just computed on the fly using this
|
|
|
|
# function. Any change you make here should return the same value (or be
|
|
|
|
# within a few seconds) for basically any value from when the billing system
|
|
|
|
# went online to within a year from now.
|
|
|
|
def add_months(dt: datetime, months: int) -> datetime:
|
|
|
|
assert(months >= 0)
|
|
|
|
# It's fine that the max day in Feb is 28 for leap years.
|
|
|
|
MAX_DAY_FOR_MONTH = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30,
|
|
|
|
7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
|
|
|
|
year = dt.year
|
|
|
|
month = dt.month + months
|
|
|
|
while month > 12:
|
|
|
|
year += 1
|
|
|
|
month -= 12
|
|
|
|
day = min(dt.day, MAX_DAY_FOR_MONTH[month])
|
|
|
|
# datetimes don't support leap seconds, so don't need to worry about those
|
|
|
|
return dt.replace(year=year, month=month, day=day)
|
|
|
|
|
|
|
|
def next_month(billing_cycle_anchor: datetime, dt: datetime) -> datetime:
|
|
|
|
estimated_months = round((dt - billing_cycle_anchor).days * 12. / 365)
|
|
|
|
for months in range(max(estimated_months - 1, 0), estimated_months + 2):
|
|
|
|
proposed_next_month = add_months(billing_cycle_anchor, months)
|
|
|
|
if 20 < (proposed_next_month - dt).days < 40:
|
|
|
|
return proposed_next_month
|
|
|
|
raise AssertionError('Something wrong in next_month calculation with '
|
|
|
|
'billing_cycle_anchor: %s, dt: %s' % (billing_cycle_anchor, dt))
|
|
|
|
|
|
|
|
# TODO take downgrade into account
|
2019-01-26 20:45:26 +01:00
|
|
|
def next_renewal_date(plan: CustomerPlan, event_time: datetime) -> datetime:
|
2018-12-15 09:33:25 +01:00
|
|
|
months_per_period = {
|
|
|
|
CustomerPlan.ANNUAL: 12,
|
|
|
|
CustomerPlan.MONTHLY: 1,
|
|
|
|
}[plan.billing_schedule]
|
|
|
|
periods = 1
|
|
|
|
dt = plan.billing_cycle_anchor
|
2019-01-26 20:45:26 +01:00
|
|
|
while dt <= event_time:
|
2018-12-15 09:33:25 +01:00
|
|
|
dt = add_months(plan.billing_cycle_anchor, months_per_period * periods)
|
|
|
|
periods += 1
|
|
|
|
return dt
|
|
|
|
|
2019-01-28 22:57:29 +01:00
|
|
|
# TODO take downgrade into account
|
|
|
|
def next_invoice_date(plan: CustomerPlan) -> datetime:
|
|
|
|
months_per_period = {
|
|
|
|
CustomerPlan.ANNUAL: 12,
|
|
|
|
CustomerPlan.MONTHLY: 1,
|
|
|
|
}[plan.billing_schedule]
|
|
|
|
if plan.automanage_licenses:
|
|
|
|
months_per_period = 1
|
|
|
|
periods = 1
|
|
|
|
dt = plan.billing_cycle_anchor
|
|
|
|
while dt <= plan.next_invoice_date:
|
|
|
|
dt = add_months(plan.billing_cycle_anchor, months_per_period * periods)
|
|
|
|
periods += 1
|
|
|
|
return dt
|
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
def renewal_amount(plan: CustomerPlan) -> Optional[int]: # nocoverage: TODO
|
2018-12-15 09:33:25 +01:00
|
|
|
if plan.fixed_price is not None:
|
2019-01-25 02:14:07 +01:00
|
|
|
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
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2018-07-27 17:47:03 +02:00
|
|
|
class BillingError(Exception):
|
2018-08-06 06:16:29 +02:00
|
|
|
# error messages
|
2018-08-11 01:23:42 +02:00
|
|
|
CONTACT_SUPPORT = _("Something went wrong. Please contact %s." % (settings.ZULIP_ADMINISTRATOR,))
|
2018-08-06 06:16:29 +02:00
|
|
|
TRY_RELOADING = _("Something went wrong. Please reload the page.")
|
|
|
|
|
|
|
|
# description is used only for tests
|
2018-12-22 05:29:25 +01:00
|
|
|
def __init__(self, description: str, message: str=CONTACT_SUPPORT) -> None:
|
2018-08-06 06:16:29 +02:00
|
|
|
self.description = description
|
|
|
|
self.message = message
|
2018-07-27 17:47:03 +02:00
|
|
|
|
2018-08-06 23:07:26 +02:00
|
|
|
class StripeCardError(BillingError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class StripeConnectionError(BillingError):
|
|
|
|
pass
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
def catch_stripe_errors(func: CallableT) -> CallableT:
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped(*args: Any, **kwargs: Any) -> Any:
|
2018-07-26 10:16:20 +02:00
|
|
|
if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage
|
|
|
|
if STRIPE_PUBLISHABLE_KEY is None:
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('missing stripe config', "Missing Stripe config. "
|
|
|
|
"See https://zulip.readthedocs.io/en/latest/subsystems/billing.html.")
|
2018-01-30 21:03:59 +01:00
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
2018-08-06 23:07:26 +02:00
|
|
|
# See https://stripe.com/docs/api/python#error_handling, though
|
|
|
|
# https://stripe.com/docs/api/ruby#error_handling suggests there are additional fields, and
|
|
|
|
# https://stripe.com/docs/error-codes gives a more detailed set of error codes
|
2018-01-30 21:03:59 +01:00
|
|
|
except stripe.error.StripeError as e:
|
2018-08-06 23:07:26 +02:00
|
|
|
err = e.json_body.get('error', {})
|
|
|
|
billing_logger.error("Stripe error: %s %s %s %s" % (
|
|
|
|
e.http_status, err.get('type'), err.get('code'), err.get('param')))
|
2018-01-30 21:03:59 +01:00
|
|
|
if isinstance(e, stripe.error.CardError):
|
2018-08-06 23:07:26 +02:00
|
|
|
# TODO: Look into i18n for this
|
|
|
|
raise StripeCardError('card error', err.get('message'))
|
|
|
|
if isinstance(e, stripe.error.RateLimitError) or \
|
|
|
|
isinstance(e, stripe.error.APIConnectionError): # nocoverage TODO
|
|
|
|
raise StripeConnectionError(
|
|
|
|
'stripe connection error',
|
|
|
|
_("Something went wrong. Please wait a few seconds and try again."))
|
|
|
|
raise BillingError('other stripe error', BillingError.CONTACT_SUPPORT)
|
2018-01-30 21:03:59 +01:00
|
|
|
return wrapped # type: ignore # https://github.com/python/mypy/issues/1927
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-08-06 18:22:55 +02:00
|
|
|
def stripe_get_customer(stripe_customer_id: str) -> stripe.Customer:
|
2018-11-17 04:41:42 +01:00
|
|
|
return stripe.Customer.retrieve(stripe_customer_id, expand=["default_source"])
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-12-15 09:33:25 +01:00
|
|
|
def do_create_customer(user: UserProfile, stripe_token: Optional[str]=None) -> Customer:
|
2018-03-31 04:13:44 +02:00
|
|
|
realm = user.realm
|
2018-08-23 06:44:00 +02:00
|
|
|
# We could do a better job of handling race conditions here, but if two
|
|
|
|
# people from a realm try to upgrade at exactly the same time, the main
|
|
|
|
# bad thing that will happen is that we will create an extra stripe
|
|
|
|
# customer that we can delete or ignore.
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe_customer = stripe.Customer.create(
|
|
|
|
description="%s (%s)" % (realm.string_id, realm.name),
|
2018-08-08 13:17:10 +02:00
|
|
|
email=user.email,
|
2018-03-31 04:13:44 +02:00
|
|
|
metadata={'realm_id': realm.id, 'realm_str': realm.string_id},
|
2018-12-12 19:41:03 +01:00
|
|
|
source=stripe_token)
|
2018-06-28 00:48:51 +02:00
|
|
|
event_time = timestamp_to_datetime(stripe_customer.created)
|
2018-08-23 03:40:38 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED,
|
|
|
|
event_time=event_time)
|
2018-08-23 07:47:05 +02:00
|
|
|
if stripe_token is not None:
|
|
|
|
RealmAuditLog.objects.create(
|
2018-09-05 09:40:29 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_CHANGED,
|
2018-08-23 07:47:05 +02:00
|
|
|
event_time=event_time)
|
2018-12-15 09:33:25 +01:00
|
|
|
customer = Customer.objects.create(realm=realm, stripe_customer_id=stripe_customer.id)
|
2018-08-22 07:49:48 +02:00
|
|
|
user.is_billing_admin = True
|
|
|
|
user.save(update_fields=["is_billing_admin"])
|
2018-12-15 09:33:25 +01:00
|
|
|
return customer
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-14 03:33:31 +02:00
|
|
|
@catch_stripe_errors
|
|
|
|
def do_replace_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer:
|
|
|
|
stripe_customer = stripe_get_customer(Customer.objects.get(realm=user.realm).stripe_customer_id)
|
|
|
|
stripe_customer.source = stripe_token
|
|
|
|
# Deletes existing card: https://stripe.com/docs/api#update_customer-source
|
|
|
|
# This can also have other side effects, e.g. it will try to pay certain past-due
|
|
|
|
# invoices: https://stripe.com/docs/api#update_customer
|
2018-10-18 19:56:17 +02:00
|
|
|
updated_stripe_customer = stripe.Customer.save(stripe_customer)
|
2018-08-14 03:33:31 +02:00
|
|
|
RealmAuditLog.objects.create(
|
2018-09-05 09:40:29 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_CHANGED,
|
2018-08-14 03:33:31 +02:00
|
|
|
event_time=timezone_now())
|
|
|
|
return updated_stripe_customer
|
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
# event_time should roughly be timezone_now(). Not designed to handle
|
|
|
|
# event_times in the past or future
|
|
|
|
# TODO handle downgrade
|
|
|
|
def add_plan_renewal_to_license_ledger_if_needed(plan: CustomerPlan, event_time: datetime) -> LicenseLedger:
|
2019-01-26 02:36:37 +01:00
|
|
|
last_ledger_entry = LicenseLedger.objects.filter(plan=plan).order_by('-id').first()
|
2019-01-26 20:45:26 +01:00
|
|
|
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)
|
|
|
|
if plan_renewal_date <= event_time:
|
|
|
|
return LicenseLedger.objects.create(
|
|
|
|
plan=plan, is_renewal=True, event_time=plan_renewal_date,
|
|
|
|
licenses=last_ledger_entry.licenses_at_next_renewal,
|
|
|
|
licenses_at_next_renewal=last_ledger_entry.licenses_at_next_renewal)
|
2018-12-28 07:20:30 +01:00
|
|
|
return last_ledger_entry
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Returns Customer instead of stripe_customer so that we don't make a Stripe
|
|
|
|
# API call if there's nothing to update
|
|
|
|
def update_or_create_stripe_customer(user: UserProfile, stripe_token: Optional[str]=None) -> Customer:
|
|
|
|
realm = user.realm
|
|
|
|
customer = Customer.objects.filter(realm=realm).first()
|
|
|
|
if customer is None:
|
|
|
|
return do_create_customer(user, stripe_token=stripe_token)
|
|
|
|
if stripe_token is not None:
|
|
|
|
do_replace_payment_source(user, stripe_token)
|
|
|
|
return customer
|
|
|
|
|
|
|
|
def compute_plan_parameters(
|
|
|
|
automanage_licenses: bool, billing_schedule: int,
|
|
|
|
discount: Optional[Decimal]) -> Tuple[datetime, datetime, datetime, int]:
|
|
|
|
# Everything in Stripe is stored as timestamps with 1 second resolution,
|
|
|
|
# so standardize on 1 second resolution.
|
|
|
|
# TODO talk about leapseconds?
|
|
|
|
billing_cycle_anchor = timezone_now().replace(microsecond=0)
|
|
|
|
if billing_schedule == CustomerPlan.ANNUAL:
|
|
|
|
# TODO use variables to account for Zulip Plus
|
|
|
|
price_per_license = 8000
|
|
|
|
period_end = add_months(billing_cycle_anchor, 12)
|
|
|
|
elif billing_schedule == CustomerPlan.MONTHLY:
|
|
|
|
price_per_license = 800
|
|
|
|
period_end = add_months(billing_cycle_anchor, 1)
|
|
|
|
else:
|
|
|
|
raise AssertionError('Unknown billing_schedule: {}'.format(billing_schedule))
|
|
|
|
if discount is not None:
|
|
|
|
# There are no fractional cents in Stripe, so round down to nearest integer.
|
|
|
|
price_per_license = int(float(price_per_license * (1 - discount / 100)) + .00001)
|
2019-01-28 14:18:21 +01:00
|
|
|
next_invoice_date = period_end
|
2018-12-15 09:33:25 +01:00
|
|
|
if automanage_licenses:
|
2019-01-28 14:18:21 +01:00
|
|
|
next_invoice_date = add_months(billing_cycle_anchor, 1)
|
|
|
|
return billing_cycle_anchor, next_invoice_date, period_end, price_per_license
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
# Only used for cloud signups
|
2018-03-31 04:13:44 +02:00
|
|
|
@catch_stripe_errors
|
2018-12-15 09:33:25 +01:00
|
|
|
def process_initial_upgrade(user: UserProfile, licenses: int, automanage_licenses: bool,
|
|
|
|
billing_schedule: int, stripe_token: Optional[str]) -> None:
|
|
|
|
realm = user.realm
|
|
|
|
customer = update_or_create_stripe_customer(user, stripe_token=stripe_token)
|
|
|
|
# TODO write a test for this
|
|
|
|
if CustomerPlan.objects.filter(customer=customer, status=CustomerPlan.ACTIVE).exists(): # nocoverage
|
2018-11-28 00:20:58 +01:00
|
|
|
# Unlikely race condition from two people upgrading (clicking "Make payment")
|
|
|
|
# at exactly the same time. Doesn't fully resolve the race condition, but having
|
|
|
|
# a check here reduces the likelihood.
|
2018-12-15 09:33:25 +01:00
|
|
|
billing_logger.warning(
|
|
|
|
"Customer {} trying to upgrade, but has an active subscription".format(customer))
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('subscribing with existing subscription', BillingError.TRY_RELOADING)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2019-01-28 14:18:21 +01:00
|
|
|
billing_cycle_anchor, next_invoice_date, period_end, price_per_license = compute_plan_parameters(
|
2018-12-15 09:33:25 +01:00
|
|
|
automanage_licenses, billing_schedule, customer.default_discount)
|
|
|
|
# The main design constraint in this function is that if you upgrade with a credit card, and the
|
|
|
|
# charge fails, everything should be rolled back as if nothing had happened. This is because we
|
|
|
|
# expect frequent card failures on initial signup.
|
|
|
|
# Hence, if we're going to charge a card, do it at the beginning, even if we later may have to
|
|
|
|
# adjust the number of licenses.
|
|
|
|
charge_automatically = stripe_token is not None
|
|
|
|
if charge_automatically:
|
|
|
|
stripe_charge = stripe.Charge.create(
|
|
|
|
amount=price_per_license * licenses,
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
|
|
|
description="Upgrade to Zulip Standard, ${} x {}".format(price_per_license/100, licenses),
|
|
|
|
receipt_email=user.email,
|
|
|
|
statement_descriptor='Zulip Standard')
|
|
|
|
# Not setting a period start and end, but maybe we should? Unclear what will make things
|
|
|
|
# most similar to the renewal case from an accounting perspective.
|
|
|
|
stripe.InvoiceItem.create(
|
|
|
|
amount=price_per_license * licenses * -1,
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
|
|
|
description="Payment (Card ending in {})".format(cast(stripe.Card, stripe_charge.source).last4),
|
|
|
|
discountable=False)
|
|
|
|
|
|
|
|
# TODO: The correctness of this relies on user creation, deactivation, etc being
|
|
|
|
# in a transaction.atomic() with the relevant RealmAuditLog entries
|
|
|
|
with transaction.atomic():
|
|
|
|
# billed_licenses can greater than licenses if users are added between the start of
|
|
|
|
# this function (process_initial_upgrade) and now
|
|
|
|
billed_licenses = max(get_seat_count(realm), licenses)
|
|
|
|
plan_params = {
|
|
|
|
'automanage_licenses': automanage_licenses,
|
|
|
|
'charge_automatically': charge_automatically,
|
|
|
|
'price_per_license': price_per_license,
|
|
|
|
'discount': customer.default_discount,
|
|
|
|
'billing_cycle_anchor': billing_cycle_anchor,
|
|
|
|
'billing_schedule': billing_schedule,
|
|
|
|
'tier': CustomerPlan.STANDARD}
|
2018-12-28 07:20:30 +01:00
|
|
|
plan = CustomerPlan.objects.create(
|
2018-12-15 09:33:25 +01:00
|
|
|
customer=customer,
|
2018-12-28 07:20:30 +01:00
|
|
|
# Deprecated, remove
|
|
|
|
licenses=-1,
|
2019-01-28 14:18:21 +01:00
|
|
|
next_invoice_date=next_invoice_date,
|
2018-12-15 09:33:25 +01:00
|
|
|
**plan_params)
|
2019-01-28 14:18:21 +01:00
|
|
|
ledger_entry = LicenseLedger.objects.create(
|
2018-12-28 07:20:30 +01:00
|
|
|
plan=plan,
|
|
|
|
is_renewal=True,
|
|
|
|
event_time=billing_cycle_anchor,
|
|
|
|
licenses=billed_licenses,
|
|
|
|
licenses_at_next_renewal=billed_licenses)
|
2019-01-28 14:18:21 +01:00
|
|
|
plan.invoiced_through = ledger_entry
|
|
|
|
plan.save(update_fields=['invoiced_through'])
|
2018-12-15 09:33:25 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm, acting_user=user, event_time=billing_cycle_anchor,
|
|
|
|
event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED,
|
|
|
|
extra_data=ujson.dumps(plan_params))
|
|
|
|
stripe.InvoiceItem.create(
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
2019-01-25 02:14:07 +01:00
|
|
|
description='Zulip Standard',
|
2018-12-15 09:33:25 +01:00
|
|
|
discountable=False,
|
|
|
|
period = {'start': datetime_to_timestamp(billing_cycle_anchor),
|
|
|
|
'end': datetime_to_timestamp(period_end)},
|
|
|
|
quantity=billed_licenses,
|
|
|
|
unit_amount=price_per_license)
|
|
|
|
|
2018-09-08 00:49:54 +02:00
|
|
|
if charge_automatically:
|
|
|
|
billing_method = 'charge_automatically'
|
|
|
|
days_until_due = None
|
|
|
|
else:
|
|
|
|
billing_method = 'send_invoice'
|
|
|
|
days_until_due = DEFAULT_INVOICE_DAYS_UNTIL_DUE
|
2018-12-15 09:33:25 +01:00
|
|
|
stripe_invoice = stripe.Invoice.create(
|
|
|
|
auto_advance=True,
|
2018-09-08 00:49:54 +02:00
|
|
|
billing=billing_method,
|
2018-12-15 09:33:25 +01:00
|
|
|
customer=customer.stripe_customer_id,
|
2018-09-08 00:49:54 +02:00
|
|
|
days_until_due=days_until_due,
|
2018-12-15 09:33:25 +01:00
|
|
|
statement_descriptor='Zulip Standard')
|
|
|
|
stripe.Invoice.finalize_invoice(stripe_invoice)
|
2018-07-27 15:37:04 +02:00
|
|
|
|
2019-01-26 02:36:37 +01:00
|
|
|
from zerver.lib.actions import do_change_plan_type
|
2018-12-15 09:33:25 +01:00
|
|
|
do_change_plan_type(realm, Realm.STANDARD)
|
2018-07-03 21:49:55 +02:00
|
|
|
|
2019-01-26 02:36:37 +01:00
|
|
|
def update_license_ledger_for_automanaged_plan(realm: Realm, plan: CustomerPlan,
|
|
|
|
event_time: datetime) -> None:
|
|
|
|
last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, event_time)
|
|
|
|
# todo: handle downgrade, where licenses_at_next_renewal should be 0
|
|
|
|
licenses_at_next_renewal = get_seat_count(realm)
|
|
|
|
licenses = max(licenses_at_next_renewal, last_ledger_entry.licenses)
|
|
|
|
LicenseLedger.objects.create(
|
|
|
|
plan=plan, event_time=event_time, licenses=licenses,
|
|
|
|
licenses_at_next_renewal=licenses_at_next_renewal)
|
|
|
|
|
|
|
|
def update_license_ledger_if_needed(realm: Realm, event_time: datetime) -> None:
|
|
|
|
customer = Customer.objects.filter(realm=realm).first()
|
|
|
|
if customer is None:
|
|
|
|
return
|
|
|
|
plan = get_active_plan(customer)
|
|
|
|
if plan is None:
|
|
|
|
return
|
|
|
|
if not plan.automanage_licenses:
|
|
|
|
return
|
|
|
|
update_license_ledger_for_automanaged_plan(realm, plan, event_time)
|
|
|
|
|
2019-01-28 22:57:29 +01:00
|
|
|
def invoice_plan(plan: CustomerPlan, event_time: datetime) -> None:
|
|
|
|
if plan.invoicing_status == CustomerPlan.STARTED:
|
|
|
|
raise NotImplementedError('Plan with invoicing_status==STARTED needs manual resolution.')
|
|
|
|
add_plan_renewal_to_license_ledger_if_needed(plan, event_time)
|
|
|
|
assert(plan.invoiced_through is not None)
|
|
|
|
licenses_base = plan.invoiced_through.licenses
|
|
|
|
invoice_item_created = False
|
|
|
|
for ledger_entry in LicenseLedger.objects.filter(plan=plan, id__gt=plan.invoiced_through.id,
|
|
|
|
event_time__lte=event_time).order_by('id'):
|
|
|
|
price_args = {} # type: Dict[str, int]
|
|
|
|
if ledger_entry.is_renewal:
|
|
|
|
if plan.fixed_price is not None:
|
|
|
|
price_args = {'amount': plan.fixed_price}
|
|
|
|
else:
|
|
|
|
assert(plan.price_per_license is not None) # needed for mypy
|
|
|
|
price_args = {'unit_amount': plan.price_per_license,
|
|
|
|
'quantity': ledger_entry.licenses}
|
|
|
|
description = "Zulip Standard - renewal"
|
|
|
|
elif ledger_entry.licenses != licenses_base:
|
|
|
|
assert(plan.price_per_license)
|
|
|
|
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)
|
|
|
|
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}
|
|
|
|
description = "Additional license ({} - {})".format(
|
|
|
|
ledger_entry.event_time.strftime('%b %-d, %Y'), period_end.strftime('%b %-d, %Y'))
|
|
|
|
|
|
|
|
if price_args:
|
|
|
|
plan.invoiced_through = ledger_entry
|
|
|
|
plan.invoicing_status = CustomerPlan.STARTED
|
|
|
|
plan.save(update_fields=['invoicing_status', 'invoiced_through'])
|
|
|
|
stripe.InvoiceItem.create(
|
|
|
|
currency='usd',
|
|
|
|
customer=plan.customer.stripe_customer_id,
|
|
|
|
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))},
|
|
|
|
idempotency_key='ledger_entry:{}'.format(ledger_entry.id),
|
|
|
|
**price_args)
|
|
|
|
invoice_item_created = True
|
|
|
|
plan.invoiced_through = ledger_entry
|
|
|
|
plan.invoicing_status = CustomerPlan.DONE
|
|
|
|
plan.save(update_fields=['invoicing_status', 'invoiced_through'])
|
|
|
|
licenses_base = ledger_entry.licenses
|
|
|
|
|
|
|
|
if invoice_item_created:
|
|
|
|
if plan.charge_automatically:
|
|
|
|
billing_method = 'charge_automatically'
|
|
|
|
days_until_due = None
|
|
|
|
else:
|
|
|
|
billing_method = 'send_invoice'
|
|
|
|
days_until_due = DEFAULT_INVOICE_DAYS_UNTIL_DUE
|
|
|
|
stripe_invoice = stripe.Invoice.create(
|
|
|
|
auto_advance=True,
|
|
|
|
billing=billing_method,
|
|
|
|
customer=plan.customer.stripe_customer_id,
|
|
|
|
days_until_due=days_until_due,
|
|
|
|
statement_descriptor='Zulip Standard')
|
|
|
|
stripe.Invoice.finalize_invoice(stripe_invoice)
|
|
|
|
|
|
|
|
plan.next_invoice_date = next_invoice_date(plan)
|
|
|
|
plan.save(update_fields=['next_invoice_date'])
|
|
|
|
|
|
|
|
def invoice_plans_as_needed(event_time: datetime) -> None:
|
|
|
|
for plan in CustomerPlan.objects.filter(next_invoice_date__lte=event_time):
|
|
|
|
invoice_plan(plan, event_time)
|
|
|
|
|
2018-12-12 19:41:03 +01:00
|
|
|
def attach_discount_to_realm(user: UserProfile, discount: Decimal) -> None:
|
2018-08-23 07:45:19 +02:00
|
|
|
customer = Customer.objects.filter(realm=user.realm).first()
|
|
|
|
if customer is None:
|
2018-12-15 09:33:25 +01:00
|
|
|
customer = do_create_customer(user)
|
2018-12-12 19:41:03 +01:00
|
|
|
customer.default_discount = discount
|
|
|
|
customer.save()
|
2018-08-23 07:45:19 +02:00
|
|
|
|
2018-12-12 07:47:53 +01:00
|
|
|
def process_downgrade(user: UserProfile) -> None: # nocoverage
|
|
|
|
pass
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverage
|
|
|
|
annual_revenue = {}
|
|
|
|
for plan in CustomerPlan.objects.filter(
|
|
|
|
status=CustomerPlan.ACTIVE).select_related('customer__realm'):
|
2018-12-28 07:20:30 +01:00
|
|
|
# TODO: figure out what to do for plans that don't automatically
|
|
|
|
# renew, but which probably will renew
|
|
|
|
renewal_cents = renewal_amount(plan) or 0
|
2018-12-15 09:33:25 +01:00
|
|
|
if plan.billing_schedule == CustomerPlan.MONTHLY:
|
|
|
|
renewal_cents *= 12
|
|
|
|
# TODO: Decimal stuff
|
|
|
|
annual_revenue[plan.customer.realm.string_id] = int(renewal_cents / 100)
|
|
|
|
return annual_revenue
|