From 411ade2f989da89eed94d9976ad1f1f1d04bccea Mon Sep 17 00:00:00 2001 From: vahidrezvani Date: Sat, 21 Feb 2026 14:09:06 +0330 Subject: [PATCH] add payment method for panel --- src/routes/payment.py | 29 +++++++++++++++++++++++++-- src/schemas/payment.py | 9 ++++++++- src/services/payment.py | 44 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/routes/payment.py b/src/routes/payment.py index 7eaef31..2bc2a03 100644 --- a/src/routes/payment.py +++ b/src/routes/payment.py @@ -1,17 +1,18 @@ -from fastapi import APIRouter, Depends +from fastapi import APIRouter,Query, Depends from fastapi.security import HTTPAuthorizationCredentials from fastapi.responses import RedirectResponse, StreamingResponse from typing import Annotated from ..messages import MESSAGES from ..dependencies import auth_scheme -from ..schemas.payment import Billings, Url, Plans, Settlement, Credit, PlansV2 ,BasaCreditRequest +from ..schemas.payment import Billings, BillingsResponse,Url, Plans, Settlement, Credit, PlansV2 ,BasaCreditRequest from ..services import ( payment as payment_service, auth as auth_service, storage as storage_service, ) + router = APIRouter( prefix="/paymant", tags=["paymant"], @@ -118,6 +119,30 @@ async def stream_file(id: str, key: str): ) + +##### Admin Routes ##### + +admin_router = APIRouter( + prefix="/admin/payment", + tags=["admin"], + responses={404: {"detail": "Not found"}}, +) + +@admin_router.get("/", response_model=BillingsResponse) +async def history_admin( + token: Annotated[HTTPAuthorizationCredentials, Depends(auth_scheme)], + page: int = Query(1, ge=1, description="شماره صفحه (از ۱ شروع می‌شود)"), + size: int = Query(10, ge=1, le=100, description="تعداد آیتم در هر صفحه (حداکثر ۱۰۰)") +): + user_id = await auth_service.verify_token(token.credentials) + + # پاس دادن مقادیر به سرویس + paginated_data = await payment_service.history_admin(user_id=user_id, page=page, size=size) + + # همون دیتای صفحه‌بندی شده رو مستقیم برمی‌گردونیم + return paginated_data + + ##### V2 Routes ##### diff --git a/src/schemas/payment.py b/src/schemas/payment.py index 2120811..3903f80 100644 --- a/src/schemas/payment.py +++ b/src/schemas/payment.py @@ -57,4 +57,11 @@ class Settlement(BaseModel): class BasaCreditRequest(BaseModel): user_id: UUID - amount: int \ No newline at end of file + amount: int + +class BillingsResponse(BaseModel): + total: int + page: int + size: int + total_pages: int + items: List[Billing] \ No newline at end of file diff --git a/src/services/payment.py b/src/services/payment.py index 9f372f1..6dc8dfd 100644 --- a/src/services/payment.py +++ b/src/services/payment.py @@ -15,7 +15,7 @@ from ..models import ( SettlementType, Plan, ) - +import math def get_credit_from_amount(amount: int): if amount == 20_000: @@ -108,6 +108,48 @@ async def history(user_id: UUID): ] +async def history_admin(user_id: UUID, page: int = 1, size: int = 10): + # محاسبه نقطه شروع (مثلاً صفحه ۲ با سایز ۱۰ میشه از رکورد ۱۰ به بعد) + offset = (page - 1) * size + + # کوئری پایه + base_query = Billing.filter(type__in=[BillingType.BASA, BillingType.FREE]) + + # گرفتن تعداد کل رکوردها (برای اینکه فرانت‌اند بدونه کلاً چند صفحه داریم) + total_count = await base_query.count() + + # گرفتن دیتای فقط همون صفحه + billings = ( + await base_query + .order_by("-created_at") + .offset(offset) + .limit(size) + ) + + items = [ + { + "id": b.id, + "amount": b.amount, + "credit": b.credit, + "discount": b.discount, + "ref_id": b.authority, + "type": b.type, + "status": b.status, + "created_at": b.created_at, + } + for b in billings + ] + + # برگردوندن دیتا همراه با اطلاعات صفحه‌بندی + return { + "total": total_count, + "page": page, + "size": size, + "total_pages": math.ceil(total_count / size), + "items": items + } + + async def verify(status: str, authority: str): client = Client("https://www.zarinpal.com/pg/services/WebGate/wsdl")