FastAPI Integration
Complete guide for integrating PayTechUZ with FastAPI applications.
Installation
Install PayTechUZ with FastAPI support:
pip install paytechuz[fastapi]
Configuration
Set up database models:
from datetime import datetime, timezone
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime
from paytechuz.integrations.fastapi import Base as PaymentsBase
from paytechuz.integrations.fastapi.models import run_migrations
# Create database engine
SQLALCHEMY_DATABASE_URL = "sqlite:///./payments.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
# Create base declarative class
Base = declarative_base()
# Create Order model
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))
# Create payment tables using run_migrations
run_migrations(engine)
# Create Order table
Base.metadata.create_all(bind=engine)
# Create session
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Create webhook handlers:
from fastapi import FastAPI, Request, Depends
from sqlalchemy.orm import Session
from paytechuz.integrations.fastapi import PaymeWebhookHandler, ClickWebhookHandler
app = FastAPI()
# Dependency to get the database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
class CustomPaymeWebhookHandler(PaymeWebhookHandler):
def successfully_payment(self, params, transaction):
# Handle successful payment
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
order.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
# Handle cancelled payment
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
order.status = "cancelled"
self.db.commit()
class CustomClickWebhookHandler(ClickWebhookHandler):
def successfully_payment(self, params, transaction):
# Handle successful payment
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
order.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
# Handle cancelled payment
order = self.db.query(Order).filter(Order.id == transaction.account_id).first()
order.status = "cancelled"
self.db.commit()
@app.post("/payments/payme/webhook")
async def payme_webhook(request: Request, db: Session = Depends(get_db)):
handler = CustomPaymeWebhookHandler(
db=db,
payme_id="your_merchant_id",
payme_key="your_merchant_key",
account_model=Order,
account_field='id',
amount_field='amount'
)
return await handler.handle_webhook(request)
@app.post("/payments/click/webhook")
async def click_webhook(request: Request, db: Session = Depends(get_db)):
handler = CustomClickWebhookHandler(
db=db,
service_id="your_service_id",
merchant_id="your_merchant_id",
secret_key="your_secret_key",
account_model=Order
)
return await handler.handle_webhook(request)
Creating Payment Links
from paytechuz.gateways.payme import PaymeGateway
from paytechuz.gateways.click import ClickGateway
# Get the order
order = db.query(Order).filter(Order.id == 1).first()
# Create Payme payment link
payme = PaymeGateway(
payme_id='your_payme_id',
payme_key='your_payme_key',
is_test_mode=True # Set to False in production
)
payme_link = payme.create_payment(
id=order.id,
amount=order.amount,
return_url="https://example.com/return"
)
# Create Click payment link
click = ClickGateway(
service_id='your_click_service_id',
merchant_id='your_click_merchant_id',
merchant_user_id='your_click_merchant_user_id',
secret_key='your_click_secret_key',
is_test_mode=True # Set to False in production
)
click_link = click.create_payment(
id=order.id,
amount=order.amount,
return_url="https://example.com/return"
)
Complete Example
For a complete working FastAPI application with PayTechUZ integration, check out our example project:
🔗 FastAPI Integration Example - Complete FastAPI project with payment processing
This example includes:
- ✅ Full application setup
- ✅ Database models and migrations
- ✅ Payment gateway integration
- ✅ Webhook handlers
- ✅ API endpoints
- ✅ Error handling
Next Steps
- FastAPI Example Project - Complete working example
- Getting Started - Basic installation guide
- Django Integration - Alternative framework integration