Skip to content

Commit

Permalink
feat(payments): add deletion of successful payment
Browse files Browse the repository at this point in the history
  • Loading branch information
exmanka committed Jan 31, 2024
1 parent a20bcf7 commit 2243709
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/database/postgres_dbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ async def get_payment_status(payment_id: int) -> bool | None:
payment_id)


async def get_payment_telegram_message_id(payment_id: int) -> int | None:
"""Return telegram message id for specified payment_id."""
return await conn.fetchval(
'''
SELECT telegram_message_id
FROM payments
WHERE id = $1;
''',
payment_id)


async def get_payment_months_number(payment_id: int) -> int | None:
"""Return paid number of months for specified payment_id."""
return await conn.fetchval(
Expand Down
36 changes: 28 additions & 8 deletions src/handlers/user_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,21 @@ async def sub_renewal_verification(message: Message, state: FSMContext):
await state.set_state(user_authorized_fsm.PaymentMenu.menu)
await message.answer(loc.auth.msgs['payment_found'].format(payment_id), parse_mode='HTML', reply_markup=user_authorized_kb.sub_renewal)

# notify admin about successful payment and check referral reward for other client
await internal_functions.notify_admin_payment_success(client_id, months_number)
await internal_functions.check_referral_reward(client_id)
# try to delete payment message
message_id = await postgres_dbms.get_payment_telegram_message_id(payment_id)
try:
await bot.delete_message(message.chat.id, message_id)

# if already deleted
except MessageToDeleteNotFound as _t:
pass

finally:
# notify admin about successful payment and check referral reward for other client
await internal_functions.notify_admin_payment_success(client_id, months_number)
await internal_functions.check_referral_reward(client_id)

is_payment_found = True
is_payment_found = True

if not is_payment_found:
await message.answer(loc.auth.msgs['cant_find_payments'])
Expand Down Expand Up @@ -648,11 +658,21 @@ async def restore_payments(message: Message):
# answer to a client
await message.answer(loc.auth.msgs['payment_found'].format(payment_id), parse_mode='HTML')

# notify admin about successful payment and check referral reward for other client
await internal_functions.notify_admin_payment_success(client_id, months_number)
await internal_functions.check_referral_reward(client_id)
# try to delete payment message
message_id = await postgres_dbms.get_payment_telegram_message_id(payment_id)
try:
await bot.delete_message(message.chat.id, message_id)

# if already deleted
except MessageToDeleteNotFound as _t:
pass

finally:
# notify admin about successful payment and check referral reward for other client
await internal_functions.notify_admin_payment_success(client_id, months_number)
await internal_functions.check_referral_reward(client_id)

is_payment_found = True
is_payment_found = True

if not is_payment_found:
await message.answer(loc.auth.msgs['cant_find_payments_restore'])
Expand Down

0 comments on commit 2243709

Please sign in to comment.