add payment method for panel
CI/CD Pipeline / build-and-deploy (push) Successful in 16s Details

This commit is contained in:
vahidrezvani 2026-02-21 14:09:06 +03:30
parent 3676a40913
commit 411ade2f98
3 changed files with 78 additions and 4 deletions

View File

@ -1,17 +1,18 @@
from fastapi import APIRouter, Depends from fastapi import APIRouter,Query, Depends
from fastapi.security import HTTPAuthorizationCredentials from fastapi.security import HTTPAuthorizationCredentials
from fastapi.responses import RedirectResponse, StreamingResponse from fastapi.responses import RedirectResponse, StreamingResponse
from typing import Annotated from typing import Annotated
from ..messages import MESSAGES from ..messages import MESSAGES
from ..dependencies import auth_scheme 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 ( from ..services import (
payment as payment_service, payment as payment_service,
auth as auth_service, auth as auth_service,
storage as storage_service, storage as storage_service,
) )
router = APIRouter( router = APIRouter(
prefix="/paymant", prefix="/paymant",
tags=["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 ##### ##### V2 Routes #####

View File

@ -57,4 +57,11 @@ class Settlement(BaseModel):
class BasaCreditRequest(BaseModel): class BasaCreditRequest(BaseModel):
user_id: UUID user_id: UUID
amount: int amount: int
class BillingsResponse(BaseModel):
total: int
page: int
size: int
total_pages: int
items: List[Billing]

View File

@ -15,7 +15,7 @@ from ..models import (
SettlementType, SettlementType,
Plan, Plan,
) )
import math
def get_credit_from_amount(amount: int): def get_credit_from_amount(amount: int):
if amount == 20_000: 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): async def verify(status: str, authority: str):
client = Client("https://www.zarinpal.com/pg/services/WebGate/wsdl") client = Client("https://www.zarinpal.com/pg/services/WebGate/wsdl")