working auto
This commit is contained in:
45
app/routers/auto.py
Normal file
45
app/routers/auto.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import crud, database
|
||||
|
||||
# Create a router for auto-specific transaction endpoints
|
||||
auto_router = APIRouter(
|
||||
prefix="/auto/transaction",
|
||||
tags=["auto_transaction"],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
@auto_router.get("/delivery/{auto_id}", summary="Get pre-authorization transaction for auto delivery")
|
||||
def get_auto_delivery_transaction(auto_id: int, db: Session = Depends(database.get_db)):
|
||||
"""
|
||||
Get the pre-authorization transaction for an auto ticket delivery.
|
||||
Used by the capture page for auto deliveries.
|
||||
"""
|
||||
transaction = crud.get_transaction_by_auto_id(db, auto_id=auto_id)
|
||||
if not transaction:
|
||||
raise HTTPException(status_code=404, detail="No pre-authorization transaction found for this auto 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
|
||||
}
|
||||
|
||||
|
||||
@auto_router.put("/delivery/{current_auto_id}/update/{new_auto_id}", summary="Update auto_id for transaction")
|
||||
def update_transaction_auto_id(current_auto_id: int, new_auto_id: int, db: Session = Depends(database.get_db)):
|
||||
"""
|
||||
Update the auto_id of a transaction based on the current auto_id.
|
||||
Used to change transaction from auto_delivery.id to auto_ticket.id
|
||||
"""
|
||||
transaction = crud.get_transaction_by_auto_id(db, auto_id=current_auto_id)
|
||||
if not transaction:
|
||||
raise HTTPException(status_code=404, detail="No transaction found for this auto delivery")
|
||||
|
||||
transaction.delivery_id = new_auto_id
|
||||
transaction.auto_id = new_auto_id
|
||||
db.commit()
|
||||
return {"message": "Transaction auto_id updated"}
|
||||
@@ -238,18 +238,19 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA
|
||||
)
|
||||
|
||||
status, auth_net_transaction_id, rejection_reason = _parse_authnet_response(auth_net_response)
|
||||
|
||||
print(transaction_req)
|
||||
# Create the transaction record in your database with the correct type
|
||||
transaction_data = schemas.TransactionBase(
|
||||
preauthorize_amount=transaction_req.preauthorize_amount,
|
||||
transaction_type=TransactionType.AUTHORIZE, # This is key
|
||||
service_id=transaction_req.service_id,
|
||||
delivery_id=transaction_req.delivery_id,
|
||||
auto_id=transaction_req.auto_id,
|
||||
card_id=transaction_req.card_id,
|
||||
rejection_reason=rejection_reason
|
||||
)
|
||||
|
||||
return crud.create_transaction(
|
||||
db_transaction = crud.create_transaction(
|
||||
db=db,
|
||||
transaction=transaction_data,
|
||||
customer_id=customer_id,
|
||||
@@ -257,4 +258,6 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA
|
||||
auth_net_transaction_id=auth_net_transaction_id
|
||||
)
|
||||
|
||||
return db_transaction
|
||||
|
||||
# --- CAPTURE ENDPOINT MOVED TO TRANSACTION ROUTER ---
|
||||
|
||||
@@ -8,7 +8,7 @@ import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
||||
|
||||
from app import crud, database, schemas
|
||||
from app import crud, database, schemas, models
|
||||
from app.services import payment_service
|
||||
|
||||
# Create a router for transaction endpoints
|
||||
@@ -42,6 +42,37 @@ def get_delivery_transaction(delivery_id: int, db: Session = Depends(database.ge
|
||||
"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
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
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"}
|
||||
|
||||
|
||||
@transaction_router.post("/capture/", response_model=schemas.Transaction, summary="Capture a previously authorized amount")
|
||||
def capture_authorized_amount(transaction: schemas.TransactionCapture, db: Session = Depends(database.get_db)):
|
||||
# This endpoint captures a previously authorized transaction
|
||||
|
||||
Reference in New Issue
Block a user