2018-09-25 12:24:11 +02:00
|
|
|
import logging
|
2021-08-29 15:33:29 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import stripe
|
|
|
|
from django.conf import settings
|
2018-09-25 12:24:11 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
2019-02-02 23:53:22 +01:00
|
|
|
from django.shortcuts import render
|
2018-09-25 12:24:11 +02:00
|
|
|
from django.urls import reverse
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from corporate.lib.stripe import (
|
2020-10-20 15:46:04 +02:00
|
|
|
cents_to_dollar_string,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_change_plan_status,
|
2020-08-13 13:06:05 +02:00
|
|
|
downgrade_at_the_end_of_billing_cycle,
|
2020-08-13 10:22:58 +02:00
|
|
|
downgrade_now_without_creating_additional_invoices,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_latest_seat_count,
|
|
|
|
make_end_of_cycle_updates_if_needed,
|
|
|
|
renewal_amount,
|
|
|
|
start_of_next_billing_cycle,
|
|
|
|
stripe_get_customer,
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan,
|
2020-12-17 16:33:19 +01:00
|
|
|
validate_licenses,
|
2020-06-11 00:54:34 +02:00
|
|
|
)
|
|
|
|
from corporate.models import (
|
|
|
|
CustomerPlan,
|
|
|
|
get_current_plan_by_customer,
|
|
|
|
get_current_plan_by_realm,
|
|
|
|
get_customer_by_realm,
|
|
|
|
)
|
2021-07-15 16:38:37 +02:00
|
|
|
from zerver.decorator import require_billing_access, zulip_login_required
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-09-25 12:24:11 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-07-04 08:19:18 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-07-29 19:01:39 +02:00
|
|
|
from zerver.lib.validator import check_bool, check_int, check_int_in
|
2023-02-13 20:40:51 +01:00
|
|
|
from zerver.models import Realm, UserProfile
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
billing_logger = logging.getLogger("corporate.stripe")
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-12-23 09:10:57 +01:00
|
|
|
# Should only be called if the customer is being charged automatically
|
|
|
|
def payment_method_string(stripe_customer: stripe.Customer) -> str:
|
2021-08-29 15:33:29 +02:00
|
|
|
default_payment_method = stripe_customer.invoice_settings.default_payment_method
|
|
|
|
if default_payment_method is None:
|
2018-09-08 00:49:54 +02:00
|
|
|
return _("No payment method on file")
|
2021-08-29 15:33:29 +02:00
|
|
|
|
|
|
|
if default_payment_method.type == "card":
|
2020-06-15 23:22:24 +02:00
|
|
|
return _("{brand} ending in {last4}").format(
|
2021-08-29 15:33:29 +02:00
|
|
|
brand=default_payment_method.card.brand,
|
|
|
|
last4=default_payment_method.card.last4,
|
2020-06-15 23:22:24 +02:00
|
|
|
)
|
2022-02-08 00:13:33 +01:00
|
|
|
# There might be one-off stuff we do for a particular customer that
|
2018-12-23 09:10:57 +01:00
|
|
|
# would land them here. E.g. by default we don't support ACH for
|
|
|
|
# automatic payments, but in theory we could add it for a customer via
|
|
|
|
# the Stripe dashboard.
|
2020-06-15 23:22:24 +02:00
|
|
|
return _("Unknown payment method. Please contact {email}.").format(
|
|
|
|
email=settings.ZULIP_ADMINISTRATOR,
|
|
|
|
) # nocoverage
|
2018-09-08 00:49:54 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-02-13 20:40:51 +01:00
|
|
|
def add_sponsorship_info_to_context(context: Dict[str, Any], user_profile: UserProfile) -> None:
|
|
|
|
def key_helper(d: Any) -> int:
|
|
|
|
return d[1]["display_order"]
|
|
|
|
|
|
|
|
context.update(
|
|
|
|
realm_org_type=user_profile.realm.org_type,
|
|
|
|
sorted_org_types=sorted(
|
|
|
|
(
|
|
|
|
[org_type_name, org_type]
|
|
|
|
for (org_type_name, org_type) in Realm.ORG_TYPES.items()
|
|
|
|
if not org_type.get("hidden")
|
|
|
|
),
|
|
|
|
key=key_helper,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
@zulip_login_required
|
2021-07-29 19:01:39 +02:00
|
|
|
@has_request_variables
|
|
|
|
def billing_home(
|
|
|
|
request: HttpRequest, onboarding: bool = REQ(default=False, json_validator=check_bool)
|
|
|
|
) -> HttpResponse:
|
2018-09-25 12:24:11 +02:00
|
|
|
user = request.user
|
2021-07-24 20:37:35 +02:00
|
|
|
assert user.is_authenticated
|
|
|
|
|
2020-03-23 13:35:04 +01:00
|
|
|
customer = get_customer_by_realm(user.realm)
|
2020-08-21 14:45:43 +02:00
|
|
|
context: Dict[str, Any] = {
|
|
|
|
"admin_access": user.has_billing_access,
|
2021-02-12 08:20:45 +01:00
|
|
|
"has_active_plan": False,
|
2020-08-21 14:45:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 23:28:17 +02:00
|
|
|
if user.realm.plan_type == user.realm.PLAN_TYPE_STANDARD_FREE:
|
2020-08-21 14:45:43 +02:00
|
|
|
context["is_sponsored"] = True
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "corporate/billing.html", context=context)
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2018-09-25 12:24:11 +02:00
|
|
|
if customer is None:
|
2021-07-15 16:38:37 +02:00
|
|
|
from corporate.views.upgrade import initial_upgrade
|
|
|
|
|
2020-09-22 02:54:44 +02:00
|
|
|
return HttpResponseRedirect(reverse(initial_upgrade))
|
2020-06-09 12:24:32 +02:00
|
|
|
|
|
|
|
if customer.sponsorship_pending:
|
2020-08-21 14:45:43 +02:00
|
|
|
context["sponsorship_pending"] = True
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "corporate/billing.html", context=context)
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
if not CustomerPlan.objects.filter(customer=customer).exists():
|
2021-07-15 16:38:37 +02:00
|
|
|
from corporate.views.upgrade import initial_upgrade
|
|
|
|
|
2020-09-22 02:54:44 +02:00
|
|
|
return HttpResponseRedirect(reverse(initial_upgrade))
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-06-09 12:24:32 +02:00
|
|
|
if not user.has_billing_access:
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "corporate/billing.html", context=context)
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-03-24 14:14:03 +01:00
|
|
|
plan = get_current_plan_by_customer(customer)
|
2018-12-15 09:33:25 +01:00
|
|
|
if plan is not None:
|
2019-01-26 20:45:26 +01:00
|
|
|
now = timezone_now()
|
2020-06-15 20:09:24 +02:00
|
|
|
new_plan, last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, now)
|
2019-04-08 05:16:35 +02:00
|
|
|
if last_ledger_entry is not None:
|
2020-06-15 20:09:24 +02:00
|
|
|
if new_plan is not None: # nocoverage
|
|
|
|
plan = new_plan
|
2021-02-12 08:19:30 +01:00
|
|
|
assert plan is not None # for mypy
|
2020-04-24 17:38:13 +02:00
|
|
|
downgrade_at_end_of_cycle = plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
|
2021-02-12 08:19:30 +01:00
|
|
|
switch_to_annual_at_end_of_cycle = (
|
|
|
|
plan.status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE
|
|
|
|
)
|
2019-04-08 05:16:35 +02:00
|
|
|
licenses = last_ledger_entry.licenses
|
2020-12-23 17:08:27 +01:00
|
|
|
licenses_at_next_renewal = last_ledger_entry.licenses_at_next_renewal
|
2020-12-17 16:34:24 +01:00
|
|
|
seat_count = get_latest_seat_count(user.realm)
|
|
|
|
|
2022-02-24 21:15:43 +01:00
|
|
|
# Should do this in JavaScript, using the user's time zone
|
2021-09-20 13:29:36 +02:00
|
|
|
if plan.is_free_trial():
|
|
|
|
assert plan.next_invoice_date is not None
|
|
|
|
renewal_date = "{dt:%B} {dt.day}, {dt.year}".format(dt=plan.next_invoice_date)
|
|
|
|
else:
|
|
|
|
renewal_date = "{dt:%B} {dt.day}, {dt.year}".format(
|
|
|
|
dt=start_of_next_billing_cycle(plan, now)
|
|
|
|
)
|
|
|
|
|
2019-04-08 05:16:35 +02:00
|
|
|
renewal_cents = renewal_amount(plan, now)
|
|
|
|
charge_automatically = plan.charge_automatically
|
2021-06-18 21:10:45 +02:00
|
|
|
assert customer.stripe_customer_id is not None # for mypy
|
2020-03-26 19:08:00 +01:00
|
|
|
stripe_customer = stripe_get_customer(customer.stripe_customer_id)
|
2019-04-08 05:16:35 +02:00
|
|
|
if charge_automatically:
|
|
|
|
payment_method = payment_method_string(stripe_customer)
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
payment_method = "Billed by invoice"
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2020-09-03 05:32:15 +02:00
|
|
|
context.update(
|
|
|
|
plan_name=plan.name,
|
|
|
|
has_active_plan=True,
|
2020-11-11 14:02:47 +01:00
|
|
|
free_trial=plan.is_free_trial(),
|
2020-09-03 05:32:15 +02:00
|
|
|
downgrade_at_end_of_cycle=downgrade_at_end_of_cycle,
|
|
|
|
automanage_licenses=plan.automanage_licenses,
|
|
|
|
switch_to_annual_at_end_of_cycle=switch_to_annual_at_end_of_cycle,
|
|
|
|
licenses=licenses,
|
2020-12-23 17:08:27 +01:00
|
|
|
licenses_at_next_renewal=licenses_at_next_renewal,
|
2020-12-17 16:34:24 +01:00
|
|
|
seat_count=seat_count,
|
2020-09-03 05:32:15 +02:00
|
|
|
renewal_date=renewal_date,
|
2020-10-20 15:46:04 +02:00
|
|
|
renewal_amount=cents_to_dollar_string(renewal_cents),
|
2020-09-03 05:32:15 +02:00
|
|
|
payment_method=payment_method,
|
|
|
|
charge_automatically=charge_automatically,
|
|
|
|
stripe_email=stripe_customer.email,
|
|
|
|
CustomerPlan=CustomerPlan,
|
2021-07-29 19:01:39 +02:00
|
|
|
onboarding=onboarding,
|
2020-09-03 05:32:15 +02:00
|
|
|
)
|
2023-02-13 20:40:51 +01:00
|
|
|
add_sponsorship_info_to_context(context, user)
|
2020-04-03 16:17:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "corporate/billing.html", context=context)
|
2018-09-25 12:24:11 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-01 11:26:29 +01:00
|
|
|
@require_billing_access
|
2019-04-08 05:16:35 +02:00
|
|
|
@has_request_variables
|
2020-12-10 18:15:09 +01:00
|
|
|
def update_plan(
|
2021-04-14 15:50:40 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user: UserProfile,
|
2020-12-10 18:15:09 +01:00
|
|
|
status: Optional[int] = REQ(
|
2021-04-14 15:50:40 +02:00
|
|
|
"status",
|
|
|
|
json_validator=check_int_in(
|
|
|
|
[
|
|
|
|
CustomerPlan.ACTIVE,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.ENDED,
|
|
|
|
]
|
|
|
|
),
|
2020-12-10 18:15:09 +01:00
|
|
|
default=None,
|
2021-04-14 15:50:40 +02:00
|
|
|
),
|
2020-12-23 17:08:27 +01:00
|
|
|
licenses: Optional[int] = REQ("licenses", json_validator=check_int, default=None),
|
|
|
|
licenses_at_next_renewal: Optional[int] = REQ(
|
|
|
|
"licenses_at_next_renewal", json_validator=check_int, default=None
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-03-24 14:22:27 +01:00
|
|
|
plan = get_current_plan_by_realm(user.realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
assert plan is not None # for mypy
|
2020-04-23 20:10:15 +02:00
|
|
|
|
2020-12-17 16:35:33 +01:00
|
|
|
new_plan, last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, timezone_now())
|
|
|
|
if new_plan is not None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-17 16:35:33 +01:00
|
|
|
_("Unable to update the plan. The plan has been expired and replaced with a new plan.")
|
|
|
|
)
|
|
|
|
|
|
|
|
if last_ledger_entry is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Unable to update the plan. The plan has ended."))
|
2020-12-17 16:35:33 +01:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
if status is not None:
|
|
|
|
if status == CustomerPlan.ACTIVE:
|
|
|
|
assert plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
|
|
|
|
do_change_plan_status(plan, status)
|
|
|
|
elif status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE:
|
|
|
|
assert plan.status == CustomerPlan.ACTIVE
|
|
|
|
downgrade_at_the_end_of_billing_cycle(user.realm)
|
|
|
|
elif status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE:
|
|
|
|
assert plan.billing_schedule == CustomerPlan.MONTHLY
|
|
|
|
assert plan.status == CustomerPlan.ACTIVE
|
|
|
|
assert plan.fixed_price is None
|
|
|
|
do_change_plan_status(plan, status)
|
|
|
|
elif status == CustomerPlan.ENDED:
|
2020-11-11 14:02:47 +01:00
|
|
|
assert plan.is_free_trial()
|
2020-12-10 18:15:09 +01:00
|
|
|
downgrade_now_without_creating_additional_invoices(user.realm)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-10 18:15:09 +01:00
|
|
|
|
2020-12-23 17:08:27 +01:00
|
|
|
if licenses is not None:
|
|
|
|
if plan.automanage_licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Unable to update licenses manually. Your plan is on automatic license management."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses == licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Your plan is already on {licenses} licenses in the current billing period."
|
|
|
|
).format(licenses=licenses)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses > licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_("You cannot decrease the licenses in the current billing period.").format(
|
|
|
|
licenses=licenses
|
|
|
|
)
|
|
|
|
)
|
2023-04-10 21:48:52 +02:00
|
|
|
validate_licenses(
|
|
|
|
plan.charge_automatically,
|
|
|
|
licenses,
|
|
|
|
get_latest_seat_count(user.realm),
|
|
|
|
plan.customer.exempt_from_license_number_check,
|
|
|
|
)
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan(plan, timezone_now(), licenses=licenses)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-23 17:08:27 +01:00
|
|
|
|
|
|
|
if licenses_at_next_renewal is not None:
|
|
|
|
if plan.automanage_licenses:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Unable to update licenses manually. Your plan is on automatic license management."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if last_ledger_entry.licenses_at_next_renewal == licenses_at_next_renewal:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2020-12-23 17:08:27 +01:00
|
|
|
_(
|
|
|
|
"Your plan is already scheduled to renew with {licenses_at_next_renewal} licenses."
|
|
|
|
).format(licenses_at_next_renewal=licenses_at_next_renewal)
|
|
|
|
)
|
2021-07-04 08:19:18 +02:00
|
|
|
validate_licenses(
|
|
|
|
plan.charge_automatically,
|
|
|
|
licenses_at_next_renewal,
|
|
|
|
get_latest_seat_count(user.realm),
|
2023-04-10 21:48:52 +02:00
|
|
|
plan.customer.exempt_from_license_number_check,
|
2021-07-04 08:19:18 +02:00
|
|
|
)
|
2020-12-23 17:08:27 +01:00
|
|
|
update_license_ledger_for_manual_plan(
|
|
|
|
plan, timezone_now(), licenses_at_next_renewal=licenses_at_next_renewal
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-12-23 17:08:27 +01:00
|
|
|
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Nothing to change."))
|