SDK Python احترافي للتكامل مع بوابة الدفع إيصالات من خدماتك الخلفية.
قم بتثبيت إيصالات Python SDK باستخدام pip. يتطلب SDK إصدار Python 3.8 أو أعلى.
pip install esaalatإليك مثال بسيط لتبدأ بإنشاء طلب:
from esaalat import Esaalat
# Initialize the client
client = Esaalat(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.esaalat.com" # api.sandbox.esaalat.com for sandbox
)
# List available subscriptions
subscriptions = client.get_subscriptions()
print(f"Found {len(subscriptions)} subscription(s)")
# Create an order
order_id = client.create_order(
subscription_id=subscriptions[0].id,
items=[
{
"name": "T-Shirt",
"description": "Premium cotton t-shirt",
"price": 29.99,
"quantity": 2
}
],
currency="USD"
)
print(f"Order created: {order_id}")قم بتهيئة عميل إيصالات ببيانات اعتمادك. يتعامل SDK مع إدارة الرموز تلقائيًا.
from esaalat import Esaalat
client = Esaalat(
client_id="your-client-id", # Your Esaalat client ID
client_secret="your-client-secret", # Your client secret
base_url="https://api.esaalat.com", # API base URL (api.sandbox.esaalat.com for sandbox)
timeout=30 # Optional: request timeout in seconds
)💡 نصيحة: قم بتخزين بيانات اعتمادك في متغيرات البيئة للأمان:
import os
from esaalat import Esaalat
client = Esaalat(
client_id=os.getenv("ESAALAT_CLIENT_ID"),
client_secret=os.getenv("ESAALAT_CLIENT_SECRET"),
base_url=os.getenv("ESAALAT_BASE_URL") # api.sandbox.esaalat.com for sandbox
)استخدم العميل كمدير سياق لضمان التنظيف الصحيح:
with Esaalat(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.esaalat.com" # api.sandbox.esaalat.com for sandbox
) as client:
subscriptions = client.get_subscriptions()
# ... use client
# Session automatically closedاسترجع قائمة حسابات الاشتراك المتاحة لتلقي المدفوعات. ستحتاج إلى معرف اشتراك لإنشاء الطلبات.
# Get all subscriptions
subscriptions = client.get_subscriptions()
# Filter for active subscriptions that can receive payments
active_subs = [s for s in subscriptions if s.is_active and s.can_receive]
# Use the first active subscription
if active_subs:
subscription_id = active_subs[0].id
print(f"Using: {active_subs[0].provider_name} - {active_subs[0].username}")يحتوي كل اشتراك على الحقول التالية التي يمكنك استخدامها:
idمعرف اشتراك فريد (استخدمه للطلبات)usernameاسم مستخدم الحسابprovider_nameاسم مزود الدفعis_activeما إذا كان الاشتراك نشطًاcan_receiveما إذا كان يمكنه تلقي المدفوعاتأنشئ طلبًا جديدًا بعنصر واحد أو أكثر. يتطلب كل عنصر اسمًا ووصفًا وسعرًا وكمية.
# Create an order with multiple items
order_id = client.create_order(
subscription_id="f7430a9b-76c6-4c52-91c7-0337d8ca9e9c",
items=[
{
"name": "T-Shirt",
"description": "Premium cotton t-shirt",
"price": 29.99,
"quantity": 2
},
{
"name": "Mug",
"description": "Coffee mug",
"price": 9.99,
"quantity": 1
}
],
currency="USD" # "USD" or "YER"
)
print(f"Order created: {order_id}")جلب تفاصيل الطلب للتحقق من حالة الدفع وعرض معلومات الطلب:
# Get order details
order = client.get_order(order_id)
print(f"Status: {order.status}")
print(f"Total: {order.total} {order.currency}")
print(f"Client: {order.client.name}")
# List items
for item in order.items:
print(f" - {item.name}: {item.quantity} x {item.price}")يوفر SDK فئات استثناء محددة لأنواع مختلفة من الأخطاء:
from esaalat import (
Esaalat,
ValidationError,
AuthenticationError,
OrderNotFoundError,
APIError
)
client = Esaalat(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.esaalat.com" # api.sandbox.esaalat.com for sandbox
)
try:
order_id = client.create_order(
subscription_id="invalid-id",
items=[
{
"name": "Product",
"description": "Description",
"price": -10.50, # Invalid: negative price
"quantity": 1
}
],
currency="USD"
)
except ValidationError as e:
print(f"Validation error: {e}")
# Handle invalid input
except AuthenticationError as e:
print(f"Authentication failed: {e}")
# Handle auth issues
except OrderNotFoundError as e:
print(f"Order not found: {e}")
# Handle missing order
except APIError as e:
print(f"API error: {e}")
print(f"Status code: {e.status_code}")
# Handle API errors
except Exception as e:
print(f"Unexpected error: {e}")
# Handle other errorsValidationErrorيُثار عند فشل التحقق من الإدخال (سعر غير صالح، كمية، إلخ)
AuthenticationErrorيُثار عند فشل المصادقة (بيانات اعتماد غير صالحة)
OrderNotFoundErrorيُثار عند عدم العثور على طلب (404)
APIErrorيُثار عند فشل طلب واجهة برمجة التطبيقات (يتضمن status_code و response_data)
إليك مثال كامل يوضح تدفق الدفع الكامل من البداية إلى النهاية:
from esaalat import Esaalat, OrderItem
# Initialize client
client = Esaalat(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.esaalat.com" # api.sandbox.esaalat.com for sandbox
)
# 1. Get active subscriptions
subscriptions = client.get_subscriptions()
active_sub = next((s for s in subscriptions if s.is_active and s.can_receive), None)
if not active_sub:
print("No active subscriptions available")
exit(1)
print(f"Using subscription: {active_sub.provider_name}")
# 2. Create order with shopping cart items
cart_items = [
OrderItem(
name="Premium T-Shirt",
description="100% cotton, size L",
price=29.99,
quantity=2
),
OrderItem(
name="Coffee Mug",
description="Ceramic mug, 350ml",
price=9.99,
quantity=1
)
]
order_id = client.create_order(
subscription_id=active_sub.id,
items=cart_items,
currency="USD"
)
print(f"Order created: {order_id}")
# 3. Check order status
order = client.get_order(order_id)
if order.status == "COMPLETED":
print("✓ Payment received!")
print(f"Total: {order.total} {order.currency}")
elif order.status == "PENDING":
print("⏳ Waiting for payment...")
else:
print(f"Status: {order.status}")استطلاع تحديثات حالة الطلب للتحقق من اكتمال الدفع:
import time
from esaalat import Esaalat, OrderNotFoundError
client = Esaalat(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.esaalat.com" # api.sandbox.esaalat.com for sandbox
)
def wait_for_payment(order_id, max_attempts=10):
"""Poll order status until payment completes"""
for attempt in range(max_attempts):
try:
order = client.get_order(order_id)
if order.status == "COMPLETED":
print("✓ Payment completed!")
return True
elif order.status == "FAILED":
print("✗ Payment failed")
return False
else:
print(f"Status: {order.status}, checking again in 5s...")
time.sleep(5)
except OrderNotFoundError:
print("Order not found")
return False
print("Timeout waiting for payment")
return False
# Use it
order_id = "your-order-id"
success = wait_for_payment(order_id)