2024-07-02 21:25:04 +02:00
|
|
|
from django.conf import settings
|
2024-08-07 20:40:03 +02:00
|
|
|
from django.db.models import F
|
2024-08-29 12:51:36 +02:00
|
|
|
from datetime import datetime, timedelta
|
2024-05-08 19:30:53 +02:00
|
|
|
from enum import Enum
|
2024-06-11 21:35:30 +02:00
|
|
|
from typing import Any, Optional, cast
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
import jwt
|
|
|
|
import requests
|
|
|
|
import structlog
|
2024-08-29 12:51:36 +02:00
|
|
|
from django.utils import timezone
|
2024-08-07 20:40:03 +02:00
|
|
|
from sentry_sdk import capture_message
|
2024-04-26 23:45:39 +02:00
|
|
|
from requests import JSONDecodeError # type: ignore[attr-defined]
|
2023-02-13 23:43:53 +01:00
|
|
|
from rest_framework.exceptions import NotAuthenticated
|
2023-11-21 19:39:37 +01:00
|
|
|
from sentry_sdk import capture_exception
|
2023-02-13 23:43:53 +01:00
|
|
|
|
2024-06-11 21:35:30 +02:00
|
|
|
from ee.billing.billing_types import BillingStatus
|
2023-02-14 20:17:26 +01:00
|
|
|
from ee.billing.quota_limiting import set_org_usage_summary, sync_org_quota_limits
|
2023-02-13 23:43:53 +01:00
|
|
|
from ee.models import License
|
|
|
|
from ee.settings import BILLING_SERVICE_URL
|
2023-08-17 20:41:25 +02:00
|
|
|
from posthog.cloud_utils import get_cached_instance_license
|
2023-02-13 23:43:53 +01:00
|
|
|
from posthog.models import Organization
|
2023-11-21 19:39:37 +01:00
|
|
|
from posthog.models.organization import OrganizationMembership, OrganizationUsageInfo
|
2024-10-10 18:31:55 +02:00
|
|
|
from posthog.models.user import User
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
logger = structlog.get_logger(__name__)
|
|
|
|
|
|
|
|
|
2024-05-08 19:30:53 +02:00
|
|
|
class BillingAPIErrorCodes(Enum):
|
|
|
|
OPEN_INVOICES_ERROR = "open_invoices_error"
|
|
|
|
|
|
|
|
|
2024-10-10 18:31:55 +02:00
|
|
|
def build_billing_token(license: License, organization: Organization, user: Optional[User] = None):
|
2023-02-13 23:43:53 +01:00
|
|
|
if not organization or not license:
|
|
|
|
raise NotAuthenticated()
|
|
|
|
|
|
|
|
license_id = license.key.split("::")[0]
|
|
|
|
license_secret = license.key.split("::")[1]
|
|
|
|
|
2024-10-10 18:31:55 +02:00
|
|
|
payload = {
|
|
|
|
"exp": datetime.now(tz=timezone.utc) + timedelta(minutes=15),
|
|
|
|
"id": license_id,
|
|
|
|
"organization_id": str(organization.id),
|
|
|
|
"organization_name": organization.name,
|
|
|
|
"aud": "posthog:license-key",
|
|
|
|
}
|
|
|
|
|
|
|
|
if user:
|
|
|
|
payload["distinct_id"] = str(user.distinct_id)
|
|
|
|
|
2023-02-13 23:43:53 +01:00
|
|
|
encoded_jwt = jwt.encode(
|
2024-10-10 18:31:55 +02:00
|
|
|
payload,
|
2023-02-13 23:43:53 +01:00
|
|
|
license_secret,
|
|
|
|
algorithm="HS256",
|
|
|
|
)
|
|
|
|
|
|
|
|
return encoded_jwt
|
|
|
|
|
|
|
|
|
|
|
|
def handle_billing_service_error(res: requests.Response, valid_codes=(200, 404, 401)) -> None:
|
|
|
|
if res.status_code not in valid_codes:
|
|
|
|
logger.error(f"Billing service returned bad status code: {res.status_code}, body: {res.text}")
|
2024-04-26 20:56:30 +02:00
|
|
|
try:
|
|
|
|
response = res.json()
|
|
|
|
raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", response)
|
2024-04-26 23:45:39 +02:00
|
|
|
except JSONDecodeError:
|
2024-04-26 20:56:30 +02:00
|
|
|
raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", res.text)
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BillingManager:
|
|
|
|
license: Optional[License]
|
2024-10-10 18:31:55 +02:00
|
|
|
user: Optional[User]
|
2023-02-13 23:43:53 +01:00
|
|
|
|
2024-10-10 18:31:55 +02:00
|
|
|
def __init__(self, license, user: Optional[User] = None):
|
2023-08-17 20:41:25 +02:00
|
|
|
self.license = license or get_cached_instance_license()
|
2024-10-10 18:31:55 +02:00
|
|
|
self.user = user
|
2023-02-13 23:43:53 +01:00
|
|
|
|
2024-04-25 09:22:28 +02:00
|
|
|
def get_billing(self, organization: Optional[Organization], plan_keys: Optional[str]) -> dict[str, Any]:
|
2023-02-13 23:43:53 +01:00
|
|
|
if organization and self.license and self.license.is_v2_license:
|
|
|
|
billing_service_response = self._get_billing(organization)
|
|
|
|
|
|
|
|
# Ensure the license and org are updated with the latest info
|
|
|
|
if billing_service_response.get("license"):
|
|
|
|
self.update_license_details(billing_service_response)
|
2024-05-08 19:30:53 +02:00
|
|
|
|
2023-02-13 23:43:53 +01:00
|
|
|
if organization and billing_service_response:
|
|
|
|
self.update_org_details(organization, billing_service_response)
|
|
|
|
|
2024-05-29 02:29:24 +02:00
|
|
|
response: dict[str, Any] = {"available_product_features": []}
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
response["license"] = {"plan": self.license.plan}
|
|
|
|
|
|
|
|
if organization and billing_service_response.get("customer"):
|
|
|
|
response.update(billing_service_response["customer"])
|
|
|
|
|
|
|
|
if not billing_service_response["customer"].get("products"):
|
|
|
|
products = self.get_default_products(organization)
|
|
|
|
response["products"] = products["products"]
|
|
|
|
|
2024-07-02 21:25:04 +02:00
|
|
|
response["stripe_portal_url"] = f"{settings.SITE_URL}/api/billing/portal"
|
2024-05-14 17:06:51 +02:00
|
|
|
|
|
|
|
# Extend the products with accurate usage_limit info
|
|
|
|
for product in response["products"]:
|
2024-06-04 21:28:16 +02:00
|
|
|
usage_key = product.get("usage_key")
|
2024-05-14 17:06:51 +02:00
|
|
|
if not usage_key:
|
|
|
|
continue
|
|
|
|
usage = response.get("usage_summary", {}).get(usage_key, {})
|
|
|
|
usage_limit = usage.get("limit")
|
2024-06-05 17:18:08 +02:00
|
|
|
billing_reported_usage = usage.get("usage") or 0
|
|
|
|
current_usage = billing_reported_usage
|
2024-05-14 17:06:51 +02:00
|
|
|
|
2024-06-04 21:28:16 +02:00
|
|
|
product_usage: dict[str, Any] = {}
|
|
|
|
if organization and organization.usage:
|
|
|
|
product_usage = organization.usage.get(usage_key) or {}
|
|
|
|
|
|
|
|
if product_usage.get("todays_usage"):
|
|
|
|
todays_usage = product_usage["todays_usage"]
|
2024-06-05 17:18:08 +02:00
|
|
|
current_usage = billing_reported_usage + todays_usage
|
2024-05-14 17:06:51 +02:00
|
|
|
|
|
|
|
product["current_usage"] = current_usage
|
|
|
|
product["percentage_usage"] = current_usage / usage_limit if usage_limit else 0
|
2023-02-13 23:43:53 +01:00
|
|
|
else:
|
|
|
|
products = self.get_default_products(organization)
|
|
|
|
response = {
|
2024-05-29 02:29:24 +02:00
|
|
|
"available_product_features": [],
|
2023-02-13 23:43:53 +01:00
|
|
|
"products": products["products"],
|
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2024-04-25 09:22:28 +02:00
|
|
|
def update_billing(self, organization: Organization, data: dict[str, Any]) -> None:
|
2023-02-13 23:43:53 +01:00
|
|
|
res = requests.patch(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/billing/",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
2024-08-07 20:40:03 +02:00
|
|
|
def update_billing_organization_users(self, organization: Organization) -> None:
|
2023-11-21 19:39:37 +01:00
|
|
|
try:
|
2024-11-08 01:45:21 +01:00
|
|
|
distinct_ids = list(organization.members.values_list("distinct_id", flat=True)) # type: ignore
|
2024-08-07 20:40:03 +02:00
|
|
|
|
|
|
|
first_owner_membership = (
|
|
|
|
OrganizationMembership.objects.filter(organization=organization, level=15)
|
|
|
|
.order_by("-joined_at")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if not first_owner_membership:
|
|
|
|
capture_message(f"No owner membership found for organization {organization.id}")
|
|
|
|
return
|
|
|
|
first_owner = first_owner_membership.user
|
2023-11-21 19:39:37 +01:00
|
|
|
|
2024-05-22 21:49:13 +02:00
|
|
|
admin_emails = list(
|
|
|
|
organization.members.filter(
|
|
|
|
organization_membership__level__gte=OrganizationMembership.Level.ADMIN
|
|
|
|
).values_list("email", flat=True)
|
|
|
|
)
|
2024-08-07 20:40:03 +02:00
|
|
|
|
|
|
|
org_users = list(
|
2024-11-08 01:45:21 +01:00
|
|
|
organization.members.values( # type: ignore
|
2024-08-07 20:40:03 +02:00
|
|
|
"email",
|
|
|
|
"distinct_id",
|
|
|
|
"organization_membership__level",
|
|
|
|
)
|
2024-10-30 17:15:46 +01:00
|
|
|
.order_by("email") # Deterministic order for tests
|
2024-08-07 20:40:03 +02:00
|
|
|
.annotate(role=F("organization_membership__level"))
|
|
|
|
.filter(role__gte=OrganizationMembership.Level.ADMIN)
|
|
|
|
.values(
|
|
|
|
"email",
|
|
|
|
"distinct_id",
|
|
|
|
"role",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.update_billing(
|
|
|
|
organization,
|
|
|
|
{
|
|
|
|
"distinct_ids": distinct_ids,
|
|
|
|
"org_customer_email": first_owner.email,
|
|
|
|
"org_admin_emails": admin_emails,
|
|
|
|
"org_users": org_users,
|
|
|
|
},
|
|
|
|
)
|
2024-05-22 21:49:13 +02:00
|
|
|
except Exception as e:
|
|
|
|
capture_exception(e)
|
|
|
|
|
2023-04-13 19:43:33 +02:00
|
|
|
def deactivate_products(self, organization: Organization, products: str) -> None:
|
|
|
|
res = requests.get(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/billing/deactivate?products={products}",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
2024-03-07 19:15:11 +01:00
|
|
|
def get_default_products(self, organization: Optional[Organization]) -> dict:
|
2023-02-13 23:43:53 +01:00
|
|
|
response = {}
|
|
|
|
# If we don't have products from the billing service then get the default ones with our local usage calculation
|
|
|
|
products = self._get_products(organization)
|
2023-06-12 17:07:52 +02:00
|
|
|
response["products"] = products
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def update_license_details(self, billing_status: BillingStatus) -> License:
|
|
|
|
"""
|
|
|
|
Ensure the license details are up-to-date locally
|
|
|
|
"""
|
|
|
|
if not self.license: # mypy
|
|
|
|
raise Exception("No license found")
|
|
|
|
|
|
|
|
license_modified = False
|
|
|
|
|
|
|
|
data = billing_status["license"]
|
|
|
|
|
2024-08-29 12:51:36 +02:00
|
|
|
if not self.license.valid_until or self.license.valid_until < timezone.now() + timedelta(days=29):
|
2023-02-13 23:43:53 +01:00
|
|
|
# NOTE: License validity is a legacy concept. For now we always extend the license validity by 30 days.
|
2024-08-29 12:51:36 +02:00
|
|
|
self.license.valid_until = timezone.now() + timedelta(days=30)
|
2023-02-13 23:43:53 +01:00
|
|
|
license_modified = True
|
|
|
|
|
|
|
|
if self.license.plan != data["type"]:
|
|
|
|
self.license.plan = data["type"]
|
|
|
|
license_modified = True
|
|
|
|
|
|
|
|
if license_modified:
|
|
|
|
self.license.save()
|
|
|
|
|
|
|
|
return self.license
|
|
|
|
|
|
|
|
def _get_billing(self, organization: Organization) -> BillingStatus:
|
|
|
|
"""
|
|
|
|
Retrieves billing info and updates local models if necessary
|
|
|
|
"""
|
|
|
|
if not self.license: # mypy
|
|
|
|
raise Exception("No license found")
|
|
|
|
|
2023-10-26 12:38:15 +02:00
|
|
|
res = requests.get(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/billing",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
2023-02-13 23:43:53 +01:00
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
data = res.json()
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2024-07-02 21:25:04 +02:00
|
|
|
def _get_stripe_portal_url(self, organization: Organization) -> str:
|
2023-04-11 18:52:46 +02:00
|
|
|
"""
|
|
|
|
Retrieves stripe protal url
|
|
|
|
"""
|
|
|
|
if not self.license: # mypy
|
|
|
|
raise Exception("No license found")
|
|
|
|
|
2023-10-26 12:38:15 +02:00
|
|
|
res = requests.get(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/billing/portal",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
2023-04-11 18:52:46 +02:00
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
data = res.json()
|
|
|
|
|
|
|
|
return data["url"]
|
|
|
|
|
2023-02-13 23:43:53 +01:00
|
|
|
def _get_products(self, organization: Optional[Organization]):
|
|
|
|
headers = {}
|
|
|
|
params = {"plan": "standard"}
|
|
|
|
|
|
|
|
if self.license and organization:
|
|
|
|
headers = self.get_auth_headers(organization)
|
|
|
|
|
|
|
|
res = requests.get(
|
2023-06-12 17:07:52 +02:00
|
|
|
f"{BILLING_SERVICE_URL}/api/products-v2",
|
2023-02-13 23:43:53 +01:00
|
|
|
params=params,
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
2023-06-12 17:07:52 +02:00
|
|
|
return res.json().get("products", [])
|
2023-02-13 23:43:53 +01:00
|
|
|
|
|
|
|
def update_org_details(self, organization: Organization, billing_status: BillingStatus) -> Organization:
|
|
|
|
"""
|
|
|
|
Ensure the relevant organization details are up-to-date locally
|
|
|
|
"""
|
|
|
|
org_modified = False
|
|
|
|
|
|
|
|
data = billing_status["customer"]
|
|
|
|
|
|
|
|
if data.get("customer_id") and organization.customer_id != data["customer_id"]:
|
|
|
|
organization.customer_id = data["customer_id"]
|
|
|
|
org_modified = True
|
|
|
|
|
|
|
|
usage_summary = cast(dict, data.get("usage_summary"))
|
|
|
|
if usage_summary:
|
2023-02-14 20:17:26 +01:00
|
|
|
usage_info = OrganizationUsageInfo(
|
|
|
|
events=usage_summary["events"],
|
|
|
|
recordings=usage_summary["recordings"],
|
2024-06-03 23:27:17 +02:00
|
|
|
rows_synced=usage_summary.get("rows_synced", {}),
|
2023-02-13 23:43:53 +01:00
|
|
|
period=[
|
|
|
|
data["billing_period"]["current_period_start"],
|
|
|
|
data["billing_period"]["current_period_end"],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2023-02-14 20:17:26 +01:00
|
|
|
if set_org_usage_summary(organization, new_usage=usage_info):
|
|
|
|
org_modified = True
|
|
|
|
sync_org_quota_limits(organization)
|
2024-05-22 16:48:24 +02:00
|
|
|
|
2023-04-27 16:50:17 +02:00
|
|
|
available_product_features = data.get("available_product_features", None)
|
|
|
|
if available_product_features and available_product_features != organization.available_product_features:
|
|
|
|
organization.available_product_features = data["available_product_features"]
|
|
|
|
org_modified = True
|
|
|
|
|
2023-10-16 16:51:18 +02:00
|
|
|
never_drop_data = data.get("never_drop_data", None)
|
|
|
|
if never_drop_data != organization.never_drop_data:
|
|
|
|
organization.never_drop_data = never_drop_data
|
|
|
|
org_modified = True
|
|
|
|
|
2024-03-07 19:15:11 +01:00
|
|
|
customer_trust_scores = data.get("customer_trust_scores", {})
|
|
|
|
|
|
|
|
product_key_to_usage_key = {
|
|
|
|
product["type"]: product["usage_key"]
|
|
|
|
for product in (
|
|
|
|
billing_status["customer"].get("products") or self.get_default_products(organization)["products"]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
org_customer_trust_scores = {}
|
|
|
|
for product_key in customer_trust_scores:
|
|
|
|
if product_key in product_key_to_usage_key:
|
|
|
|
org_customer_trust_scores[product_key_to_usage_key[product_key]] = customer_trust_scores[product_key]
|
|
|
|
|
|
|
|
if org_customer_trust_scores != organization.customer_trust_scores:
|
2024-05-27 23:39:09 +02:00
|
|
|
organization.customer_trust_scores.update(org_customer_trust_scores)
|
2024-03-07 19:15:11 +01:00
|
|
|
org_modified = True
|
|
|
|
|
2023-02-13 23:43:53 +01:00
|
|
|
if org_modified:
|
|
|
|
organization.save()
|
|
|
|
|
|
|
|
return organization
|
|
|
|
|
|
|
|
def get_auth_headers(self, organization: Organization):
|
|
|
|
if not self.license: # mypy
|
|
|
|
raise Exception("No license found")
|
2024-10-10 18:31:55 +02:00
|
|
|
billing_service_token = build_billing_token(self.license, organization, self.user)
|
2023-02-13 23:43:53 +01:00
|
|
|
return {"Authorization": f"Bearer {billing_service_token}"}
|
2024-05-08 19:30:53 +02:00
|
|
|
|
|
|
|
def get_invoices(self, organization: Organization, status: Optional[str]):
|
|
|
|
res = requests.get(
|
2024-09-11 16:38:03 +02:00
|
|
|
# TODO(@zach): update this to /api/invoices
|
2024-05-08 19:30:53 +02:00
|
|
|
f"{BILLING_SERVICE_URL}/api/billing/get_invoices",
|
|
|
|
params={"status": status},
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
data = res.json()
|
|
|
|
|
|
|
|
return data
|
2024-09-11 16:38:03 +02:00
|
|
|
|
|
|
|
def credits_overview(self, organization: Organization):
|
|
|
|
res = requests.get(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/credits/overview",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
|
|
|
|
def purchase_credits(self, organization: Organization, data: dict[str, Any]):
|
|
|
|
res = requests.post(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/credits/purchase",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
return res.json()
|
2024-10-11 21:37:01 +02:00
|
|
|
|
2024-11-01 16:50:36 +01:00
|
|
|
def activate_trial(self, organization: Organization, data: dict[str, Any]):
|
|
|
|
res = requests.post(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/trials/activate",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
|
|
|
|
def cancel_trial(self, organization: Organization, data: dict[str, Any]):
|
|
|
|
res = requests.post(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/trials/cancel",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
2024-10-11 21:37:01 +02:00
|
|
|
def authorize(self, organization: Organization):
|
|
|
|
res = requests.post(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/activate/authorize",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
|
|
|
|
def authorize_status(self, organization: Organization, data: dict[str, Any]):
|
|
|
|
res = requests.post(
|
|
|
|
f"{BILLING_SERVICE_URL}/api/activate/authorize/status",
|
|
|
|
headers=self.get_auth_headers(organization),
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_billing_service_error(res)
|
|
|
|
|
|
|
|
return res.json()
|