81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
import uuid
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_user
|
|
from app.db.session import get_db
|
|
from app.models.order import Order, OrderStatus
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class PaymentRequest(BaseModel):
|
|
order_id: str
|
|
payment_method: str
|
|
card_number: str
|
|
card_expiry: str
|
|
card_cvv: str
|
|
|
|
|
|
class PaymentResponse(BaseModel):
|
|
payment_id: str
|
|
order_id: str
|
|
amount: float
|
|
status: str
|
|
message: str
|
|
|
|
|
|
@router.post("/", response_model=PaymentResponse)
|
|
def process_payment(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
payment_in: PaymentRequest,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Process payment for an order. This is a mock implementation.
|
|
"""
|
|
# Get order
|
|
order = db.query(Order).filter(Order.id == payment_in.order_id).first()
|
|
|
|
if not order:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Order not found",
|
|
)
|
|
|
|
# Check permissions
|
|
if order.user_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions",
|
|
)
|
|
|
|
# Check if order can be paid
|
|
if order.status != OrderStatus.PENDING:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Cannot process payment for order with status {order.status}",
|
|
)
|
|
|
|
# Mock payment processing
|
|
# In a real application, this would integrate with a payment gateway
|
|
payment_id = str(uuid.uuid4())
|
|
|
|
# Update order status and payment ID
|
|
order.status = OrderStatus.PAID
|
|
order.payment_id = payment_id
|
|
db.add(order)
|
|
db.commit()
|
|
|
|
return {
|
|
"payment_id": payment_id,
|
|
"order_id": order.id,
|
|
"amount": order.total_amount,
|
|
"status": "success",
|
|
"message": "Payment processed successfully",
|
|
} |