|
| 1 | +from fastapi import APIRouter, HTTPException |
| 2 | +from pydantic import BaseModel, Field |
| 3 | +from typing import Optional |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +router = APIRouter(prefix="/api/payments", tags=["payments"]) |
| 7 | + |
| 8 | + |
| 9 | +class CheckoutRequest(BaseModel): |
| 10 | + product_code: str = Field(..., description="Internal product or service identifier") |
| 11 | + customer_reference: str = Field(..., description="User, account, or lead identifier") |
| 12 | + amount_usd: float = Field(..., gt=0) |
| 13 | + currency: str = Field(default="USD") |
| 14 | + mode: str = Field(default="one_time", description="one_time, subscription, or usage") |
| 15 | + wallet_address: Optional[str] = None |
| 16 | + metadata: dict = Field(default_factory=dict) |
| 17 | + |
| 18 | + |
| 19 | +class CheckoutResponse(BaseModel): |
| 20 | + checkout_id: str |
| 21 | + status: str |
| 22 | + payment_url: Optional[str] = None |
| 23 | + service_receipt_id: Optional[str] = None |
| 24 | + |
| 25 | + |
| 26 | +class ServiceReceiptResponse(BaseModel): |
| 27 | + receipt_id: str |
| 28 | + status: str |
| 29 | + product_code: str |
| 30 | + customer_reference: str |
| 31 | + delivery_state: str |
| 32 | + |
| 33 | + |
| 34 | +@router.post("/checkout", response_model=CheckoutResponse) |
| 35 | +def create_checkout(request: CheckoutRequest) -> CheckoutResponse: |
| 36 | + checkout_id = f"chk_{uuid4().hex}" |
| 37 | + receipt_id = f"srv_{uuid4().hex}" |
| 38 | + |
| 39 | + return CheckoutResponse( |
| 40 | + checkout_id=checkout_id, |
| 41 | + status="created", |
| 42 | + payment_url=f"https://payments.example/checkout/{checkout_id}", |
| 43 | + service_receipt_id=receipt_id, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@router.get("/service-receipts/{receipt_id}", response_model=ServiceReceiptResponse) |
| 48 | +def get_service_receipt(receipt_id: str) -> ServiceReceiptResponse: |
| 49 | + if not receipt_id.startswith("srv_"): |
| 50 | + raise HTTPException(status_code=404, detail="Service receipt not found") |
| 51 | + |
| 52 | + return ServiceReceiptResponse( |
| 53 | + receipt_id=receipt_id, |
| 54 | + status="active", |
| 55 | + product_code="unknown", |
| 56 | + customer_reference="unknown", |
| 57 | + delivery_state="pending", |
| 58 | + ) |
0 commit comments