Official 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).
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
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 starpay_ethiopia-0.1.0.tar.gz.
File metadata
- Download URL: starpay_ethiopia-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cc3d69d94abce67bbdd5ef06661cfc5e4eea38aa0e79908ad40516ab47a362f
|
|
| MD5 |
424ad7551238084cc8cb9e1e0c96e539
|
|
| BLAKE2b-256 |
0d5a13d53203b68ed6685bd40f1242b35c88fba86b5d77b318f998b093cf2901
|
File details
Details for the file starpay_ethiopia-0.1.0-py3-none-any.whl.
File metadata
- Download URL: starpay_ethiopia-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42b1179994e5edcc58466213429011a89e1226488f588812b44d2195fd1e5a1c
|
|
| MD5 |
7dfa185dd9c37b5ebd4401c66bc992b6
|
|
| BLAKE2b-256 |
f7c0282df54cd47d9cb4b11062ffc30dc038ca33771589beb72daeb15872ae63
|