File size: 1,425 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
import express from 'express';
import { telegrafBots } from '../models';
import { createLogger } from '../utils/logger';

const logger = createLogger('PaymentWebhooks');
const router = express.Router();

// Get the first bot instance from the map
const bot = Array.from(telegrafBots.values())[0];

// PayPal webhook endpoint
router.post('/paypal', async (req, res) => {
  try {
    const { paymentId, status } = req.body;
    await bot.emit('webhook:paypal', { paymentId, status });
    res.json({ success: true });
  } catch (error) {
    logger.error('PayPal webhook error:', error);
    res.status(500).json({ success: false, error: error.message });
  }
});

// Crypto webhook endpoint
router.post('/crypto', async (req, res) => {
  try {
    const { paymentId, status } = req.body;
    await bot.emit('webhook:crypto', { paymentId, status });
    res.json({ success: true });
  } catch (error) {
    logger.error('Crypto webhook error:', error);
    res.status(500).json({ success: false, error: error.message });
  }
});

// Admin payment verification endpoint
router.post('/admin', async (req, res) => {
  try {
    const { paymentId, status } = req.body;
    await bot.emit('webhook:admin', { paymentId, status });
    res.json({ success: true });
  } catch (error) {
    logger.error('Admin webhook error:', error);
    res.status(500).json({ success: false, error: error.message });
  }
});

export default router;