feat(orders): integrate Pally payment and order management
This commit is contained in:
41
routes/orders.py
Normal file
41
routes/orders.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import cfg
|
||||
from core.deps import get_auth_context, get_pally_client
|
||||
from db.session import get_db
|
||||
from external.pally import PallyClient
|
||||
from repositories import AddonsRepository, PricingRepository
|
||||
from repositories.invoices import InvoiceRepository
|
||||
from schemas.dto import AuthContext
|
||||
from schemas.invoices import InvoiceResponse, InvoiceStatus
|
||||
from schemas.plans import OrderDetails
|
||||
from services.plans import calculate_price, get_pricing_model
|
||||
|
||||
router = APIRouter(prefix="/orders")
|
||||
|
||||
|
||||
@router.post("/checkout", response_model=InvoiceResponse, status_code=201)
|
||||
async def checkout(
|
||||
order: OrderDetails,
|
||||
ctx: AuthContext = Depends(get_auth_context),
|
||||
session: AsyncSession = Depends(get_db),
|
||||
pally: PallyClient = Depends(get_pally_client),
|
||||
):
|
||||
addons_repo = AddonsRepository(session)
|
||||
pricing_repo = PricingRepository(session)
|
||||
invoices_repo = InvoiceRepository(session)
|
||||
|
||||
pricing = await get_pricing_model(addons_repo, pricing_repo)
|
||||
price = await calculate_price(addons_repo=addons_repo, order=order, pricing=pricing)
|
||||
|
||||
invoice = await invoices_repo.create(
|
||||
creator_id=ctx.user.id, amount=price, status=InvoiceStatus.ACTIVE
|
||||
)
|
||||
|
||||
bill = await pally.bills.create(price, cfg.pally_shop_id, order_id=invoice.id)
|
||||
|
||||
if not (bill.success and bill.link_page_url):
|
||||
raise HTTPException(500, detail="Failed to create an invoice.")
|
||||
|
||||
return InvoiceResponse(success=True, payment_link=bill.link_page_url, amount=float(price))
|
||||
Reference in New Issue
Block a user