bot-me / src /bots /services /PaymentVerificationService.ts
Mohammed Foud
allh
80d4bc1
import { createLogger } from '../../utils/logger';
import { supabase } from '../../db/supabase';
import { AuthService } from './auth';
import { Telegraf } from 'telegraf';
const logger = createLogger('PaymentVerificationService');
export class PaymentVerificationService {
private static instance: PaymentVerificationService;
private bot: Telegraf;
private constructor() {}
public static getInstance(): PaymentVerificationService {
if (!PaymentVerificationService.instance) {
PaymentVerificationService.instance = new PaymentVerificationService();
}
return PaymentVerificationService.instance;
}
public setBot(bot: Telegraf) {
this.bot = bot;
}
async verifyAndUpdatePayment(paymentId: string, status: 'COMPLETED' | 'FAILED') {
try {
// Get payment details
const { data: payment, error: paymentError } = await supabase
.from('payments')
.select('*, users_bot_telegram!inner(telegram_id, balance)')
.eq('id', paymentId)
.single();
if (paymentError) throw paymentError;
if (!payment) throw new Error('Payment not found');
// Update payment status
const { error: updateError } = await supabase
.from('payments')
.update({
status: status,
completed_at: status === 'COMPLETED' ? new Date().toISOString() : null
})
.eq('id', paymentId);
if (updateError) throw updateError;
if (status === 'COMPLETED') {
// Update user balance
const { error: balanceError } = await supabase
.from('users_bot_telegram')
.update({
balance: payment.users_bot_telegram.balance + payment.amount
})
.eq('id', payment.user_id);
if (balanceError) throw balanceError;
// Notify user
await this.notifyUser(payment.users_bot_telegram.telegram_id, payment.amount);
}
return true;
} catch (error) {
logger.error('Error verifying payment:', error);
throw error;
}
}
private async notifyUser(telegramId: number, amount: number) {
try {
await this.bot.telegram.sendMessage(
telegramId,
`✅ <b>تم إتمام عملية الدفع بنجاح!</b>\n\n` +
`تم إضافة ${amount}$ إلى رصيدك.\n` +
`يمكنك الآن استخدام الخدمات المتاحة.`,
{ parse_mode: 'HTML' }
);
} catch (error) {
logger.error('Error notifying user:', error);
}
}
}