FastAPI Integratsiyasi
PayTechUZ ni FastAPI ilovalari bilan integratsiya qilish uchun to'liq qo'llanma.
Tayyor misol loyiha
Tayyor FastAPI loyihasi kerakmi? To'liq to'lov integratsiyasi bilan FastAPI misol repository ni ko'ring!
O'rnatish
PayTechUZ ni FastAPI qo'llab-quvvatlashi bilan o'rnating:
pip install paytechuz[fastapi]
Konfiguratsiya
Ma'lumotlar bazasi modellarini sozlang:
from datetime import datetime, timezone
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey
from paytechuz.integrations.fastapi import Base as PaymentsBase
from paytechuz.integrations.fastapi.models import run_migrations
# Ma'lumotlar bazasi dvigatelini yaratish
SQLALCHEMY_DATABASE_URL = "sqlite:///./payments.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
# Asosiy deklarativ klassni yaratish
Base = declarative_base()
# Buyurtma modelini yaratish
class Order(Base):
__tablename__ = "orders"
id = Column(Integer, primary_key=True, index=True)
product_name = Column(String, index=True)
total_amount = Column(Float)
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
# Hisob-faktura modelini yaratish
class Invoice(Base):
__tablename__ = "invoices"
id = Column(Integer, primary_key=True, index=True)
order_id = Column(Integer, ForeignKey("orders.id"))
amount = Column(Float)
status = Column(String, default="pending")
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
order = relationship("Order")
# run_migrations yordamida to'lov jadvallarini yaratish
run_migrations(engine)
# Buyurtma va Hisob-faktura jadvallarini yaratish
Base.metadata.create_all(bind=engine)
# Sessiya yaratish
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Webhook ishlovchilarini yaratish:
from fastapi import FastAPI, Request, Depends
from sqlalchemy.orm import Session
from paytechuz.integrations.fastapi import (
PaymeWebhookHandler,
ClickWebhookHandler,
AtmosWebhookHandler
)
app = FastAPI()
# Ma'lumotlar bazasi sessiyasini olish uchun bog'liqlik
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
class CustomPaymeWebhookHandler(PaymeWebhookHandler):
def successfully_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.status = "cancelled"
self.db.commit()
class CustomClickWebhookHandler(ClickWebhookHandler):
def successfully_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.status = "cancelled"
self.db.commit()
class CustomAtmosWebhookHandler(AtmosWebhookHandler):
def successfully_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.status = "paid"
self.db.commit()
def cancelled_payment(self, params, transaction):
invoice = self.db.query(Invoice).filter(Invoice.id == transaction.account_id).first()
invoice.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=Invoice,
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",
secret_key="your_secret_key",
account_model=Invoice
)
return await handler.handle_webhook(request)
@app.post("/payments/atmos/webhook")
async def atmos_webhook(request: Request, db: Session = Depends(get_db)):
handler = CustomAtmosWebhookHandler(
db=db,
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret",
store_id="your_store_id",
api_key="your_api_key",
account_model=Invoice,
account_field='id'
)
return await handler.handle_webhook(request)
To'lov havolalarini yaratish
import os
from pydantic import BaseModel
from paytechuz.gateways.payme import PaymeGateway
from paytechuz.gateways.click import ClickGateway
from paytechuz.gateways.atmos import AtmosGateway
class OrderCreate(BaseModel):
product_name: str
amount: float
payment_type: str # 'payme', 'click', or 'atmos'
@app.post("/order/create")
def create_order(order_data: OrderCreate, db: Session = Depends(get_db)):
# 1. Buyurtma yaratish
order = Order(
product_name=order_data.product_name,
total_amount=order_data.amount
)
db.add(order)
db.commit()
db.refresh(order)
# 2. Buyurtmaga bog'langan hisob-faktura yaratish
invoice = Invoice(
order_id=order.id,
amount=order.total_amount,
status="pending"
)
db.add(invoice)
db.commit()
db.refresh(invoice)
payment_url = None
# 3. To'lov turiga qarab to'lov havolasini yaratish
if order_data.payment_type == 'payme':
gateway = PaymeGateway(
payme_id='your_payme_id',
payme_key='your_payme_key',
is_test_mode=True
)
payment_url = gateway.create_payment(
id=invoice.id,
amount=int(invoice.amount * 100), # Payme tiyin bilan ishlaydi
return_url="https://example.com/success"
)
elif order_data.payment_type == 'click':
gateway = 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
)
payment_url = gateway.create_payment(
id=invoice.id,
amount=invoice.amount,
return_url="https://example.com/success"
)
elif order_data.payment_type == 'atmos':
gateway = AtmosGateway(
consumer_key='your_atmos_consumer_key',
consumer_secret='your_atmos_consumer_secret',
store_id='your_atmos_store_id',
is_test_mode=True
)
payment_data = gateway.create_payment(
account_id=invoice.id,
amount=int(invoice.amount * 100) # Atmos tiyin bilan ishlaydi
)
payment_url = payment_data['payment_url']
return {
"order_id": order.id,
"invoice_id": invoice.id,
"payment_url": payment_url
}
Keyingi qadamlar
- Misollar - To'liq ishlaydigan misollar
- Boshlash - Asosiy o'rnatish qo'llanmasi
- Django Integratsiyasi - Muqobil framework integratsiyasi