/** * 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();