A modern Python SDK for the ZenoPay payment API
Project description
ZenoPay Python SDK
Modern Python SDK for ZenoPay payment API with async/sync support and webhook handling.
Installation
pip install zenopay-sdk
Quick Start
from elusion.zenopay import ZenoPay
from elusion.zenopay.models.order import NewOrder
# Initialize client
client = ZenoPay(account_id="your_account_id")
# Create order (sync)
with client:
order = NewOrder(
buyer_email="customer@example.com",
buyer_name="John Doe",
buyer_phone="0700000000",
amount=1000
)
response = client.orders.sync.create(order)
print(f"Order ID: {response.data.order_id}")
Configuration
Environment Variables
export ZENOPAY_ACCOUNT_ID="your_account_id"
export ZENOPAY_API_KEY="your_api_key" # Optional
export ZENOPAY_SECRET_KEY="your_secret_key" # Optional
Code Configuration
client = ZenoPay(
account_id="your_account_id",
api_key="your_api_key",
secret_key="your_secret_key",
timeout=30.0
)
API Usage
Synchronous Operations
# Create order
with client:
order_data = {
"buyer_email": "test@example.com",
"buyer_name": "Test User",
"buyer_phone": "0700000000",
"amount": 5000,
"webhook_url": "https://yoursite.com/webhook"
}
response = client.orders.sync.create(order_data)
order_id = response.data.order_id
# Check status
with client:
status = client.orders.sync.get_status(order_id)
print(f"Payment status: {status.data.payment_status}")
# Check if paid
with client:
is_paid = client.orders.sync.check_payment(order_id)
print(f"Paid: {is_paid}")
# Wait for payment
with client:
try:
completed = client.orders.sync.wait_for_payment(order_id, timeout=300)
print("Payment completed!")
except TimeoutError:
print("Payment timeout")
Asynchronous Operations
import asyncio
async def create_payment():
async with client:
order_data = {
"buyer_email": "test@example.com",
"buyer_name": "Test User",
"buyer_phone": "0700000000",
"amount": 5000
}
# Create order
response = await client.orders.create(order_data)
order_id = response.data.order_id
# Check status
status = await client.orders.get_status(order_id)
print(f"Status: {status.data.payment_status}")
# Wait for completion
try:
completed = await client.orders.wait_for_payment(order_id)
print("Payment completed!")
except TimeoutError:
print("Payment timeout")
asyncio.run(create_payment())
Webhook Handling
Basic Setup
# Setup handlers
def payment_completed(event):
order_id = event.payload.order_id
reference = event.payload.reference
print(f"Payment completed: {order_id} - {reference}")
def payment_failed(event):
order_id = event.payload.order_id
print(f"Payment failed: {order_id}")
# Register handlers
client.webhooks.on_payment_completed(payment_completed)
client.webhooks.on_payment_failed(payment_failed)
# Process webhook
webhook_data = '{"order_id":"123","payment_status":"COMPLETED","reference":"REF123"}'
response = client.webhooks.process_webhook_request(webhook_data)
Flask Integration
from flask import Flask, request, jsonify
app = Flask(__name__)
client = ZenoPay(account_id="your_account_id")
def handle_completed_payment(event):
order_id = event.payload.order_id
# Update database, send emails, etc.
print(f"Order {order_id} completed")
client.webhooks.on_payment_completed(handle_completed_payment)
@app.route('/zenopay/webhook', methods=['POST'])
def webhook():
raw_data = request.data.decode('utf-8')
response = client.webhooks.process_webhook_request(raw_data)
return jsonify({'status': response.status})
if __name__ == '__main__':
app.run()
FastAPI Integration
from fastapi import FastAPI, Request
app = FastAPI()
client = ZenoPay(account_id="your_account_id")
def handle_completed_payment(event):
order_id = event.payload.order_id
print(f"Order {order_id} completed")
client.webhooks.on_payment_completed(handle_completed_payment)
@app.post("/zenopay/webhook")
async def webhook(request: Request):
raw_data = await request.body()
raw_data_str = raw_data.decode('utf-8')
response = client.webhooks.process_webhook_request(raw_data_str)
return {'status': response.status}
Error Handling
from elusion.zenopay.exceptions import (
ZenoPayError,
ZenoPayValidationError,
ZenoPayNetworkError
)
try:
with client:
response = client.orders.sync.create(order_data)
except ZenoPayValidationError as e:
print(f"Validation error: {e.message}")
print(f"Details: {e.validation_errors}")
except ZenoPayNetworkError as e:
print(f"Network error: {e.message}")
except ZenoPayError as e:
print(f"General error: {e.message}")
Order Models
Creating Orders
from elusion.zenopay.models.order import NewOrder
# Using model
order = NewOrder(
buyer_email="customer@example.com",
buyer_name="John Doe",
buyer_phone="0700000000",
amount=1000,
webhook_url="https://yoursite.com/webhook",
metadata={
"product_id": "12345",
"campaign": "summer_sale"
}
)
# Using dictionary
order_data = {
"buyer_email": "customer@example.com",
"buyer_name": "John Doe",
"buyer_phone": "0700000000",
"amount": 1000
}
Response Models
# Order creation response
response = client.orders.sync.create(order)
print(f"Order ID: {response.data.order_id}")
print(f"Status: {response.data.status}")
print(f"Message: {response.data.message}")
# Status check response
status = client.orders.sync.get_status(order_id)
print(f"Payment Status: {status.data.payment_status}")
print(f"Order ID: {status.data.order_id}")
API Reference
Order Operations
| Method | Sync | Async | Description |
|---|---|---|---|
| Create Order | client.orders.sync.create() |
await client.orders.create() |
Create new payment order |
| Get Status | client.orders.sync.get_status() |
await client.orders.get_status() |
Check order payment status |
| Check Payment | client.orders.sync.check_payment() |
await client.orders.check_payment() |
Returns boolean if paid |
| Wait for Payment | client.orders.sync.wait_for_payment() |
await client.orders.wait_for_payment() |
Poll until completed |
Webhook Events
| Event | Handler Method | Description |
|---|---|---|
| COMPLETED | client.webhooks.on_payment_completed() |
Payment successful |
| FAILED | client.webhooks.on_payment_failed() |
Payment failed |
| PENDING | client.webhooks.on_payment_pending() |
Payment initiated |
| CANCELLED | client.webhooks.on_payment_cancelled() |
Payment cancelled |
Testing
# Create test webhook
test_event = client.webhooks.create_test_webhook("test-order-123", "COMPLETED")
response = client.webhooks.handle_webhook(test_event)
print(f"Test response: {response.status}")
Best Practices
Context Managers
Always use context managers for proper resource cleanup:
# Sync
with client:
response = client.orders.sync.create(order)
# Async
async with client:
response = await client.orders.create(order)
Error Handling
Handle specific exceptions for better error management:
try:
with client:
response = client.orders.sync.create(order)
except ZenoPayValidationError:
# Handle validation errors
pass
except ZenoPayNetworkError:
# Handle network issues
pass
Environment Configuration
Use environment variables for sensitive configuration:
# Don't hardcode credentials
client = ZenoPay(account_id=os.getenv('ZENOPAY_ACCOUNT_ID'))
Support
- GitHub: zenopay-python-sdk
- Issues: Report bugs
- Email: elusion.lab@gmail.com
License
MIT License - see LICENSE file for details.
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
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 zenopay_sdk-0.1.0.tar.gz.
File metadata
- Download URL: zenopay_sdk-0.1.0.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f61d351484c7d84011fadc6656efffeea00e6d3cc575fb3ad41d0f97a3224d7
|
|
| MD5 |
07a1fc7e5631ce1e46c868a5bb701c01
|
|
| BLAKE2b-256 |
48371294776f3ae8ed3962c56aa0de26a69b381a0a53fab0a74a79bf521b53dc
|
Provenance
The following attestation bundles were made for zenopay_sdk-0.1.0.tar.gz:
Publisher:
release.yaml on elusionhub/zenopay-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zenopay_sdk-0.1.0.tar.gz -
Subject digest:
6f61d351484c7d84011fadc6656efffeea00e6d3cc575fb3ad41d0f97a3224d7 - Sigstore transparency entry: 240332066
- Sigstore integration time:
-
Permalink:
elusionhub/zenopay-python-sdk@4d980b381099d6a7e4f268299e0155d063e87d66 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/elusionhub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@4d980b381099d6a7e4f268299e0155d063e87d66 -
Trigger Event:
push
-
Statement type:
File details
Details for the file zenopay_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: zenopay_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39d2e8bd17fa5de0a76cb01eb83a33394cca79a2876d26dbabae800c2e8441d7
|
|
| MD5 |
fbd89f72211f6d3b1c6e6cbc77fe99e8
|
|
| BLAKE2b-256 |
c1eb7c83d38dfd6c5026eca07256797e740a54cfaf532196570b5e693cf4dac9
|
Provenance
The following attestation bundles were made for zenopay_sdk-0.1.0-py3-none-any.whl:
Publisher:
release.yaml on elusionhub/zenopay-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zenopay_sdk-0.1.0-py3-none-any.whl -
Subject digest:
39d2e8bd17fa5de0a76cb01eb83a33394cca79a2876d26dbabae800c2e8441d7 - Sigstore transparency entry: 240332087
- Sigstore integration time:
-
Permalink:
elusionhub/zenopay-python-sdk@4d980b381099d6a7e4f268299e0155d063e87d66 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/elusionhub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@4d980b381099d6a7e4f268299e0155d063e87d66 -
Trigger Event:
push
-
Statement type: