Files
eamco_authorize/app/routers/transaction.py
Edwin Eames 97261f6c51 Refactor payment service, fix DB session, and consolidate endpoints
- Fix critical NameError in database.py by restoring Session factory
- Refactor payment_service.py and crud.py to use shared constants.py and utils.py
- Deduplicate state mapping and input sanitization logic
- Move transaction amount calculation logic from CRUD to Router layer
- Enforce type safety in schemas using IntEnum for TransactionType/Status
- Move capture endpoint from transaction.py to payment.py (now /payments/capture)
- Update create_customer_profile signature for clarity
2026-02-01 12:31:42 -05:00

78 lines
3.3 KiB
Python

"""
Transaction Router - Endpoints for transaction lookup and capture operations.
"""
import logging
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app import crud, database, schemas, models
from app.services import payment_service
from app.services.payment_service import parse_authnet_response, TransactionStatus
logger = logging.getLogger(__name__)
# Create a router for transaction endpoints
transaction_router = APIRouter()
# Test endpoint to verify router is working
@transaction_router.get("/test/", summary="Test transaction router")
def test_transaction_router():
"""Test endpoint to verify transaction router is loaded"""
logger.info("GET /test - Testing transaction router")
return {"test": "transaction router is working"}
@transaction_router.get("/transaction/delivery/{delivery_id}", summary="Get pre-authorization transaction for a delivery")
def get_delivery_transaction(delivery_id: int, db: Session = Depends(database.get_db)):
"""
Get the pre-authorization transaction for a specific delivery.
This endpoint is used to retrieve transaction details for delivery finalization.
"""
logger.info(f"GET /transaction/delivery/{delivery_id} - Fetching delivery transaction")
transaction = crud.get_transaction_by_delivery_id(db, delivery_id=delivery_id)
if not transaction:
raise HTTPException(status_code=404, detail="No pre-authorization transaction found for this delivery")
return {
"id": transaction.id,
"transaction_type": transaction.transaction_type,
"status": transaction.status,
"auth_net_transaction_id": transaction.auth_net_transaction_id,
"preauthorize_amount": transaction.preauthorize_amount
}
@transaction_router.put("/transaction/delivery/{delivery_id}/update/{new_delivery_id}", summary="Update delivery_id for transaction")
def update_transaction_delivery(delivery_id: int, new_delivery_id: int, db: Session = Depends(database.get_db)):
"""
Update the delivery_id of a transaction based on the current delivery_id.
Used to change transaction from auto_delivery.id to auto_ticket.id
"""
logger.info(f"PUT /transaction/delivery/{delivery_id}/update/{new_delivery_id} - Updating transaction delivery ID")
transaction = crud.get_transaction_by_delivery_id(db, delivery_id=delivery_id)
if not transaction:
raise HTTPException(status_code=404, detail="No transaction found for this delivery")
transaction.delivery_id = new_delivery_id
db.commit()
return {"message": "Transaction delivery_id updated"}
@transaction_router.put("/transaction/{transaction_id}/update_auto_id/{new_auto_id}", summary="Update auto_id for transaction")
def update_transaction_auto_id(transaction_id: int, new_auto_id: int, db: Session = Depends(database.get_db)):
"""
Update the auto_id of a transaction by its ID.
Used to set auto_id to auto_ticket.id after ticket creation.
"""
logger.info(f"PUT /transaction/{transaction_id}/update_auto_id/{new_auto_id} - Updating transaction auto ID")
transaction = db.query(models.Transaction).filter(models.Transaction.id == transaction_id).first()
if not transaction:
raise HTTPException(status_code=404, detail="Transaction not found")
transaction.auto_id = new_auto_id
db.commit()
return {"message": "Transaction auto_id updated"}