billing_service.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. from typing import Optional
  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 models.account import TenantAccountJoin, TenantAccountRole
  7. class BillingService:
  8. base_url = os.environ.get("BILLING_API_URL", "BILLING_API_URL")
  9. secret_key = os.environ.get("BILLING_API_SECRET_KEY", "BILLING_API_SECRET_KEY")
  10. @classmethod
  11. def get_info(cls, tenant_id: str):
  12. params = {"tenant_id": tenant_id}
  13. billing_info = cls._send_request("GET", "/subscription/info", params=params)
  14. return billing_info
  15. @classmethod
  16. def get_subscription(cls, plan: str, interval: str, prefilled_email: str = "", tenant_id: str = ""):
  17. params = {"plan": plan, "interval": interval, "prefilled_email": prefilled_email, "tenant_id": tenant_id}
  18. return cls._send_request("GET", "/subscription/payment-link", params=params)
  19. @classmethod
  20. def get_model_provider_payment_link(cls, provider_name: str, tenant_id: str, account_id: str, prefilled_email: str):
  21. params = {
  22. "provider_name": provider_name,
  23. "tenant_id": tenant_id,
  24. "account_id": account_id,
  25. "prefilled_email": prefilled_email,
  26. }
  27. return cls._send_request("GET", "/model-provider/payment-link", params=params)
  28. @classmethod
  29. def get_invoices(cls, prefilled_email: str = "", tenant_id: str = ""):
  30. params = {"prefilled_email": prefilled_email, "tenant_id": tenant_id}
  31. return cls._send_request("GET", "/invoices", params=params)
  32. @classmethod
  33. @retry(
  34. wait=wait_fixed(2),
  35. stop=stop_before_delay(10),
  36. retry=retry_if_exception_type(httpx.RequestError),
  37. reraise=True,
  38. )
  39. def _send_request(cls, method, endpoint, json=None, params=None):
  40. headers = {"Content-Type": "application/json", "Billing-Api-Secret-Key": cls.secret_key}
  41. url = f"{cls.base_url}{endpoint}"
  42. response = httpx.request(method, url, json=json, params=params, headers=headers)
  43. return response.json()
  44. @staticmethod
  45. def is_tenant_owner_or_admin(current_user):
  46. tenant_id = current_user.current_tenant_id
  47. join: Optional[TenantAccountJoin] = (
  48. db.session.query(TenantAccountJoin)
  49. .filter(TenantAccountJoin.tenant_id == tenant_id, TenantAccountJoin.account_id == current_user.id)
  50. .first()
  51. )
  52. if not join:
  53. raise ValueError("Tenant account join not found")
  54. if not TenantAccountRole.is_privileged_role(join.role):
  55. raise ValueError("Only team owner or team admin can perform this action")