Skip to main content

Unofficial Python client for the StarPay API (Ethiopia)

Project description

StarPay Python

A robust, framework-agnostic Python client for the StarPay API (Ethiopia's premier payment gateway).

PyPI version License: MIT

This library allows you to easily integrate StarPay into your Python application (Django, Flask, FastAPI, or pure Python). It handles order creation and secure webhook signature verification.


📦 Installation

Install via pip:

pip install starpay-ethiopia

🚀 Quick Start

1. Initialize the Client

You will need your API URL, API Secret, and Merchant ID from the StarPay developer dashboard.

from starpay import StarPayClient

client = StarPayClient(
    api_url="https://sandbox-api.starpayethiopia.com/v1/starpay-api",  # Or production URL
    api_secret="your_api_secret_here",
    merchant_id="your_merchant_id_here"
)

2. Create a Payment Order

Use create_order to generate a secure checkout URL to which you redirect your user.

from starpay import StarPayAPIError

try:
    payment_url = client.create_order(
        amount=150.50,
        description="Premium Subscription",
        customer_name="Abebe Bikila",
        customer_phone="+251900000000",
        callback_url="https://yourdomain.com/api/webhooks/starpay/",
        items=[
            {
                "productId": "sub-001",
                "quantity": 1,
                "item_name": "Monthly Premium",
                "unit_price": 150.50
            }
        ],
        metadata={"internal_order_id": "ORD-998877"}
    )
    
    print(f"Redirect user to: {payment_url}")
    
except StarPayAPIError as e:
    print(f"Failed to create order: {e}")

3. Verify Webhook Signatures

When a user completes a payment, StarPay will send an HTTP POST request to your callback_url. You must verify the signature to ensure the request genuinely came from StarPay.

Here is an example of verifying the signature in a typical webhook handler:

from starpay import StarPaySignatureError

def handle_webhook(request):
    payload = request.json()                  # Parse JSON body
    timestamp = request.headers.get("X-Timestamp")
    signature = request.headers.get("X-Signature")

    try:
        is_valid = client.verify_signature(
            payload=payload,
            timestamp=timestamp,
            signature=signature
        )
        
        if is_valid:
            # ✅ Payment is authentic!
            order_id = payload.get("metadata", {}).get("internal_order_id")
            payment_status = payload.get("status")
            print(f"Order {order_id} status is now {payment_status}")
            return {"status": "success"}, 200
        else:
            # ❌ Invalid signature
            return {"error": "Unauthorized"}, 401
            
    except StarPaySignatureError as e:
        print(f"Signature verification failed: {e}")
        return {"error": "Bad Request"}, 400

🛠 Advanced

Error Handling

The library exports custom exceptions in starpay.exceptions:

  • StarPayError: Base exception for all StarPay errors.
  • StarPayAPIError: Raised when the StarPay API returns a non-success response or fails to connect.
  • StarPaySignatureError: Raised when webhook signature verification fails due to malformed data.

Framework Agnostic

This library uses Python's standard urllib and hmac libraries under the hood. It has zero external dependencies, making it incredibly lightweight and compatible with any Python environment >= 3.7.


📄 License

This project is licensed under the 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

starpay_ethiopia-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

starpay_ethiopia-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file starpay_ethiopia-0.1.1.tar.gz.

File metadata

  • Download URL: starpay_ethiopia-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for starpay_ethiopia-0.1.1.tar.gz
Algorithm Hash digest
SHA256 eba3e85b0dc15af12446ac7d30bd835d6367854e86b1ff2d483cc633744a98e0
MD5 4901c1a528de30fce1319ff5ec432193
BLAKE2b-256 c1db45b36406e8f222dc2dd4a01a098e1bd3cb0da73e58ac4156ce575df216c4

See more details on using hashes here.

File details

Details for the file starpay_ethiopia-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for starpay_ethiopia-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83c34061ed9910cb2d384a349b2bb1f1879e74f3fe932a1a7ad2c7a3c806aade
MD5 ab8716e45bdfd75e3df05e1b76fdb96f
BLAKE2b-256 40ec0d3df8828ecc34bdf7d7da942c301c0e6df7db602cf47c92eeeb598b33ef

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