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 telegramId = callbackQuery.from.id;
const productId = callbackQuery.data.replace('buy_product_', ''); const productId = callbackQuery.data.replace('buy_product_', '');
const state = userStates.get(chatId); const state = userStates.get(chatId);
try { try {
const user = await UserService.getUserByTelegramId(telegramId) const user = await UserService.getUserByTelegramId(telegramId);
if (!user) { if (!user) {
throw new Error('User not found'); throw new Error('User not found');
} }
const product = await ProductService.getProductById(productId); const product = await ProductService.getProductById(productId);
if (!product) { if (!product) {
throw new Error('Product not found'); throw new Error('Product not found');
} }
const quantity = state?.quantity || 1; const quantity = state?.quantity || 1;
const totalPrice = product.price * quantity; 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(` const cryptoWallets = await db.allAsync(`
SELECT wallet_type, address SELECT wallet_type, address
FROM crypto_wallets FROM crypto_wallets
WHERE user_id = ? WHERE user_id = ?
ORDER BY wallet_type ORDER BY wallet_type
`, [user.id]); `, [user.id]);
if (cryptoWallets.length === 0) { if (cryptoWallets.length === 0) {
await bot.sendMessage( await bot.sendMessage(
chatId, chatId,
@ -525,21 +541,21 @@ Subcategory: ${product.subcategory_name}
{ {
reply_markup: { reply_markup: {
inline_keyboard: [[ inline_keyboard: [[
{text: ' Add Wallet', callback_data: 'add_wallet'} { text: ' Add Wallet', callback_data: 'add_wallet' }
]] ]]
} }
} }
); );
return; return;
} }
const keyboard = { const keyboard = {
inline_keyboard: [ inline_keyboard: [
[{ text: `Pay`, callback_data: `pay_with_main_${productId}_${quantity}` }], [{ 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( await bot.editMessageText(
`🛒 Purchase Summary:\n\n` + `🛒 Purchase Summary:\n\n` +
`Product: ${product.name}\n` + `Product: ${product.name}\n` +