Skip to main content

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

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

zenopay_sdk-0.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

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

zenopay_sdk-0.1.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

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

Hashes for zenopay_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f61d351484c7d84011fadc6656efffeea00e6d3cc575fb3ad41d0f97a3224d7
MD5 07a1fc7e5631ce1e46c868a5bb701c01
BLAKE2b-256 48371294776f3ae8ed3962c56aa0de26a69b381a0a53fab0a74a79bf521b53dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for zenopay_sdk-0.1.0.tar.gz:

Publisher: release.yaml on elusionhub/zenopay-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for zenopay_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39d2e8bd17fa5de0a76cb01eb83a33394cca79a2876d26dbabae800c2e8441d7
MD5 fbd89f72211f6d3b1c6e6cbc77fe99e8
BLAKE2b-256 c1eb7c83d38dfd6c5026eca07256797e740a54cfaf532196570b5e693cf4dac9

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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