Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.1 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)
from products.warehouse_sources.sources.xsolla._config import XsollaSourceConfig
from products.warehouse_sources.sources.xsolla.settings import API_DOCS_URL, AUTH_ERROR, ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.xsolla.xsolla import (
    MERCHANT_ID_ERROR,
    XsollaResumeConfig,
    validate_credentials as validate_xsolla_credentials,
    xsolla_source,
)


@SourceRegistry.register
class XsollaSource(ResumableSource[XsollaSourceConfig, XsollaResumeConfig]):
    codebase_detection = detect(
        env=("^XSOLLA_",),
        script_hosts=("cdn.xsolla.net",),
        npm=("@xsolla/pay-station-sdk",),
        composer=("xsolla/xsolla-sdk-php",),
    )

    lists_tables_without_credentials = True
    # The version is the path segment after /merchant. The endpoints this source reads are on v2.
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = API_DOCS_URL

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.XSOLLA

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            AUTH_ERROR: AUTH_ERROR,
            MERCHANT_ID_ERROR: MERCHANT_ID_ERROR,
            "401 Client Error": AUTH_ERROR,
            "403 Client Error": AUTH_ERROR,
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.xsolla.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: XsollaSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names, merge_only={"transactions"})

    def validate_credentials(
        self,
        config: XsollaSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_xsolla_credentials(config, self.resolve_api_version(api_version))

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[XsollaResumeConfig]:
        return ResumableSourceManager[XsollaResumeConfig](inputs, XsollaResumeConfig)

    def source_for_pipeline(
        self,
        config: XsollaSourceConfig,
        resumable_source_manager: ResumableSourceManager[XsollaResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return xsolla_source(
            config=config,
            endpoint_name=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            manager=resumable_source_manager,
            api_version=self.resolve_api_version(inputs.api_version),
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.XSOLLA,
            category=DataWarehouseSourceCategory.PAYMENTS___BILLING,
            label="Xsolla",
            caption=(
                "Connect your Xsolla merchant account. Find the merchant ID in "
                "[Publisher Account](https://publisher.xsolla.com/) under **Company settings > Company**. "
                "Create the API key under **Company settings > API keys**. "
                "Use a company API key, because a project API key cannot read merchant reports. "
                "Incremental transaction syncs read the last seven days again to collect status changes."
            ),
            docsUrl="https://posthog.com/docs/cdp/sources/xsolla",
            iconPath="/static/services/xsolla.png",
            releaseStatus=ReleaseStatus.BETA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="merchant_id",
                        label="Merchant ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="123456",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )