mirror of
http://88.130.71.182:3000/BlitTech/deals24togo_be.git
synced 2026-06-12 23:33:21 +00:00
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
"""Payment endpoints — subscription and purchase flows via CinetPay."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Request, Response
|
|
|
|
from app.middleware.auth import get_current_user, require_agency
|
|
from app.schemas.payment import (
|
|
PaymentInitiate,
|
|
PaymentInitiateResponse,
|
|
PaymentReceiptResponse,
|
|
PaymentResponse,
|
|
SubscriptionResponse,
|
|
)
|
|
from app.services.payment_service import PaymentService
|
|
|
|
router = APIRouter(tags=["Payments"])
|
|
|
|
|
|
# ── Payments ─────────────────────────────────────────────
|
|
|
|
|
|
@router.post("/payments/initiate", response_model=PaymentInitiateResponse, status_code=201)
|
|
def initiate_payment(
|
|
body: PaymentInitiate,
|
|
user: dict = Depends(get_current_user),
|
|
):
|
|
svc = PaymentService()
|
|
return svc.initiate(
|
|
user_id=user["id"],
|
|
payment_type=body.type,
|
|
plan=body.plan,
|
|
listing_id=body.listing_id,
|
|
)
|
|
|
|
|
|
@router.post("/payments/webhook")
|
|
async def cinetpay_webhook(request: Request):
|
|
"""Receive CinetPay webhook — no auth required. Returns 200 immediately."""
|
|
try:
|
|
form = await request.form()
|
|
form_data = dict(form)
|
|
except Exception:
|
|
form_data = {}
|
|
|
|
# Also accept query params (CinetPay may use GET in some flows)
|
|
if not form_data:
|
|
form_data = dict(request.query_params)
|
|
|
|
svc = PaymentService()
|
|
try:
|
|
svc.handle_webhook(form_data)
|
|
except Exception:
|
|
pass # Always return 200 to CinetPay
|
|
|
|
return Response(status_code=200)
|
|
|
|
|
|
@router.get("/payments/", response_model=list[PaymentResponse])
|
|
def list_my_payments(user: dict = Depends(get_current_user)):
|
|
svc = PaymentService()
|
|
return svc.get_my_payments(user["id"])
|
|
|
|
|
|
@router.get("/payments/{transaction_id}", response_model=PaymentReceiptResponse)
|
|
def get_payment_receipt(
|
|
transaction_id: str,
|
|
user: dict = Depends(get_current_user),
|
|
):
|
|
svc = PaymentService()
|
|
return svc.get_receipt(transaction_id, user["id"])
|
|
|
|
|
|
# ── Subscriptions ─────────────────────────────────────────
|
|
|
|
|
|
@router.get("/subscriptions/me")
|
|
def my_subscription(user: dict = Depends(require_agency)):
|
|
svc = PaymentService()
|
|
return svc.get_subscription_status(user["id"])
|