Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.6 KB
Raw Blame
"""Application service for Console Billing portal links."""

from collections.abc import Callable
from typing import Protocol

from machinery.context import RequestContext
from services.account_errors import AccountNotFoundError
from services.account_ports import AccountRepository


class BillingPortalLink(Protocol):
    @property
    def url(self) -> str: ...


class BillingPortalService:
    def __init__(
        self,
        *,
        accounts: AccountRepository,
        get_subscription: Callable[[str, str, str, str], BillingPortalLink],
        get_invoices: Callable[[str, str], BillingPortalLink],
    ) -> None:
        self._accounts = accounts
        self._get_subscription = get_subscription
        self._get_invoices = get_invoices

    def get_subscription(
        self,
        context: RequestContext,
        *,
        plan: str,
        interval: str,
    ) -> BillingPortalLink:
        email, workspace_id = self._resolve_account_email_and_workspace_id(context)
        return self._get_subscription(plan, interval, email, workspace_id)

    def get_invoices(self, context: RequestContext) -> BillingPortalLink:
        email, workspace_id = self._resolve_account_email_and_workspace_id(context)
        return self._get_invoices(email, workspace_id)

    def _resolve_account_email_and_workspace_id(self, context: RequestContext) -> tuple[str, str]:
        workspace_id = context.active_workspace_id

        account = self._accounts.get(context.account_id)
        if account is None:
            raise AccountNotFoundError
        return account.email, workspace_id