FastAPI Integration Example
Complete FastAPI application example showing how to integrate PayTechUZ with FastAPI for payment processing.
📁 Complete Project
The full working FastAPI example is available on GitHub:
This example includes:
- ✅ Complete FastAPI application setup
- ✅ Database models and migrations
- ✅ Payment gateway integration (Payme & Click)
- ✅ Webhook handlers for payment notifications
- ✅ Order management system
- ✅ API endpoints for creating payments
- ✅ Error handling and validation
🚀 Quick Start
1. Clone the Example
git clone https://github.com/PayTechUz/paytechuz.git
cd paytechuz/examples/paytechuz_fastapi
2. Install Dependencies
pip install -r requirements.txt
3. Configure Environment
Create a .env
file:
# Payme Configuration
PAYME_ID=your_payme_id
PAYME_KEY=your_payme_key
# Click Configuration
CLICK_SERVICE_ID=your_service_id
CLICK_MERCHANT_ID=your_merchant_id
CLICK_MERCHANT_USER_ID=your_merchant_user_id
CLICK_SECRET_KEY=your_secret_key
# Environment
IS_TEST_MODE=True
DATABASE_URL=sqlite:///./payments.db
4. Run the Application
uvicorn main:app --reload
The API will be available at http://localhost:8000
📋 API Endpoints
Create Order with Payment
POST /orders/
{
"product_name": "Test Product",
"amount": 50.00,
"payment_method": "payme",
"return_url": "https://yoursite.com/success"
}
Response:
{
"id": 1,
"product_name": "Test Product",
"amount": 50.00,
"status": "pending",
"created_at": "2024-01-01T12:00:00Z",
"payment_method": "payme",
"payment_link": "https://checkout.paycom.uz/..."
}
Webhook Endpoints
- POST
/payments/payme/webhook
- Payme payment notifications - POST
/payments/click/webhook
- Click payment notifications
🏗️ Project Structure
paytechuz_fastapi/
├── app/
│ ├── __init__.py
│ ├── database.py # Database configuration
│ ├── models.py # SQLAlchemy models
│ ├── typing.py # Pydantic models
│ └── webhook_handlers.py # Custom webhook handlers
├── main.py # FastAPI application
├── requirements.txt # Dependencies
└── README.md # Project documentation
💡 Key Features
1. Order Management
The example includes a complete order management system:
class Order(Base):
__tablename__ = "orders"
id = Column(Integer, primary_key=True, index=True)
product_name = Column(String, index=True)
amount = Column(Float)
status = Column(String, default="pending")
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
2. Payment Gateway Integration
Support for both Payme and Click payment gateways:
# Payme Gateway
payme = PaymeGateway(
payme_id="your_payme_id",
payme_key="your_payme_key",
is_test_mode=True
)
# Click Gateway
click = ClickGateway(
service_id="your_service_id",
merchant_id="your_merchant_id",
merchant_user_id="your_merchant_user_id",
secret_key="your_secret_key",
is_test_mode=True
)
3. Webhook Handling
Automatic payment status updates via webhooks:
class CustomPaymeWebhookHandler(PaymeWebhookHandler):
def successfully_payment(self, params, transaction):
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
if order:
order.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
if order:
order.status = "cancelled"
self.db.commit()
🧪 Testing the Example
1. Create a Test Order
curl -X POST "http://localhost:8000/orders/" \
-H "Content-Type: application/json" \
-d '{
"product_name": "Test Product",
"amount": 50.00,
"payment_method": "payme",
"return_url": "https://example.com/success"
}'
2. Test Payment Flow
- Use the returned
payment_link
to make a test payment - Payment status will be automatically updated via webhooks
- Check order status in your database
📚 Learn More
For detailed integration guides, visit:
- FastAPI Integration Guide - Complete setup instructions
- Django Integration Guide - Django alternative
- Getting Started - Basic installation
🆘 Support
If you need help with the FastAPI example:
- Check the GitHub repository
- Join our Telegram community
- Contact for consultation
⭐ Star the project on GitHub if this example helped you!