Files
malenia/tests/send_pally_webhook.py
hexdev 287f8eebda feat: add subscription and transaction repositories
- Implemented SubscriptionRepository with methods to get, create, and update subscriptions.
- Added BalanceTXRepository for creating and retrieving balance transactions.
- Introduced a test script for simulating Pally webhook for local testing.
2026-04-27 21:33:25 +07:00

82 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Симулятор вебхука Pally для локального тестирования.
Использование:
python tests/send_test_webhook.py --user-id 123456789 --amount 300 --bill-id 1
Предварительно:
- API должен быть запущен: uvicorn api.main:app --port 8000
- Пользователь с --user-id должен существовать в БД
- Счёт с --bill-id должен существовать в БД и принадлежать этому пользователю
"""
import argparse
import hashlib
import os
import httpx
from dotenv import load_dotenv
load_dotenv()
def calculate_signature(
payment_id: str,
status: str,
req_amount: str,
currency: str,
order_id: str,
pally_token: str,
) -> str:
raw = f"{payment_id}{status}{req_amount}{currency}{order_id}{pally_token}"
md5 = hashlib.md5(raw.encode()).hexdigest()
return hashlib.sha1(md5.encode()).hexdigest()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--user-id", type=int, required=True)
parser.add_argument("--amount", type=int, required=True)
parser.add_argument("--bill-id", type=int, required=True)
parser.add_argument("--host", default="http://127.0.0.1:8000")
parser.add_argument(
"--status",
default="SUCCESS",
choices=["SUCCESS", "FAIL", "PROCESS"],
help="Статус платежа (по умолчанию SUCCESS)",
)
args = parser.parse_args()
pally_token = os.environ["PALLY_TOKEN"]
# Эти поля могут быть любыми строками — вебхук их не использует содержательно,
# только включает в подпись. Главное чтобы совпадали с тем, что пойдёт в подпись.
payment_id = "test_payment_001"
bill_id = f"test_bill_{args.bill_id}"
currency = "RUB"
order_id = str(args.bill_id) # order_id == id счёта в нашей БД
signature = calculate_signature(
payment_id, args.status, str(args.amount), currency, order_id, pally_token
)
payload = {
"id": payment_id,
"bill_id": bill_id,
"status": args.status,
"req_amount": str(args.amount),
"currency": currency,
"order_id": order_id,
"signature": signature,
}
print(f"{args.host}/checkout/pally/callback")
print(f"Отправляю вебхук: {payload}\n")
response = httpx.post(f"{args.host}/checkout/pally/callback", data=payload)
print(f"Статус ответа: {response.status_code}")
print(f"Тело ответа: {response.text}")
if __name__ == "__main__":
main()