# 🧾 Billing Orchestrator


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L26"
target="_blank" style="float:right; font-size:smaller">source</a>

### SubscriptionStatus

``` python

def SubscriptionStatus(
    args:VAR_POSITIONAL, kwds:VAR_KEYWORD
):

```

*Canonical subscription status values.*

Every subscription in the system maps to one of these statuses. Use
[`SubscriptionStatus.normalize()`](https://abhisheksreesaila.github.io/fh-saas/utils_billing.html#subscriptionstatus.normalize)
to map raw Stripe/legacy values.

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L66"
target="_blank" style="float:right; font-size:smaller">source</a>

### BillingConfig

``` python

def BillingConfig(
    stripe_secret_key:str='', stripe_webhook_secret:str=None, monthly_price_id:str=None, yearly_price_id:str=None,
    trial_days:int=30, grace_period_days:int=3, base_url:str='http://localhost:5001', pricing_path:str='/pricing',
    checkout_path:str='/checkout/{plan_type}', success_path:str='/payment-success',
    cancel_path:str='/settings/payment', webhook_path:str='/stripe/webhook', portal_path:str='/billing-portal',
    pricing_mode:str='public_and_post_login_pricing', allow_promotions:bool=True, is_development:bool=False
)->None:

```

*Configuration for the billing orchestrator.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L128"
target="_blank" style="float:right; font-size:smaller">source</a>

### validate_billing_config

``` python

def validate_billing_config(
    config:BillingConfig
)->List[str]:

```

*Validate billing configuration and return actionable error messages.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L156"
target="_blank" style="float:right; font-size:smaller">source</a>

### AccessDecision

``` python

def AccessDecision(
    allowed:bool, reason:SubscriptionStatus, redirect_to:Optional[str]=None
)->None:

```

*Result of a subscription access check.*

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L164"
target="_blank" style="float:right; font-size:smaller">source</a>

### PlanStore

``` python

def PlanStore(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

```

*Interface for accessing pricing plan data.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L171"
target="_blank" style="float:right; font-size:smaller">source</a>

### SubscriptionStore

``` python

def SubscriptionStore(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

```

*Interface for subscription persistence and webhook idempotency.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L181"
target="_blank" style="float:right; font-size:smaller">source</a>

### UserContextProvider

``` python

def UserContextProvider(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

```

*Interface for extracting user context from HTTP requests.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L189"
target="_blank" style="float:right; font-size:smaller">source</a>

### BillingUIAdapter

``` python

def BillingUIAdapter(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

```

*Optional UI rendering callbacks for billing routes (headless-first).*

### Default Implementations

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L196"
target="_blank" style="float:right; font-size:smaller">source</a>

### HostDBPlanStore

``` python

def HostDBPlanStore(
    host_db
):

```

*Default PlanStore backed by HostDatabase.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L217"
target="_blank" style="float:right; font-size:smaller">source</a>

### HostDBSubscriptionStore

``` python

def HostDBSubscriptionStore(
    host_db
):

```

*Default SubscriptionStore backed by HostDatabase.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L297"
target="_blank" style="float:right; font-size:smaller">source</a>

### RequestUserContext

``` python

def RequestUserContext(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

```

*Default UserContextProvider reading from request.state.user.*

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L313"
target="_blank" style="float:right; font-size:smaller">source</a>

### init_trial_on_first_login

``` python

def init_trial_on_first_login(
    tenant_id:str, user_email:str, subscription_store:SubscriptionStore, config:BillingConfig
):

```

*Create a trial subscription if the tenant has no subscription yet and
return the resolved subscription.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L340"
target="_blank" style="float:right; font-size:smaller">source</a>

### resolve_subscription_access

``` python

def resolve_subscription_access(
    subscription, config:BillingConfig, now:datetime=None
)->AccessDecision:

```

*Determine whether a user should be allowed access based on subscription
state. Pure function.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L377"
target="_blank" style="float:right; font-size:smaller">source</a>

### resolve_checkout_return

``` python

def resolve_checkout_return(
    session_id:str, tenant_id:str, subscription_store:SubscriptionStore, subscription:NoneType=None
)->SubscriptionStatus:

```

*Determine subscription state after a checkout redirect.*

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L426"
target="_blank" style="float:right; font-size:smaller">source</a>

### register_billing_routes

``` python

def register_billing_routes(
    app, config:BillingConfig, plan_store:PlanStore=None, subscription_store:SubscriptionStore=None,
    user_ctx:UserContextProvider=None, stripe_service:NoneType=None, ui_adapter:BillingUIAdapter=None,
    host_db:NoneType=None
):

```

*Wire all billing routes onto a FastHTML app in one call.*

------------------------------------------------------------------------

<a
href="https://github.com/abhisheksreesaila/fh-saas/blob/main/fh_saas/utils_billing.py#L563"
target="_blank" style="float:right; font-size:smaller">source</a>

### attach_billing_auth_hooks

``` python

def attach_billing_auth_hooks(
    app, config:BillingConfig, subscription_store:SubscriptionStore=None, user_ctx:UserContextProvider=None,
    host_db:NoneType=None, skip_paths:List[str]=None
):

```

*Attach first-login trial init and paywall guard as app middleware.*
