52 lines
3.0 KiB
Python
52 lines
3.0 KiB
Python
from __future__ import annotations
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from fastapi.responses import HTMLResponse
|
|
from app.db.models import AppUser
|
|
from app.features.payments import get_razorpay_service
|
|
from .deps import get_checkout_service, get_current_user
|
|
from .schema import CheckoutOrderRequest, CheckoutOrderResponse, CheckoutVerifyRequest
|
|
from .service import CheckoutService
|
|
router = APIRouter(prefix="/checkout", tags=["Checkout & Payments"])
|
|
|
|
@router.get("/config")
|
|
async def get_checkout_config():
|
|
try: return {"razorpay_key_id": get_razorpay_service().key_id}
|
|
except RuntimeError as exc: raise HTTPException(503, detail=str(exc)) from exc
|
|
|
|
@router.post("/create-order", response_model=CheckoutOrderResponse)
|
|
async def create_checkout_order(payload: CheckoutOrderRequest, user: AppUser = Depends(get_current_user), service: CheckoutService = Depends(get_checkout_service)):
|
|
try: return await service.create_order(user, payload)
|
|
except ValueError as exc: raise HTTPException(400, detail=str(exc)) from exc
|
|
except RuntimeError as exc: raise HTTPException(503, detail=str(exc)) from exc
|
|
except Exception as exc: raise HTTPException(502, detail=f"Razorpay checkout initialization failed: {exc}") from exc
|
|
|
|
@router.post("/verify")
|
|
async def verify_checkout_payment(payload: CheckoutVerifyRequest, user: AppUser = Depends(get_current_user), service: CheckoutService = Depends(get_checkout_service)):
|
|
try: return await service.verify(user, payload)
|
|
except LookupError as exc: raise HTTPException(404, detail=str(exc)) from exc
|
|
except ValueError as exc: raise HTTPException(400, detail=str(exc)) from exc
|
|
except RuntimeError as exc: raise HTTPException(503, detail=str(exc)) from exc
|
|
|
|
@router.get("/payment/{payment_id}/invoice")
|
|
async def get_checkout_payment_invoice(payment_id: str, user: AppUser = Depends(get_current_user), service: CheckoutService = Depends(get_checkout_service)):
|
|
from sqlalchemy import select
|
|
from app.db.models import PaymentTransaction
|
|
tx = (await service.session.execute(select(PaymentTransaction).where(PaymentTransaction.gateway_transaction_id == payment_id, PaymentTransaction.user_id == user.id))).scalar_one_or_none()
|
|
if not tx:
|
|
raise HTTPException(404, detail="Transaction record not found or access denied.")
|
|
return {"short_url": f"/api/checkout/payment/{payment_id}/invoice/view"}
|
|
|
|
@router.get("/payment/{payment_id}/invoice/view", response_class=HTMLResponse)
|
|
async def view_checkout_payment_invoice_html(
|
|
payment_id: str,
|
|
token: str | None = Query(None),
|
|
service: CheckoutService = Depends(get_checkout_service),
|
|
):
|
|
from app.features.admin.service import AdminService
|
|
try:
|
|
return await AdminService(service.session).render_invoice_html(payment_id)
|
|
except LookupError as exc:
|
|
raise HTTPException(404, detail=str(exc)) from exc
|
|
except Exception as exc:
|
|
raise HTTPException(500, detail=f"Failed to render HTML invoice: {exc}") from exc
|