diff --git a/handlers/payments/kassai.py b/handlers/payments/kassai.py index a07180bc..8231ad0a 100644 --- a/handlers/payments/kassai.py +++ b/handlers/payments/kassai.py @@ -76,7 +76,7 @@ async def process_callback_pay_kassai(callback_query: types.CallbackQuery, state ) ) builder.row(InlineKeyboardButton(text="Ввести сумму", callback_data=f"kassai_custom_amount|{method_name}")) - builder.row(InlineKeyboardButton(text=BACK, callback_data="pay")) + builder.row(InlineKeyboardButton(text=BACK, callback_data="balance")) await callback_query.message.delete() new_message = await callback_query.message.answer( @@ -209,16 +209,21 @@ async def handle_custom_amount_input(message: types.Message, state: FSMContext): confirm_keyboard = InlineKeyboardMarkup( inline_keyboard=[ [InlineKeyboardButton(text=PAY_2, url=payment_url)], - [InlineKeyboardButton(text=BACK, callback_data="pay_kassai")], + [InlineKeyboardButton(text=BACK, callback_data="balance")], ] ) - await edit_or_send_message( + payment_message = await edit_or_send_message( target_message=message, text=KASSAI_PAYMENT_MESSAGE.format(amount=amount), reply_markup=confirm_keyboard, force_text=True, ) + + await state.update_data( + payment_message_id=payment_message.message_id if hasattr(payment_message, 'message_id') else message.message_id, + payment_chat_id=message.chat.id + ) await state.set_state(ReplenishBalanceKassaiState.waiting_for_payment_confirmation) @@ -258,16 +263,21 @@ async def process_amount_selection(callback_query: types.CallbackQuery, state: F confirm_keyboard = InlineKeyboardMarkup( inline_keyboard=[ [InlineKeyboardButton(text=PAY_2, url=payment_url)], - [InlineKeyboardButton(text=BACK, callback_data="pay_kassai")], + [InlineKeyboardButton(text=BACK, callback_data="balance")], ] ) - await edit_or_send_message( + payment_message = await edit_or_send_message( target_message=callback_query.message, text=KASSAI_PAYMENT_MESSAGE.format(amount=amount), reply_markup=confirm_keyboard, force_text=True, ) + + await state.update_data( + payment_message_id=payment_message.message_id if hasattr(payment_message, 'message_id') else callback_query.message.message_id, + payment_chat_id=callback_query.message.chat.id + ) await state.set_state(ReplenishBalanceKassaiState.waiting_for_payment_confirmation) diff --git a/handlers/payments/wata.py b/handlers/payments/wata.py index d873d260..e2478a81 100644 --- a/handlers/payments/wata.py +++ b/handlers/payments/wata.py @@ -131,7 +131,7 @@ async def process_cassa_selection(callback_query: types.CallbackQuery, state: FS callback_data=f'wata_amount|{cassa_name}|{PAYMENT_OPTIONS[i]["callback_data"]}', ) ) - builder.row(InlineKeyboardButton(text=BACK, callback_data="pay_wata")) + builder.row(InlineKeyboardButton(text=BACK, callback_data="balance")) await callback_query.message.delete() new_message = await callback_query.message.answer( text=cassa["desc"], @@ -194,7 +194,7 @@ async def handle_custom_amount_input(message: types.Message, state: FSMContext): confirm_keyboard = InlineKeyboardMarkup( inline_keyboard=[ [InlineKeyboardButton(text=PAY_2, url=payment_url)], - [InlineKeyboardButton(text=BACK, callback_data="pay_wata")], + [InlineKeyboardButton(text=BACK, callback_data="balance")], ] ) await edit_or_send_message( @@ -237,7 +237,7 @@ async def process_amount_selection(callback_query: types.CallbackQuery, state: F confirm_keyboard = InlineKeyboardMarkup( inline_keyboard=[ [InlineKeyboardButton(text=PAY_2, url=payment_url)], - [InlineKeyboardButton(text=BACK, callback_data="pay_wata")], + [InlineKeyboardButton(text=BACK, callback_data="balance")], ] ) await edit_or_send_message( diff --git a/web/kassai_payment.py b/web/kassai_payment.py index dd6cc603..d21169f2 100644 --- a/web/kassai_payment.py +++ b/web/kassai_payment.py @@ -1,9 +1,12 @@ import hashlib import json +import bot from aiohttp import web from database import add_payment, async_session_maker, update_balance from handlers.payments.utils import send_payment_success_notification from handlers.payments.kassai import verify_kassai_signature +from aiogram.fsm.context import FSMContext +from aiogram.fsm.storage.base import StorageKey from config import KASSAI_SECRET_KEY, KASSAI_SHOP_ID from logger import logger @@ -32,13 +35,14 @@ async def kassai_payment_webhook(request: web.Request): merchant_order_id = data_dict.get("MERCHANT_ORDER_ID") amount = data_dict.get("AMOUNT") p_email = data_dict.get("P_EMAIL") + intid = data_dict.get("intid") - if not amount: - logger.error(f"KassaAI: Missing AMOUNT") + if not amount or not intid: + logger.error(f"KassaAI: Missing AMOUNT or intid") return web.Response(status=400, text="Missing required fields") - if merchant_order_id and merchant_order_id in processed_payments: - logger.warning(f"KassaAI: Duplicate payment {merchant_order_id}") + if intid in processed_payments: + logger.warning(f"KassaAI: Duplicate payment intid={intid}") return web.Response(status=200, text="YES") try: @@ -58,10 +62,26 @@ async def kassai_payment_webhook(request: web.Request): await send_payment_success_notification(tg_id, amount_float, session) await add_payment(session, tg_id, amount_float, "kassai") - if merchant_order_id: - processed_payments.add(merchant_order_id) + try: + storage_key = StorageKey(bot_id=bot.bot.id, chat_id=tg_id, user_id=tg_id) + state_data = await bot.dp.storage.get_data(storage_key) + + if state_data and 'payment_message_id' in state_data: + payment_message_id = state_data['payment_message_id'] + try: + await bot.bot.delete_message(chat_id=tg_id, message_id=payment_message_id) + logger.info(f"Payment message deleted for user {tg_id}") + except Exception as e: + logger.warning(f"Could not delete payment message for user {tg_id}: {e}") + + await bot.dp.storage.set_state(storage_key, None) + await bot.dp.storage.set_data(storage_key, {}) + except Exception as e: + logger.warning(f"Error handling payment message deletion: {e}") - logger.info(f"✅ KassaAI: Payment processed for user {tg_id}, amount {amount_float}") + processed_payments.add(intid) + + logger.info(f"✅ KassaAI: Payment processed for user {tg_id}, amount {amount_float}, intid={intid}") return web.Response(status=200, text="YES") except Exception as e: