Skip to main content

Official Sunbay Nexus Python SDK

Project description

Sunbay Nexus Python SDK

Official Python SDK for the Sunbay Nexus payment platform.

This SDK provides a simple and professional way to integrate with Sunbay Nexus payment platform from Python applications, with full support for all payment operations.

Features

  • Simple and intuitive API
  • Thread-safe client with connection pooling
  • Clear separation between network errors and business errors
  • Automatic authentication via API key
  • Configurable timeouts and retries for GET requests
  • Python 3.7+ support

Installation

Once published to PyPI:

pip install sunbay-nexus-sdk

Supported Python versions

  • Officially supported: Python 3.7 and above
  • Python 2 is not supported.

Quick Start

1. Initialize client

from sunbay_nexus_sdk import NexusClient

# Option 1: pass api_key explicitly
client = NexusClient(api_key="sk_test_xxx")

# Option 2: read api_key from environment variable SUNBAY_API_KEY
# client = NexusClient()

The NexusClient is thread-safe and can be reused across multiple threads. Create it once and reuse it in your application.

2. Sale transaction

Important: All amount fields are in the smallest currency unit (e.g., cents for USD, fen for CNY). For example, 100.00 USD should be passed as 10000 (cents).

from sunbay_nexus_sdk import NexusClient, SunbayBusinessError, SunbayNetworkError
from sunbay_nexus_sdk.models.common import SaleAmount
from sunbay_nexus_sdk.models.request import SaleRequest

client = NexusClient(api_key="sk_test_xxx")

# 100.00 USD = 10000 cents
amount = SaleAmount(order_amount=10000, price_currency="USD")

request = SaleRequest(
    app_id="app_123456",
    merchant_id="mch_789012",
    reference_order_id="ORDER20231119001",
    transaction_request_id="PAY_REQ_1234567890",
    amount=amount,
    description="Product purchase",
    terminal_sn="T1234567890",
)

try:
    # If we reach here, code == "0" (success), no need to check is_success()
    response = client.sale(request)
    print("Transaction ID:", response.transaction_id)
except SunbayNetworkError as e:
    print("Network Error:", e)
except SunbayBusinessError as e:
    print("API Error:", e.code, "-", e)

3. Query transaction

from sunbay_nexus_sdk import NexusClient
from sunbay_nexus_sdk.models.request import QueryRequest

client = NexusClient(api_key="sk_test_xxx")

request = QueryRequest(
    app_id="app_123456",
    merchant_id="mch_789012",
    transaction_id="TXN20231119001",
)

try:
    # If we reach here, code == "0" (success), no need to check is_success()
    response = client.query(request)
    print("Status:", response.transaction_status)
except SunbayBusinessError as e:
    print("API Error:", e.code, "-", e)
except SunbayNetworkError as e:
    print("Network Error:", e)

API Overview

The SDK provides a NexusClient with comprehensive payment APIs:

  • Transaction APIs:
    • sale(request: SaleRequest) -> SaleResponse
    • auth(request: AuthRequest) -> AuthResponse
    • forced_auth(request: ForcedAuthRequest) -> ForcedAuthResponse
    • incremental_auth(request: IncrementalAuthRequest) -> IncrementalAuthResponse
    • post_auth(request: PostAuthRequest) -> PostAuthResponse
    • refund(request: RefundRequest) -> RefundResponse
    • void_transaction(request: VoidRequest) -> VoidResponse
    • abort(request: AbortRequest) -> AbortResponse
    • tip_adjust(request: TipAdjustRequest) -> TipAdjustResponse
  • Query APIs:
    • query(request: QueryRequest) -> QueryResponse
  • Settlement APIs:
    • batch_query(request: BatchQueryRequest) -> BatchQueryResponse
    • batch_close(request: BatchCloseRequest) -> BatchCloseResponse

Exceptions

The SDK differentiates between network-level and business-level errors:

  • SunbayNetworkError
    • Thrown for network errors, timeouts, or HTTP non-2xx responses.
    • Has a retryable flag to indicate whether the request may be retried safely.
  • SunbayBusinessError
    • Thrown for API business errors (e.g. code != "0") and local parameter validation failures.
    • Contains code and trace_id fields when available.

Always catch SunbayNetworkError before SunbayBusinessError if you need to distinguish between them.

Configuration

You can configure the client using constructor arguments:

from sunbay_nexus_sdk import NexusClient

client = NexusClient(
    api_key="sk_test_xxx",
    base_url="https://open.sunbay.us",   # default
    connect_timeout=30.0,                # seconds, default 30.0
    read_timeout=60.0,                   # seconds, default 60.0
    max_retries=3,                       # default 3 for GET requests
    max_connections=200,                 # default 200
    # Optional: custom logger instance
    # logger=my_logger,
)

In addition, the SDK uses the standard Python logging library:

  • By default it logs HTTP requests/responses and errors to the logger named sunbay_nexus_sdk.http.
  • The SDK does not configure handlers or logging levels itself — you are free to integrate with any logging stack (standard logging, loguru, structlog, etc.) by configuring or adapting a logging.Logger.
  • For advanced use cases, you can pass a custom logger via the NexusClient(logger=...) constructor parameter; this logger will be used by the underlying HTTP client for all log output.

Using enums

For some fields (such as transaction status and card network type), the SDK provides enums to make the code more self-documenting:

from sunbay_nexus_sdk import TransactionStatus

# In try-catch block, if we reach here, code == "0" (success)
# So we only need to check transaction_status
if response.transaction_status == TransactionStatus.SUCCESS:
    print("Transaction succeeded")

Integration in web frameworks

In web frameworks (such as FastAPI or Django), it is recommended to create a single NexusClient instance at startup and reuse it:

from sunbay_nexus_sdk import NexusClient

client = NexusClient(api_key="sk_live_xxx")

def process_payment(request_data):
    # build SaleRequest here...
    response = client.sale(request_data)
    ...

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sunbay_nexus_sdk-1.0.9.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sunbay_nexus_sdk-1.0.9-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file sunbay_nexus_sdk-1.0.9.tar.gz.

File metadata

  • Download URL: sunbay_nexus_sdk-1.0.9.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for sunbay_nexus_sdk-1.0.9.tar.gz
Algorithm Hash digest
SHA256 3ca6975a8a7da3451aee304947e5cad5dbfb961c9f82e3f613ab0d3d7188d689
MD5 a3d76a9b9dded826048b6b3fff514861
BLAKE2b-256 6217a94e62cf844c6578b9cf68826a9c115dbfdbdadb4889df5f4a32fe783e83

See more details on using hashes here.

File details

Details for the file sunbay_nexus_sdk-1.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for sunbay_nexus_sdk-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5f4a6a0e97fc41030a85343c6aad538c7be69be0a5d2e6e0ccd28ea649acfe72
MD5 eeda2f9223e4b1b0bd90ff0044615c10
BLAKE2b-256 c452b8e75c163d22764d266a63288c2e9ff4ce7acdfb492d6c1d85e45f32bd64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page