zurri / test-paystack.ts
nexusbert's picture
push to space
4a285d2
/**
* 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();