-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-immediate-verification.js
More file actions
370 lines (318 loc) Β· 14.2 KB
/
Copy pathtest-immediate-verification.js
File metadata and controls
370 lines (318 loc) Β· 14.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* Test script for the new immediate + background verification system
*/
const axios = require('axios');
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api/v1';
// Admin credentials for verification endpoint
const ADMIN_CREDENTIALS = {
admin: process.env.TORONET_ADMIN || '',
adminpwd: process.env.TORONET_ADMIN_PWD || ''
};
async function testImmediateVerification() {
console.log('π§ͺ Testing Immediate + Background Verification System...\n');
try {
// Step 1: Create a payment link
console.log('1. Creating test payment link...');
const paymentLinkResponse = await axios.post(`${BASE_URL}/payment-links`, {
merchantId: 'test-merchant-immediate',
userId: 'test-user-immediate',
name: 'Immediate Verification Test',
amount: '150.00',
currency: 'USD',
token: 'USDT',
selectedCurrency: 'USD',
paymentType: 'card',
description: 'Testing immediate verification system',
successUrl: 'https://webhook.site/your-webhook-url-here'
});
const paymentLink = paymentLinkResponse.data.data;
console.log(`β
Payment link created: ${paymentLink.id}`);
// Step 2: Access the payment link to create a transaction
console.log('\n2. Accessing payment link to create transaction...');
const accessResponse = await axios.post(`${BASE_URL}/payment-links/${paymentLink.id}/access`, {
payerInfo: {
payername: 'Immediate Test User',
payerphone: '+1234567890',
payeremail: 'immediate.test@example.com'
}
});
const transactionId = accessResponse.data.data.transactionId;
const transactionReference = accessResponse.data.data.transactionId; // This should be the reference
console.log(`β
Transaction created: ${transactionId}`);
console.log(`π Transaction reference: ${transactionReference}`);
// Step 3: Get initial transaction details
console.log('\n3. Checking initial transaction state...');
const initialTransactionResponse = await axios.get(`${BASE_URL}/transactions/${transactionId}`);
const initialTransaction = initialTransactionResponse.data.data;
console.log(`π Initial Transaction Details:`);
console.log(` ID: ${initialTransaction.id}`);
console.log(` Reference: ${initialTransaction.reference}`);
console.log(` State: ${initialTransaction.state}`);
console.log(` Amount: ${initialTransaction.amount} ${initialTransaction.currency}`);
// Step 4: Start immediate verification
console.log('\n4. Starting immediate verification...');
const verificationData = {
senderName: 'Immediate Test User',
senderPhone: '+1234567890',
senderEmail: 'immediate.test@example.com',
currency: 'USD',
txid: `TORO_${Date.now()}_TEST`, // Mock Toronet reference
paymentType: 'card',
amount: '150.00',
successUrl: 'https://webhook.site/your-webhook-url-here',
paymentLinkId: paymentLink.id
};
const verifyResponse = await axios.post(
`${BASE_URL}/transactions/${transactionId}/verify`,
verificationData,
{
headers: {
'Content-Type': 'application/json',
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
console.log(`β
Verification started successfully`);
console.log(`π§ Email: ${verifyResponse.data.data.email}`);
console.log(`β±οΈ Phase: ${verifyResponse.data.data.verificationPhase}`);
console.log(`π Interval: ${verifyResponse.data.data.checkInterval}`);
console.log(`β° Duration: ${verifyResponse.data.data.duration}`);
// Step 5: Monitor transaction state changes
console.log('\n5. Monitoring transaction state (will check for 2 minutes)...');
const monitoringDuration = 2 * 60 * 1000; // 2 minutes
const checkInterval = 10 * 1000; // 10 seconds
const startTime = Date.now();
const monitorInterval = setInterval(async () => {
const elapsed = Date.now() - startTime;
if (elapsed >= monitoringDuration) {
clearInterval(monitorInterval);
console.log('\nβ±οΈ Monitoring period ended');
return;
}
try {
// Test the new status endpoint
const statusResponse = await axios.get(
`${BASE_URL}/transactions/${transactionId}/status`,
{
headers: {
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
const statusData = statusResponse.data.data;
console.log(`π [${Math.floor(elapsed/1000)}s] Status: ${statusData.state}, Last Check: ${statusData.lastVerificationCheck || 'Never'}`);
if (statusData.state !== 'PENDING') {
clearInterval(monitorInterval);
console.log(`\nπ Transaction state changed to: ${statusData.state}`);
if (statusData.state === 'PAID') {
console.log(`β
Payment confirmed!`);
console.log(`π§ Confirmation email should be sent to: ${statusData.senderEmail}`);
console.log(`π€ Webhook should be called to: ${verificationData.successUrl}`);
}
}
} catch (error) {
console.error(`β Error monitoring transaction:`, error.message);
}
}, checkInterval);
// Step 6: Test the status endpoint specifically
setTimeout(async () => {
console.log('\n6. Testing status endpoint...');
try {
const statusResponse = await axios.get(
`${BASE_URL}/transactions/${transactionId}/status`,
{
headers: {
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
console.log(`β
Status endpoint test successful`);
console.log(`π Transaction Status Details:`);
const status = statusResponse.data.data;
console.log(` Transaction ID: ${status.transactionId}`);
console.log(` State: ${status.state}`);
console.log(` Amount: ${status.amount} ${status.currency}`);
console.log(` Sender: ${status.senderName} (${status.senderEmail})`);
console.log(` Toronet Ref: ${status.toronetReference || 'Not set'}`);
console.log(` Verification Started: ${status.verificationStartedAt || 'Not started'}`);
console.log(` Last Check: ${status.lastVerificationCheck || 'Never'}`);
console.log(` Expires: ${status.expiresAt || 'Not set'}`);
} catch (error) {
console.error(`β Status endpoint test failed:`, error.response?.data || error.message);
}
}, 20000); // Wait 20 seconds
// Step 7: Test error cases
// Step 7: Test error cases
setTimeout(async () => {
console.log('\n7. Testing error cases...');
// Test status endpoint with invalid transaction reference
try {
await axios.get(
`${BASE_URL}/transactions/invalid-reference/status`,
{
headers: {
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
} catch (error) {
console.log(`β
Status endpoint invalid reference test: ${error.response?.status} - ${error.response?.data?.message}`);
}
// Test status endpoint without admin credentials
try {
await axios.get(`${BASE_URL}/transactions/${initialTransaction.reference}/status`);
} catch (error) {
console.log(`β
Status endpoint missing credentials test: ${error.response?.status} - ${error.response?.data?.message}`);
}
// Test verify endpoint with invalid transaction reference
try {
await axios.post(
`${BASE_URL}/transactions/invalid-transaction-id/verify`,
verificationData,
{
headers: {
'Content-Type': 'application/json',
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
} catch (error) {
console.log(`β
Verify endpoint invalid ID test: ${error.response?.status} - ${error.response?.data?.message}`);
}
// Test verify endpoint without admin credentials
try {
await axios.post(
`${BASE_URL}/transactions/${transactionId}/verify`,
verificationData
);
} catch (error) {
console.log(`β
Verify endpoint missing credentials test: ${error.response?.status} - ${error.response?.data?.message}`);
}
// Test verify endpoint with invalid admin credentials
try {
await axios.post(
`${BASE_URL}/transactions/${transactionId}/verify`,
verificationData,
{
headers: {
'Content-Type': 'application/json',
'admin': 'invalid',
'adminpwd': 'invalid'
}
}
);
} catch (error) {
console.log(`β
Verify endpoint invalid credentials test: ${error.response?.status} - ${error.response?.data?.message}`);
}
}, 35000); // Wait 35 seconds before testing error cases
console.log('\nπ Verification System Summary:');
console.log('π Phase 1: Immediate verification (0-15 min, every 3 seconds)');
console.log('π Phase 2: Background verification (15 min - 24 hours, every 1 hour)');
console.log('β° Phase 3: Expiration handling (after 24 hours)');
console.log('π§ Email notifications on confirmation and expiration');
console.log('π€ Webhook notifications to merchant success URLs');
} catch (error) {
console.error('β Test failed:', error.response?.data || error.message);
}
}
// Function to test background verification monitoring
async function testBackgroundVerificationMonitoring() {
console.log('π Testing Background Verification Monitoring...\n');
try {
// Get all pending transactions that should be in background verification
const response = await axios.get(`${BASE_URL}/transactions?state=PENDING`);
const pendingTransactions = response.data.data.transactions;
console.log(`π Found ${pendingTransactions.length} PENDING transactions`);
if (pendingTransactions.length === 0) {
console.log('β
No pending transactions to monitor');
return;
}
console.log('\nπ Pending Transactions Analysis:');
pendingTransactions.forEach((tx, index) => {
const now = new Date();
const created = new Date(tx.createdAt);
const verificationStarted = tx.verificationStartedAt ? new Date(tx.verificationStartedAt) : null;
const expires = tx.expiresAt ? new Date(tx.expiresAt) : null;
const lastCheck = tx.lastVerificationCheck ? new Date(tx.lastVerificationCheck) : null;
const ageMinutes = Math.floor((now - created) / (1000 * 60));
const verificationAgeMinutes = verificationStarted ? Math.floor((now - verificationStarted) / (1000 * 60)) : null;
const hoursUntilExpiry = expires ? Math.floor((expires - now) / (1000 * 60 * 60)) : null;
console.log(`\n${index + 1}. Transaction ${tx.id}`);
console.log(` Reference: ${tx.reference}`);
console.log(` Amount: ${tx.amount} ${tx.currency}`);
console.log(` Age: ${ageMinutes} minutes`);
console.log(` Verification Started: ${verificationStarted ? `${verificationAgeMinutes} minutes ago` : 'Not started'}`);
console.log(` Last Check: ${lastCheck ? lastCheck.toLocaleString() : 'Never'}`);
console.log(` Expires: ${expires ? `in ${hoursUntilExpiry} hours` : 'Not set'}`);
console.log(` Email: ${tx.payerInfo?.email || 'Not provided'}`);
console.log(` Toronet Ref: ${tx.toronetReference || 'Not set'}`);
// Determine verification phase
if (!verificationStarted) {
console.log(` Phase: β³ Not started`);
} else if (verificationAgeMinutes < 15) {
console.log(` Phase: π Immediate verification (${15 - verificationAgeMinutes} min remaining)`);
} else {
console.log(` Phase: π Background verification (hourly checks)`);
}
});
console.log('\nπ Verification Process:');
console.log('- Immediate: Every 3 seconds for 15 minutes after verification starts');
console.log('- Background: Every 1 hour for transactions past 15-minute mark');
console.log('- Expiration: After 24 hours, transaction marked as failed');
} catch (error) {
console.error('β Failed to monitor background verification:', error.response?.data || error.message);
}
}
// Function to test verification service status
async function testVerificationServiceStatus() {
console.log('π§ Testing Verification Service Status...\n');
try {
// Test health endpoint
const healthResponse = await axios.get(`${BASE_URL}/health`);
console.log(`β
API Health: ${healthResponse.data.message}`);
// Get transaction counts by state
const states = ['PENDING', 'INITIALIZED', 'PAID', 'COMPLETED', 'PAYOUT_FAILED'];
console.log('\nπ Transaction Counts by State:');
for (const state of states) {
try {
const response = await axios.get(`${BASE_URL}/transactions?state=${state}`);
console.log(`${state}: ${response.data.data.total} transactions`);
} catch (error) {
console.log(`${state}: Error - ${error.message}`);
}
}
console.log('\nπ Service Configuration:');
console.log(`- Immediate verification: 3-second intervals for 15 minutes`);
console.log(`- Background verification: 1-hour intervals`);
console.log(`- Transaction expiration: 24 hours`);
console.log(`- Email service: ${process.env.SMTP_HOST ? 'Configured' : 'Not configured'}`);
console.log(`- Toronet admin: ${ADMIN_CREDENTIALS.admin ? 'Set' : 'Not set'}`);
} catch (error) {
console.error('β Service status check failed:', error.response?.data || error.message);
}
}
// Run tests based on command line argument
if (require.main === module) {
const command = process.argv[2];
switch (command) {
case 'monitor':
testBackgroundVerificationMonitoring();
break;
case 'status':
testVerificationServiceStatus();
break;
default:
testImmediateVerification();
break;
}
}
module.exports = {
testImmediateVerification,
testBackgroundVerificationMonitoring,
testVerificationServiceStatus
};