fix(pally): align webhook handler with official API spec

This commit is contained in:
2026-05-31 14:25:05 +07:00
parent 763e041a8b
commit b5976d0b21
2 changed files with 25 additions and 29 deletions

View File

@@ -22,40 +22,40 @@ logger = logging.getLogger(__name__)
@router.post("/result") @router.post("/result")
async def pally_callback( async def pally_callback(
id: str = Form(...), InvId: str = Form(...),
bill_id: str = Form(...), OutSum: str = Form(...),
status: str = Form(...), Commission: str = Form(...),
req_amount: str = Form(...), TrsId: str = Form(...),
currency: str = Form(...), Status: str = Form(...),
order_id: str | None = Form(None), CurrencyIn: str = Form(...),
signature: str = Form(...), custom: str | None = Form(None),
SignatureValue: str = Form(...),
session: AsyncSession = Depends(get_db), session: AsyncSession = Depends(get_db),
bot: Bot = Depends(get_bot), bot: Bot = Depends(get_bot),
deps: APIDependenciesDTO = Depends(get_deps), deps: APIDependenciesDTO = Depends(get_deps),
): ):
oid = order_id or "" oid = custom or ""
raw_string = f"{id}{status}{req_amount}{currency}{oid}{settings.pally_token}" raw_string = f"{OutSum}:{InvId}:{settings.pally_token}"
md5_hash = hashlib.md5(raw_string.encode("utf-8")).hexdigest() expected_signature = hashlib.md5(raw_string.encode("utf-8")).hexdigest().upper()
expected_signature = hashlib.sha1(md5_hash.encode("utf-8")).hexdigest()
if signature != expected_signature: if SignatureValue != expected_signature:
logger.critical("Invalid signature for bill %s", bill_id) logger.critical("Invalid signature for TrsId %s", TrsId)
raise HTTPException(403, detail="Invalid signature.") raise HTTPException(403, detail="Invalid signature.")
if status != BillStatus.SUCCESS or not req_amount.isdigit(): if Status != BillStatus.SUCCESS:
logger.info("Bill %s skipped: status=%s, req_amount=%s", bill_id, status, req_amount) logger.info("Bill %s skipped: status=%s", TrsId, Status)
return "OK" return "OK"
logger.info("Received successfully paid bill %s", bill_id) logger.info("Received successfully paid bill %s", TrsId)
if not oid.isdigit(): if not oid.isdigit():
logger.critical("Invalid order_id format for bill %s", bill_id) logger.critical("Invalid custom field format for TrsId %s", TrsId)
return "OK" return "OK"
bill = await deps.bills_repository.get_bill_by_id(session, int(oid)) bill = await deps.bills_repository.get_bill_by_id(session, int(oid))
if not bill: if not bill:
logger.critical("Bill %s is not found in db", bill_id) logger.critical("Bill %s is not found in db", TrsId)
return "OK" return "OK"
if bill.status != DepositStatus.PENDING: if bill.status != DepositStatus.PENDING:
@@ -63,7 +63,7 @@ async def pally_callback(
return "OK" return "OK"
try: try:
amount = int(req_amount) amount = int(float(OutSum))
if bill.amount != amount: if bill.amount != amount:
logger.warning("requested amount for bill=%s is not equal to db entry", bill.id) logger.warning("requested amount for bill=%s is not equal to db entry", bill.id)
@@ -94,8 +94,8 @@ async def pally_callback(
except Exception: except Exception:
logger.exception( logger.exception(
"Exception caught while processing balances for bill %s (Creator: %s)", "Exception caught while processing balances for TrsId %s (Creator: %s)",
bill_id, TrsId,
bill.creator_id, bill.creator_id,
) )

View File

@@ -22,16 +22,12 @@ load_dotenv()
ENDPOINT = "/pally/result" ENDPOINT = "/pally/result"
def calculate_signature( def calculate_signature(
payment_id: str, out_sum: str,
status: str, inv_id: str,
req_amount: str,
currency: str,
order_id: str,
pally_token: str, pally_token: str,
) -> str: ) -> str:
raw = f"{payment_id}{status}{req_amount}{currency}{order_id}{pally_token}" raw = f"{out_sum}:{inv_id}:{pally_token}"
md5 = hashlib.md5(raw.encode()).hexdigest() return hashlib.md5(raw.encode()).hexdigest().upper()
return hashlib.sha1(md5.encode()).hexdigest()
def main(): def main():