File size: 2,519 Bytes
80d4bc1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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);
}
}
} |