Usage Guide
This comprehensive guide will help you understand how to use Payman in your project and integrate with specific payment gateways.
Supported Gateways
Each gateway comes with its own detailed usage guide and example code:
Common Payment Workflow
Regardless of the gateway, the typical payment flow follows these steps:
- Initialize Gateway - Create a Payman instance with your credentials
- Create Payment Request - Send payment details to the gateway
- Redirect User - Send user to the payment page
- Handle Callback - Process the return from the payment gateway
- Verify Payment - Confirm the transaction was successful
Payman standardizes this flow across all gateways, making it easy to switch between providers.
Basic Usage Patterns
1. Gateway Initialization
from payman import Payman
# Initialize Zibal gateway
pay = Payman("zibal", merchant_id="your-merchant-id")
# For testing, use sandbox mode
pay = Payman("zibal", merchant_id="zibal") # Sandbox mode
2. Payment Creation
response = await pay.initiate_payment(
amount=10_000,
callback_url="https://your-site.com/callback",
description="Order #123",
order_id="order-123"
)
3. User Redirection
if response.success:
payment_url = pay.get_payment_redirect_url(response.track_id)
# Redirect user to payment_url
print(f"Redirect user to: {payment_url}")
4. Payment Verification
# After user returns from payment gateway
verify_response = await pay.verify_payment(track_id=response.track_id)
if verify_response.success:
print(f"Payment successful! Ref: {verify_response.ref_number}")
else:
print(f"Payment failed: {verify_response.message}")
Input Flexibility
Payman supports multiple input formats for maximum flexibility:
Pydantic Models
from zibal.models import PaymentRequest
request = PaymentRequest(
amount=10_000,
callback_url="https://site.com/callback",
description="Test payment"
)
response = await pay.initiate_payment(request)
Dictionary Input
request_data = {
"amount": 10_000,
"callback_url": "https://site.com/callback",
"description": "Test payment"
}
response = await pay.initiate_payment(request_data)
Keyword Arguments
response = await pay.initiate_payment(
amount=10_000,
callback_url="https://site.com/callback",
description="Test payment"
)
Error Handling
Payman provides comprehensive error handling:
from payman import GatewayError
from zibal.exceptions import PaymentNotSuccessfulError
try:
response = await pay.initiate_payment(...)
except GatewayError as e:
print(f"Gateway error: {e}")
except PaymentNotSuccessfulError as e:
print(f"Payment failed: {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")
Advanced Features
Custom HTTP Client Configuration
from payman import Payman
# Configure HTTP client with custom settings
pay = Payman(
"zibal",
merchant_id="your-merchant-id",
timeout=30.0,
max_retries=3,
retry_delay=1.0
)
Logging Configuration
import logging
# Configure logging for Payman
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("payman")
# Payman will automatically use the configured logger
Testing
Sandbox Mode
For testing, use sandbox mode:
# Zibal sandbox mode
pay = Payman("zibal", merchant_id="zibal")
# This will use mock responses for testing
response = await pay.initiate_payment(
amount=10_000,
callback_url="https://your-site.com/callback"
)
Best Practices
- Always use HTTPS for callback URLs in production
- Validate amounts before sending to the gateway
- Store track_id securely for verification
- Handle all exceptions gracefully
- Use environment variables for sensitive data
- Test thoroughly in sandbox mode before going live
- Implement proper logging for debugging and monitoring
Next Steps
- Explore Zibal Integration for detailed Zibal-specific features
- Check the API Reference for complete method documentation
- Review examples for real-world implementation patterns