|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import dotenv from 'dotenv'; |
|
|
import { PaystackService } from './src/services/paystackService'; |
|
|
|
|
|
|
|
|
dotenv.config(); |
|
|
|
|
|
async function testPaystack() { |
|
|
console.log('π§ͺ Testing Paystack Integration\n'); |
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
console.log('π§ Initializing PaystackService...'); |
|
|
const paystackService = new PaystackService(); |
|
|
console.log('β
PaystackService initialized\n'); |
|
|
|
|
|
|
|
|
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'); |
|
|
} |
|
|
|
|
|
|
|
|
console.log('π³ Testing transaction initialization...'); |
|
|
const testEmail = 'test@example.com'; |
|
|
const testAmount = 1000; |
|
|
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`); |
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
testPaystack(); |
|
|
|
|
|
|