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"
)
# 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"
)
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,
timeout: int = 30
)
Parameters:
api_key(required) - Your merchant API keytimeout(optional) - Request timeout in seconds
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 rublesexpires_in- Payment lifetime in secondscomment- 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_atasdatetimeobjectcreated_at_datetime-created_atasdatetimeobjectpaid_at_datetime-paid_atasdatetimeobject
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 exceptionUnepayAPIError- General API errorUnepayAuthError- 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")
# 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")
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unepay_sdk-1.0.0.tar.gz.
File metadata
- Download URL: unepay_sdk-1.0.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b61e636d43c980cbceaa81b71a32830b7b5b4f766e02b55d74986a907a867276
|
|
| MD5 |
8990c711de3a217c64990261d833fc1b
|
|
| BLAKE2b-256 |
2d0351e17eaf69d4ba1f4da3dc223875ebc6c57c5f2044e1c4cb8acb82afeddf
|
File details
Details for the file unepay_sdk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: unepay_sdk-1.0.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0788cf870ce1d1e83e78230d668c3be0864324e242bc7b88fa29aa6c5aee3cab
|
|
| MD5 |
d873081ebf711f63141d7731c214430f
|
|
| BLAKE2b-256 |
eb9fba57aaa579cc64933563bbd168ed2fbed79a2d63b52a500646795124fef7
|