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.2 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.paystack._config import PaystackSourceConfig
from products.warehouse_sources.sources.paystack.paystack import (
    PaystackResumeConfig,
    paystack_source,
    validate_credentials as validate_paystack_credentials,
)
from products.warehouse_sources.sources.paystack.settings import ENDPOINTS, PAYSTACK_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class PaystackSource(ResumableSource[PaystackSourceConfig, PaystackResumeConfig]):
    codebase_detection = detect(
        env=("^PAYSTACK_",),
        script_hosts=("js.paystack.co",),
        npm=("@paystack/inline-js", "@paystack/paystack-sdk"),
        pypi=("paystackapi", "paystack-sdk"),
        cocoapods=("Paystack", "PaystackCheckout"),
        composer=("yabacon/paystack-php",),
        maven=("co.paystack.android:paystack",),
        rubygems=("paystack-ruby",),
    )

    api_docs_url = "https://paystack.com/docs/api/"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.paystack.co": "Paystack authentication failed. Please check that your secret API key is valid.",
            "403 Client Error: Forbidden for url: https://api.paystack.co": "Paystack denied access. Please check that your secret API key has the required permissions.",
        }

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

        return CANONICAL_DESCRIPTIONS

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.PAYSTACK,
            category=DataWarehouseSourceCategory.PAYMENTS___BILLING,
            label="Paystack",
            caption="""Enter your Paystack secret API key to pull your Paystack data into the PostHog Data warehouse.

You can find your secret key (it starts with `sk_live_` or `sk_test_`) under **Settings → API Keys & Webhooks** in your [Paystack dashboard](https://dashboard.paystack.com/#/settings/developers). The key has read access to your integration's data.""",
            iconPath="/static/services/paystack.png",
            docsUrl="https://posthog.com/docs/cdp/sources/paystack",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="secret_api_key",
                        label="Secret API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="sk_live_...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_schemas(
        self,
        config: PaystackSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=[],
            )
            for endpoint in ENDPOINTS
        ]

        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]

        return schemas

    def validate_credentials(
        self,
        config: PaystackSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_paystack_credentials(config.secret_api_key):
            return True, None

        return False, "Invalid Paystack secret API key"

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

    def source_for_pipeline(
        self,
        config: PaystackSourceConfig,
        resumable_source_manager: ResumableSourceManager[PaystackResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        resource = paystack_source(
            secret_api_key=config.secret_api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )
        return SourceResponse(
            name=resource.name,
            items=lambda: resource,
            primary_keys=PAYSTACK_ENDPOINTS[inputs.schema_name].primary_keys,
            column_hints=resource.column_hints,
        )