Skip to main content

unepay sdk for python

Project description

Unepay Python SDK

Python SDK for working with Unepay API. Easy way to create payments, check their status and manage them through Python.

Installation

pip install unepay-sdk

Or from source:

pip install .

Quick Start

Simple payment creation example

from unepay_lib import UnepayClient

# Initialize client
client = UnepayClient(
    api_key="unepay_your_api_key_here",
    base_url="https://unepay.lol"
)

# Create payment
payment = client.create_payment(
    payment_id="order_12345",      # Your internal order ID
    amount=1500.0,                  # Amount in rubles
    expires_in=600,                 # Lifetime in seconds (10 minutes)
    comment="Payment for order #12345"  # Comment (optional)
)

print(f"Payment URL: {payment.payment_url}")
print(f"Payment ID: {payment.id}")
print(f"External ID: {payment.external_id}")  # Your order ID

# Check payment status
status = client.get_payment_status(payment.id)
print(f"Status: {status.status}")
print(f"Is expired: {status.is_expired}")

# Get list of active payments
active_payments = client.list_active_payments()
for p in active_payments:
    print(f"{p.external_id}: {p.amount} RUB - {p.status}")

Full example with error handling

from unepay_lib import UnepayClient, UnepayAuthError, UnepayAPIError

client = UnepayClient(
    api_key="your_api_key",
    base_url="https://unepay.lol"
)

try:
    # Create payment
    payment = client.create_payment(
        payment_id="order_12345",
        amount=1500.0,
        expires_in=600,
        comment="Payment for order"
    )
    
    print(f"✓ Payment created!")
    print(f"  URL: {payment.payment_url}")
    print(f"  ID: {payment.id}")
    
    # Track status
    import time
    while True:
        status = client.get_payment_status(payment.id)
        if status.status == "paid":
            print("✓ Payment completed!")
            break
        elif status.is_expired:
            print("⚠ Payment expired")
            break
        print(f"Status: {status.status}, waiting...")
        time.sleep(5)
        
except UnepayAuthError as e:
    print(f"Authentication error: {e}")
except UnepayAPIError as e:
    print(f"API error: {e}")

API Documentation

Client initialization

client = UnepayClient(
    api_key: str,
    base_url: str,
    timeout: int = 30
)

Parameters:

Create payment

payment = client.create_payment(
    payment_id: str,
    amount: float,
    expires_in: int,
    comment: Optional[str] = None
) -> Payment

Parameters:

  • payment_id - Unique payment ID (your internal identifier)
  • amount - Payment amount in rubles
  • expires_in - Payment lifetime in seconds
  • comment - Payment comment (optional)

Returns: Payment object with created payment data

Example:

payment = client.create_payment(
    payment_id="order_123",
    amount=2500.50,
    expires_in=1800,  # 30 minutes
    comment="Payment for product"
)

Get payment status

payment = client.get_payment_status(payment_id: str) -> Payment

Parameters:

  • payment_id - Payment ID (public_id, returned when created)

Returns: Payment object with current status

Example:

payment = client.get_payment_status("n5AvkX5BhasxnL00EnhIVA9kZeXKquTb")
print(f"Status: {payment.status}")
print(f"Amount: {payment.amount} RUB")
print(f"Is expired: {payment.is_expired}")

Get full payment information

payment = client.get_payment(payment_id: str) -> Payment

Parameters:

  • payment_id - Payment ID (public_id)

Returns: Payment object with full information

List active payments

payments = client.list_active_payments() -> List[Payment]

Returns: List of Payment objects with all active payments for the merchant

Example:

payments = client.list_active_payments()
for payment in payments:
    print(f"ID: {payment.external_id}")
    print(f"Amount: {payment.amount} RUB")
    print(f"Status: {payment.status}")
    print(f"URL: {payment.payment_url}")
    print("---")

Payment Model

The Payment object contains the following information:

@dataclass
class Payment:
    id: str                    # Public ID (used in URL)
    external_id: str           # Your internal ID
    amount: float              # Payment amount
    comment: str               # Comment
    status: str                # Status: pending, paid, expired, cancelled
    expires_at: int            # Expiration timestamp (milliseconds)
    created_at: int            # Creation timestamp (milliseconds)
    paid_at: int               # Payment timestamp (milliseconds)
    payment_url: str           # Payment page URL
    is_expired: bool           # Expiration flag

Useful properties:

  • expires_at_datetime - expires_at as datetime object
  • created_at_datetime - created_at as datetime object
  • paid_at_datetime - paid_at as datetime object

Example:

payment = client.get_payment_status(payment_id)
if payment.expires_at_datetime:
    print(f"Expires at: {payment.expires_at_datetime.strftime('%Y-%m-%d %H:%M:%S')}")

Error Handling

The library uses the following exceptions:

  • UnepayException - Base exception
  • UnepayAPIError - General API error
  • UnepayAuthError - Authentication error (invalid API key)
  • UnepayNotFoundError - Resource not found (404)
  • UnepayValidationError - Data validation error (400)

Error handling example:

from unepay_lib import UnepayClient, UnepayAuthError, UnepayAPIError

try:
    client = UnepayClient(api_key="invalid_key")
    payment = client.create_payment(...)
except UnepayAuthError as e:
    print(f"Authentication error: {e}")
except UnepayAPIError as e:
    print(f"API error: {e}")
    print(f"Status code: {e.status_code}")

Usage Examples

Create and track payment

from unepay_lib import UnepayClient
import time

client = UnepayClient(api_key="your_api_key", base_url="https://unepay.lol")

# Create payment
payment = client.create_payment(
    payment_id="order_123",
    amount=1000.0,
    expires_in=600,
    comment="Payment for order"
)

print(f"Payment created: {payment.payment_url}")

# Track status
while True:
    status = client.get_payment_status(payment.id)
    
    if status.status == "paid":
        print("Payment completed!")
        break
    elif status.status == "expired" or status.is_expired:
        print("Payment expired")
        break
    
    print(f"Status: {status.status}, waiting...")
    time.sleep(5)  # Check every 5 seconds

Get payment statistics

from unepay_lib import UnepayClient
from collections import Counter

client = UnepayClient(api_key="your_api_key", base_url="https://unepay.lol")
payments = client.list_active_payments()

# Statistics by status
statuses = Counter(p.status for p in payments)
print("Payment statuses:", dict(statuses))

# Total amount of active payments
total_amount = sum(p.amount for p in payments if p.status == "pending")
print(f"Total pending amount: {total_amount} RUB")

# Number of paid payments
paid_count = sum(1 for p in payments if p.status == "paid")
print(f"Paid payments: {paid_count}")

License

MIT

Support

If you have questions or issues, create an issue in the project repository.

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

unepay_sdk-1.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

unepay_sdk-1.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file unepay_sdk-1.0.1.tar.gz.

File metadata

  • Download URL: unepay_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for unepay_sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 929d9a444075618c0193c585bc5edb4ce2a5accb3d922592a44f69a0fda6e948
MD5 f4390942fd96dc82c11407410df3da65
BLAKE2b-256 eefc45ba4c3f5bb1779d28ae37596683b862620eda2c8f7b4a64df2d9184804e

See more details on using hashes here.

File details

Details for the file unepay_sdk-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: unepay_sdk-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for unepay_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a0cbb1cacaab18596bd5daae5e9ea5656f1b32b151657238d4407c674c139ad
MD5 983eaad3320328468e06538c7202694a
BLAKE2b-256 58a8e4f632c1cc9d44cc38dc289818f341db05e29d9c1d1de5fd35d089ddf165

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