From b036727e3b4e6210e02aa9d83648c158a3687b0d Mon Sep 17 00:00:00 2001 From: Zakhar Izmaylov Date: Mon, 11 Nov 2024 14:26:00 +0300 Subject: [PATCH] Add base cryptobot --- .flake8 | 2 +- .isort.cfg | 3 +-- handlers/payment/cryprobot_pay.py | 32 +++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 handlers/payment/cryprobot_pay.py diff --git a/.flake8 b/.flake8 index 63d91628..f0ee7e25 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,6 @@ [flake8] max-line-length = 200 -ignore = E203, E266, E501, W503, F541, E704, W293, W291, E126, E121, E123, E128, E302,E131,E231,W292 +ignore = E203, E266, E501, W503, F541, E704, W293, W291, E126, E121, E123, E128, E302, E131, E231, W292, E402 max-complexity = 25 select = B, C, E, F, W, T4, B9 exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,.txt \ No newline at end of file diff --git a/.isort.cfg b/.isort.cfg index 63c90e22..ffc56c98 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -1,4 +1,3 @@ [settings] profile=black -line_length = 200 -skip = ["bot.py"] \ No newline at end of file +line_length = 200 \ No newline at end of file diff --git a/handlers/payment/cryprobot_pay.py b/handlers/payment/cryprobot_pay.py new file mode 100644 index 00000000..9fb260d4 --- /dev/null +++ b/handlers/payment/cryprobot_pay.py @@ -0,0 +1,32 @@ +from aiocryptopay import AioCryptoPay, Networks +from aiohttp import web +from loguru import logger + +from config import CRYPTO_BOT_ENABLE, CRYPTO_BOT_TOKEN + +if CRYPTO_BOT_ENABLE: + crypto = AioCryptoPay(token=CRYPTO_BOT_TOKEN, network=Networks.MAIN_NET) + + +async def cryptobot_webhook(request): + try: + data = await request.json() + logger.info(f"Получены данные вебхука: {data}") + if data.get("update_type") == "invoice_paid": + await process_crypto_payment(data["payload"]) + return web.Response(status=200) + else: + logger.warning(f"Неподдерживаемый тип обновления: {data.get('update_type')}") + return web.Response(status=400) + except Exception as e: + logger.error(f"Ошибка обработки вебхука: {e}") + return web.Response(status=500) + + +async def process_crypto_payment(payload): + if payload["status"] == "paid": + custom_payload = payload["payload"] + user_id, sub_type = custom_payload.split(":") + # TODO + else: + logger.warning(f"Получен неоплаченный инвойс: {payload}") diff --git a/requirements.txt b/requirements.txt index 4e86dd62..da96b1f2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,4 +25,5 @@ urllib3==2.2.3 wrapt==1.16.0 yarl==1.15.5 yookassa==3.3.0 -loguru \ No newline at end of file +loguru +aiocryptopay \ No newline at end of file