feat: implement billing system with Pally integration
- Added billing models and repository for handling bills. - Introduced BillingService to manage payment initiation and bill creation. - Updated user service to remove ticket-related functionality. - Refactored settings to include new billing-related fields. - Removed ticket-related handlers and schemas. - Added new billing handlers for processing payments. - Updated database schema with new bills table and status enum. - Enhanced utility functions to calculate prices considering discounts. - Introduced prehandling for non-private messages. - Updated main application to initialize new billing services and repositories.
This commit is contained in:
@@ -1,23 +1,14 @@
|
||||
from aiogram import F, Router
|
||||
from aiogram.filters import Command, CommandObject
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery, Message
|
||||
from aiogram.types import CallbackQuery
|
||||
from aiogram.utils.deep_linking import create_start_link
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import settings
|
||||
from keyboards.client import close_kb, referals_kb
|
||||
from keyboards.client import referals_kb
|
||||
from misc.utils import format_share_deep_link
|
||||
from schemas.common import GeneralMessageContext
|
||||
from schemas.di import DependenciesDTO
|
||||
from states.admins import AdminStorage
|
||||
from texts import (
|
||||
GENERAL_PROCESSING,
|
||||
INVALID_ARGUMENT_REFPAY,
|
||||
REFERALS_MENU,
|
||||
SPECIFY_REFERAL_PAYMENT_AMOUNT,
|
||||
SUCCESSFUL_REFPAY,
|
||||
USER_NO_REFERAL,
|
||||
)
|
||||
|
||||
router = Router()
|
||||
@@ -37,85 +28,3 @@ async def referal_menu(
|
||||
REFERALS_MENU.format(balance=user.balance, link=referal_link),
|
||||
reply_markup=referals_kb(share_link),
|
||||
)
|
||||
|
||||
|
||||
@router.message(F.chat.id == settings.admin_group_id, Command("refpay"))
|
||||
async def refpay(
|
||||
msg: Message,
|
||||
command: CommandObject,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
deps: DependenciesDTO,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
await msg.delete()
|
||||
status_msg = await msg.answer(GENERAL_PROCESSING)
|
||||
user = await deps.user_service.fetch_user_by_thread(session, msg.message_thread_id)
|
||||
referal = await deps.user_repository.get_user_by_id(session, user.referal_id)
|
||||
if not referal:
|
||||
await status_msg.edit_text(USER_NO_REFERAL)
|
||||
return
|
||||
|
||||
amount = command.args
|
||||
|
||||
if not amount.isdigit():
|
||||
await status_msg.edit_text(INVALID_ARGUMENT_REFPAY)
|
||||
return
|
||||
|
||||
amount = int(amount)
|
||||
await deps.user_repository.increase_user_balance(session, referal.id, amount)
|
||||
await status_msg.edit_text(SUCCESSFUL_REFPAY.format(referal_id=referal.id, amount=amount))
|
||||
|
||||
|
||||
@router.callback_query(F.message.chat.id == settings.admin_group_id, F.data == "refpay")
|
||||
async def refpay_cb(
|
||||
cb: CallbackQuery,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
deps: DependenciesDTO,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
user = await deps.user_service.fetch_user_by_thread(session, cb.message.message_thread_id)
|
||||
referal = await deps.user_repository.get_user_by_id(session, user.referal_id)
|
||||
|
||||
if not referal:
|
||||
await cb.message.answer(USER_NO_REFERAL, reply_markup=close_kb)
|
||||
return
|
||||
|
||||
msg = await cb.message.answer(SPECIFY_REFERAL_PAYMENT_AMOUNT, reply_markup=close_kb)
|
||||
ctx = GeneralMessageContext(msg=msg)
|
||||
await state.set_state(AdminStorage.refpay_amount)
|
||||
await state.set_data({"ctx": ctx})
|
||||
|
||||
|
||||
@router.message(
|
||||
AdminStorage.refpay_amount,
|
||||
F.chat.id == settings.admin_group_id,
|
||||
F.from_user.is_bot == False, # noqa: E712
|
||||
)
|
||||
async def refpay_execute(
|
||||
msg: Message, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
|
||||
):
|
||||
data = await state.get_data()
|
||||
await state.clear()
|
||||
await msg.delete()
|
||||
|
||||
ctx: GeneralMessageContext | None = data.get("ctx")
|
||||
if ctx:
|
||||
status_msg = ctx.msg
|
||||
else:
|
||||
status_msg = await msg.answer(GENERAL_PROCESSING)
|
||||
|
||||
amount = msg.text
|
||||
if not amount.isdigit():
|
||||
await status_msg.edit_text(INVALID_ARGUMENT_REFPAY, reply_markup=close_kb)
|
||||
return
|
||||
|
||||
user = await deps.user_service.fetch_user_by_thread(session, msg.message_thread_id)
|
||||
referal = await deps.user_repository.get_user_by_id(session, user.referal_id)
|
||||
|
||||
amount = int(amount)
|
||||
await deps.user_repository.increase_user_balance(session, referal.id, amount)
|
||||
await status_msg.edit_text(SUCCESSFUL_REFPAY.format(referal_id=referal.id, amount=amount))
|
||||
|
||||
Reference in New Issue
Block a user