File size: 4,349 Bytes
4a285d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * Paystack Integration Test Script
 * Run with: npx ts-node test-paystack.ts
 */

import dotenv from 'dotenv';
import { PaystackService } from './src/services/paystackService';

// Load environment variables
dotenv.config();

async function testPaystack() {
  console.log('πŸ§ͺ Testing Paystack Integration\n');

  // Check environment variables
  console.log('πŸ“‹ Checking environment variables...');
  const secretKey = process.env.PAYSTACK_SECRET_KEY;
  const publicKey = process.env.PAYSTACK_PUBLIC_KEY;

  if (!secretKey) {
    console.error('❌ PAYSTACK_SECRET_KEY is not set in .env');
    process.exit(1);
  }

  if (!publicKey) {
    console.error('❌ PAYSTACK_PUBLIC_KEY is not set in .env');
    process.exit(1);
  }

  console.log('βœ… PAYSTACK_SECRET_KEY is set');
  console.log('βœ… PAYSTACK_PUBLIC_KEY is set');
  console.log(`   Using ${secretKey.startsWith('sk_live') ? 'LIVE' : 'TEST'} keys\n`);

  try {
    // Initialize PaystackService
    console.log('πŸ”§ Initializing PaystackService...');
    const paystackService = new PaystackService();
    console.log('βœ… PaystackService initialized\n');

    // Test public key retrieval
    console.log('πŸ”‘ Testing public key retrieval...');
    const retrievedKey = paystackService.getPublicKey();
    if (retrievedKey === publicKey) {
      console.log('βœ… Public key matches\n');
    } else {
      console.log('⚠️  Public key mismatch (this might be okay)\n');
    }

    // Test transaction initialization (with test data)
    console.log('πŸ’³ Testing transaction initialization...');
    const testEmail = 'test@example.com';
    const testAmount = 1000; // 1000 NGN = 10 NGN (1000 kobo)
    const testReference = `test_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;

    console.log(`   Email: ${testEmail}`);
    console.log(`   Amount: ${testAmount} kobo (${testAmount / 100} NGN)`);
    console.log(`   Reference: ${testReference}`);

    try {
      const initResult = await paystackService.initializeTransaction({
        email: testEmail,
        amount: testAmount,
        reference: testReference,
        metadata: {
          userId: 'test-user-id',
          test: true,
        },
        callback_url: 'http://localhost:3000/payment/callback',
      });

      console.log('βœ… Transaction initialized successfully');
      console.log(`   Authorization URL: ${initResult.authorization_url}`);
      console.log(`   Access Code: ${initResult.access_code}`);
      console.log(`   Reference: ${initResult.reference}\n`);

      // Test verification (this will fail for test reference, but tests the API call)
      console.log('πŸ” Testing transaction verification...');
      try {
        const verifyResult = await paystackService.verifyTransaction(testReference);
        console.log('βœ… Verification API call successful');
        console.log(`   Status: ${verifyResult.status}`);
        console.log(`   Amount: ${verifyResult.amount}`);
        console.log(`   Reference: ${verifyResult.reference}\n`);
      } catch (verifyError: any) {
        if (verifyError.message.includes('not found') || verifyError.message.includes('No transaction')) {
          console.log('⚠️  Transaction not found (expected for test reference)');
          console.log('   This is normal - verification will work after actual payment\n');
        } else {
          throw verifyError;
        }
      }
    } catch (initError: any) {
      console.error('❌ Transaction initialization failed:', initError.message);
      if (initError.message.includes('Invalid key')) {
        console.error('   Check that your PAYSTACK_SECRET_KEY is correct');
      }
      process.exit(1);
    }

    console.log('πŸŽ‰ All Paystack service tests passed!\n');
    console.log('πŸ“ Next steps:');
    console.log('   1. Start the server: npm run dev');
    console.log('   2. Test the API endpoints:');
    console.log('      - POST /api/wallet/fund (with user auth token)');
    console.log('      - GET /api/wallet/verify/:reference');
    console.log('   3. Use test cards: 4084084084084081 (success)');
    console.log('   4. Check PAYMENT_TEST.md for frontend integration examples\n');

  } catch (error: any) {
    console.error('❌ Test failed:', error.message);
    console.error(error);
    process.exit(1);
  }
}

// Run tests
testPaystack();