You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import os
  2. from typing import Literal
  3. import httpx
  4. from tenacity import retry, retry_if_exception_type, stop_before_delay, wait_fixed
  5. from extensions.ext_database import db
  6. from extensions.ext_redis import redis_client
  7. from libs.helper import RateLimiter
  8. from models.account import Account, TenantAccountJoin, TenantAccountRole
  9. class BillingService:
  10. base_url = os.environ.get("BILLING_API_URL", "BILLING_API_URL")
  11. secret_key = os.environ.get("BILLING_API_SECRET_KEY", "BILLING_API_SECRET_KEY")
  12. compliance_download_rate_limiter = RateLimiter("compliance_download_rate_limiter", 4, 60)
  13. @classmethod
  14. def get_info(cls, tenant_id: str):
  15. params = {"tenant_id": tenant_id}
  16. billing_info = cls._send_request("GET", "/subscription/info", params=params)
  17. return billing_info
  18. @classmethod
  19. def get_knowledge_rate_limit(cls, tenant_id: str):
  20. params = {"tenant_id": tenant_id}
  21. knowledge_rate_limit = cls._send_request("GET", "/subscription/knowledge-rate-limit", params=params)
  22. return {
  23. "limit": knowledge_rate_limit.get("limit", 10),
  24. "subscription_plan": knowledge_rate_limit.get("subscription_plan", "sandbox"),
  25. }
  26. @classmethod
  27. def get_subscription(cls, plan: str, interval: str, prefilled_email: str = "", tenant_id: str = ""):
  28. params = {"plan": plan, "interval": interval, "prefilled_email": prefilled_email, "tenant_id": tenant_id}
  29. return cls._send_request("GET", "/subscription/payment-link", params=params)
  30. @classmethod
  31. def get_model_provider_payment_link(cls, provider_name: str, tenant_id: str, account_id: str, prefilled_email: str):
  32. params = {
  33. "provider_name": provider_name,
  34. "tenant_id": tenant_id,
  35. "account_id": account_id,
  36. "prefilled_email": prefilled_email,
  37. }
  38. return cls._send_request("GET", "/model-provider/payment-link", params=params)
  39. @classmethod
  40. def get_invoices(cls, prefilled_email: str = "", tenant_id: str = ""):
  41. params = {"prefilled_email": prefilled_email, "tenant_id": tenant_id}
  42. return cls._send_request("GET", "/invoices", params=params)
  43. @classmethod
  44. @retry(
  45. wait=wait_fixed(2),
  46. stop=stop_before_delay(10),
  47. retry=retry_if_exception_type(httpx.RequestError),
  48. reraise=True,
  49. )
  50. def _send_request(cls, method: Literal["GET", "POST", "DELETE"], endpoint: str, json=None, params=None):
  51. headers = {"Content-Type": "application/json", "Billing-Api-Secret-Key": cls.secret_key}
  52. url = f"{cls.base_url}{endpoint}"
  53. response = httpx.request(method, url, json=json, params=params, headers=headers)
  54. if method == "GET" and response.status_code != httpx.codes.OK:
  55. raise ValueError("Unable to retrieve billing information. Please try again later or contact support.")
  56. return response.json()
  57. @staticmethod
  58. def is_tenant_owner_or_admin(current_user: Account):
  59. tenant_id = current_user.current_tenant_id
  60. join: TenantAccountJoin | None = (
  61. db.session.query(TenantAccountJoin)
  62. .where(TenantAccountJoin.tenant_id == tenant_id, TenantAccountJoin.account_id == current_user.id)
  63. .first()
  64. )
  65. if not join:
  66. raise ValueError("Tenant account join not found")
  67. if not TenantAccountRole.is_privileged_role(TenantAccountRole(join.role)):
  68. raise ValueError("Only team owner or team admin can perform this action")
  69. @classmethod
  70. def delete_account(cls, account_id: str):
  71. """Delete account."""
  72. params = {"account_id": account_id}
  73. return cls._send_request("DELETE", "/account/", params=params)
  74. @classmethod
  75. def is_email_in_freeze(cls, email: str) -> bool:
  76. params = {"email": email}
  77. try:
  78. response = cls._send_request("GET", "/account/in-freeze", params=params)
  79. return bool(response.get("data", False))
  80. except Exception:
  81. return False
  82. @classmethod
  83. def update_account_deletion_feedback(cls, email: str, feedback: str):
  84. """Update account deletion feedback."""
  85. json = {"email": email, "feedback": feedback}
  86. return cls._send_request("POST", "/account/delete-feedback", json=json)
  87. class EducationIdentity:
  88. verification_rate_limit = RateLimiter(prefix="edu_verification_rate_limit", max_attempts=10, time_window=60)
  89. activation_rate_limit = RateLimiter(prefix="edu_activation_rate_limit", max_attempts=10, time_window=60)
  90. @classmethod
  91. def verify(cls, account_id: str, account_email: str):
  92. if cls.verification_rate_limit.is_rate_limited(account_email):
  93. from controllers.console.error import EducationVerifyLimitError
  94. raise EducationVerifyLimitError()
  95. cls.verification_rate_limit.increment_rate_limit(account_email)
  96. params = {"account_id": account_id}
  97. return BillingService._send_request("GET", "/education/verify", params=params)
  98. @classmethod
  99. def status(cls, account_id: str):
  100. params = {"account_id": account_id}
  101. return BillingService._send_request("GET", "/education/status", params=params)
  102. @classmethod
  103. def activate(cls, account: Account, token: str, institution: str, role: str):
  104. if cls.activation_rate_limit.is_rate_limited(account.email):
  105. from controllers.console.error import EducationActivateLimitError
  106. raise EducationActivateLimitError()
  107. cls.activation_rate_limit.increment_rate_limit(account.email)
  108. params = {"account_id": account.id, "curr_tenant_id": account.current_tenant_id}
  109. json = {
  110. "institution": institution,
  111. "token": token,
  112. "role": role,
  113. }
  114. return BillingService._send_request("POST", "/education/", json=json, params=params)
  115. @classmethod
  116. def autocomplete(cls, keywords: str, page: int = 0, limit: int = 20):
  117. params = {"keywords": keywords, "page": page, "limit": limit}
  118. return BillingService._send_request("GET", "/education/autocomplete", params=params)
  119. @classmethod
  120. def get_compliance_download_link(
  121. cls,
  122. doc_name: str,
  123. account_id: str,
  124. tenant_id: str,
  125. ip: str,
  126. device_info: str,
  127. ):
  128. limiter_key = f"{account_id}:{tenant_id}"
  129. if cls.compliance_download_rate_limiter.is_rate_limited(limiter_key):
  130. from controllers.console.error import ComplianceRateLimitError
  131. raise ComplianceRateLimitError()
  132. json = {
  133. "doc_name": doc_name,
  134. "account_id": account_id,
  135. "tenant_id": tenant_id,
  136. "ip_address": ip,
  137. "device_info": device_info,
  138. }
  139. res = cls._send_request("POST", "/compliance/download", json=json)
  140. cls.compliance_download_rate_limiter.increment_rate_limit(limiter_key)
  141. return res
  142. @classmethod
  143. def clean_billing_info_cache(cls, tenant_id: str):
  144. redis_client.delete(f"tenant:{tenant_id}:billing_info")