Update handleBuyProduct

This commit is contained in:
NW 2024-12-14 12:58:38 +00:00
parent eea5d9b9e7
commit 3843dcb094

View File

@ -494,30 +494,46 @@ Subcategory: ${product.subcategory_name}
const telegramId = callbackQuery.from.id;
const productId = callbackQuery.data.replace('buy_product_', '');
const state = userStates.get(chatId);
try {
const user = await UserService.getUserByTelegramId(telegramId)
const user = await UserService.getUserByTelegramId(telegramId);
if (!user) {
throw new Error('User not found');
}
const product = await ProductService.getProductById(productId);
if (!product) {
throw new Error('Product not found');
}
const quantity = state?.quantity || 1;
const totalPrice = product.price * quantity;
// Get user's crypto wallets with balances
// Проверка баланса пользователя
const userBalance = await UserService.getUserBalance(user.id);
if (userBalance < totalPrice) {
await bot.sendMessage(
chatId,
`❌ Insufficient balance. Your current balance is $${userBalance}. You need $${totalPrice} to complete this purchase.`,
{
reply_markup: {
inline_keyboard: [[
{ text: '💰 Top Up Balance', callback_data: 'top_up_wallet' }
]]
}
}
);
return;
}
// Получение криптокошельков пользователя
const cryptoWallets = await db.allAsync(`
SELECT wallet_type, address
FROM crypto_wallets
WHERE user_id = ?
ORDER BY wallet_type
`, [user.id]);
SELECT wallet_type, address
FROM crypto_wallets
WHERE user_id = ?
ORDER BY wallet_type
`, [user.id]);
if (cryptoWallets.length === 0) {
await bot.sendMessage(
chatId,
@ -525,21 +541,21 @@ Subcategory: ${product.subcategory_name}
{
reply_markup: {
inline_keyboard: [[
{text: ' Add Wallet', callback_data: 'add_wallet'}
{ text: ' Add Wallet', callback_data: 'add_wallet' }
]]
}
}
);
return;
}
const keyboard = {
inline_keyboard: [
[{ text: `Pay`, callback_data: `pay_with_main_${productId}_${quantity}` }],
[{text: '« Cancel', callback_data: `shop_product_${productId}`}]
[{ text: '« Cancel', callback_data: `shop_product_${productId}` }]
]
};
await bot.editMessageText(
`🛒 Purchase Summary:\n\n` +
`Product: ${product.name}\n` +