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.

6.1 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.gocardless._config import GoCardlessSourceConfig
from products.warehouse_sources.sources.gocardless.gocardless import (
    GoCardlessResumeConfig,
    gocardless_source,
    validate_credentials as validate_gocardless_credentials,
)
from products.warehouse_sources.sources.gocardless.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceFieldSelectConfig,
    SourceFieldSelectConfigOption,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class GoCardlessSource(ResumableSource[GoCardlessSourceConfig, GoCardlessResumeConfig]):
    codebase_detection = detect(
        env=("^GOCARDLESS_",),
        script_hosts=("pay.gocardless.com",),
        npm=("gocardless-nodejs", "@gocardless/react-dropin"),
        pypi=("gocardless-pro",),
        composer=("gocardless/gocardless-pro", "gocardless/gocardless", "gocardless/gocardless-pro-php"),
        go=("github.com/gocardless/gocardless-pro-go",),
        maven=("com.gocardless:gocardless-pro", "com.gocardless:gocardless-java", "com.gocardless:gocardlesssdk"),
        rubygems=("gocardless_pro", "gocardless"),
    )

    supported_versions = ("2015-07-06",)
    default_version = "2015-07-06"
    api_docs_url = "https://developer.gocardless.com/api-reference"

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

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

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.gocardless.com": "GoCardless authentication failed. Please check your access token.",
            "401 Client Error: Unauthorized for url: https://api-sandbox.gocardless.com": "GoCardless authentication failed. Please check your access token (and that it matches the selected environment).",
            "403 Client Error: Forbidden for url: https://api.gocardless.com": "GoCardless denied access. Please check your access token's permissions.",
            "403 Client Error: Forbidden for url: https://api-sandbox.gocardless.com": "GoCardless denied access. Please check your access token's permissions.",
        }

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.GOCARDLESS,
            category=DataWarehouseSourceCategory.PAYMENTS___BILLING,
            label="GoCardless",
            caption="""Enter your GoCardless access token to pull your GoCardless payments data into the PostHog Data warehouse.

Create a read-only access token in the [GoCardless dashboard](https://manage.gocardless.com/developers) under Developers > Create > Access token. Sandbox and live environments use separate hosts and tokens — make sure the environment matches your token.""",
            iconPath="/static/services/gocardless.png",
            docsUrl="https://posthog.com/docs/cdp/sources/gocardless",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldSelectConfig(
                        name="environment",
                        label="Environment",
                        required=True,
                        defaultValue="live",
                        options=[
                            SourceFieldSelectConfigOption(label="Live", value="live"),
                            SourceFieldSelectConfigOption(label="Sandbox", value="sandbox"),
                        ],
                    ),
                    SourceFieldInputConfig(
                        name="access_token",
                        label="Access token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_schemas(
        self,
        config: GoCardlessSourceConfig,
        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)

    def validate_credentials(
        self,
        config: GoCardlessSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_gocardless_credentials(config.environment, config.access_token):
            return True, None

        return False, "Invalid GoCardless access token"

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

    def source_for_pipeline(
        self,
        config: GoCardlessSourceConfig,
        resumable_source_manager: ResumableSourceManager[GoCardlessResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return gocardless_source(
            environment=config.environment,
            access_token=config.access_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )