File size: 1,961 Bytes
e3fefe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { dbGet, dbSet, dbUpdate, dbGetAll } = require('../config/firebase');
const { v4: uuidv4 } = require('uuid');

const PAYMENTS_PATH = 'payments';
const EASYPAISA_NUMBER = process.env.EASYPAISA_ACCOUNT_NUMBER || '03455876761';

const PaymentModel = {
  async create({ bookingId, customerId, professionalId, amount, commission }) {
    const paymentId = uuidv4();
    const payment = {
      paymentId,
      bookingId,
      customerId,
      professionalId,
      amount,
      commission: commission || 0,
      easypaisaNumber: EASYPAISA_NUMBER,
      transactionId: '',
      status: 'initiated',
      _createdAt: Date.now(),
      confirmedAt: 0,
    };
    await dbSet(`${PAYMENTS_PATH}/${paymentId}`, payment);
    return payment;
  },

  /**
   * Simulate EasyPaisa verification.
   * Replace with real EasyPaisa Checkout API in production.
   */
  async verifyEasypaisa(transactionId) {
    if (!transactionId || transactionId.trim().length < 3) {
      return { verified: false, message: 'Please enter a valid transaction ID (minimum 3 characters).' };
    }
    // Simulate processing delay
    await new Promise(resolve => setTimeout(resolve, 1500));
    return {
      verified: true,
      message: '✅ Payment verified successfully!',
      transactionId: transactionId.trim(),
    };
  },

  async confirm(paymentId, transactionId) {
    await dbUpdate(`${PAYMENTS_PATH}/${paymentId}`, {
      transactionId,
      status: 'completed',
      confirmedAt: Date.now(),
    });
  },

  async getByBookingId(bookingId) {
    const all = await dbGetAll(PAYMENTS_PATH);
    const matches = all.filter(p => p.bookingId === bookingId);
    if (!matches.length) return null;
    return matches.sort((a, b) => (b._createdAt || 0) - (a._createdAt || 0))[0];
  },

  async markFailed(paymentId) {
    await dbUpdate(`${PAYMENTS_PATH}/${paymentId}`, {
      status: 'failed',
      failedAt: Date.now(),
    });
  },
};

module.exports = PaymentModel;