Repository: All-Hands-AI/OpenHands
File: enterprise/server/routes/billing.py (Python)

Code before:
    SUBSCRIPTION_PRICE_DATA,
    get_default_litellm_model,
)
from server.logger import logger
from storage.billing_session import BillingSession
from storage.database import session_maker
from storage.subscription_access import SubscriptionAccess
from storage.user_settings import UserSettings

from openhands.server.user_auth import get_user_id

stripe.api_key = STRIPE_API_KEY
billing_router = APIRouter(prefix='/api/billing')


def is_all_hands_saas_environment(request: Request) -> bool:
    """Check if the current domain is an All Hands SaaS environment.

    Args:
        request: FastAPI Request object

    Returns:
        True if the current domain contains "all-hands.dev" or "openhands.dev" postfix
    """
    hostname = request.url.hostname or ""
    return hostname.endswith("all-hands.dev") or hostname.endswith("openhands.dev")


def validate_saas_environment(request: Request) -> None:
    """Validate that the request is coming from an All Hands SaaS environment.

    Args:
        request: FastAPI Request object

    Raises:
        HTTPException: If the request is not from an All Hands SaaS environment
    """
    if not is_all_hands_saas_environment(request):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Checkout sessions are only available for All Hands SaaS environments',
        )


class BillingSessionType(Enum):
    DIRECT_PAYMENT = 'DIRECT_PAYMENT'
    MONTHLY_SUBSCRIPTION = 'MONTHLY_SUBSCRIPTION'


class GetCreditsResponse(BaseModel):
    credits: Decimal | None = None

Review comment (suggestion):
I think there is a flag you can use to figure this out:
```
server_config = load_server_config()

...

  if server_config.app_mode == AppMode.SAAS:
   ...
```

Code after:
    SUBSCRIPTION_PRICE_DATA,
    get_default_litellm_model,
)
from server.logger import logger
from storage.billing_session import BillingSession
from storage.database import session_maker
from storage.subscription_access import SubscriptionAccess
from storage.user_settings import UserSettings

from openhands.server.user_auth import get_user_id

stripe.api_key = STRIPE_API_KEY
billing_router = APIRouter(prefix='/api/billing')


# TODO: Add a new app_mode named "ON_PREM" to support self-hosted customers instead of doing this
# and members should comment out the "validate_saas_environment" function if they are developing and testing locally.
def is_all_hands_saas_environment(request: Request) -> bool:
    """Check if the current domain is an All Hands SaaS environment.

    Args:
        request: FastAPI Request object

    Returns:
        True if the current domain contains "all-hands.dev" or "openhands.dev" postfix
    """
    hostname = request.url.hostname or ''
    return hostname.endswith('all-hands.dev') or hostname.endswith('openhands.dev')


def validate_saas_environment(request: Request) -> None:
    """Validate that the request is coming from an All Hands SaaS environment.

    Args:
        request: FastAPI Request object

    Raises:
        HTTPException: If the request is not from an All Hands SaaS environment
    """
    if not is_all_hands_saas_environment(request):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Checkout sessions are only available for All Hands SaaS environments',
        )


class BillingSessionType(Enum):
    DIRECT_PAYMENT = 'DIRECT_PAYMENT'
    MONTHLY_SUBSCRIPTION = 'MONTHLY_SUBSCRIPTION'