-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-verify-endpoints.js
More file actions
211 lines (177 loc) · 7.71 KB
/
Copy pathtest-verify-endpoints.js
File metadata and controls
211 lines (177 loc) · 7.71 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
/**
* Test script to verify the difference between payment link verify and transaction verify endpoints
*/
const axios = require('axios');
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api/v1';
// Admin credentials
const ADMIN_CREDENTIALS = {
admin: process.env.TORONET_ADMIN || '',
adminpwd: process.env.TORONET_ADMIN_PWD || ''
};
async function testVerifyEndpoints() {
console.log('🧪 Testing Verify Endpoints Clarification...\n');
try {
// Step 1: Create a payment link and transaction
console.log('1. Setting up test data...');
const paymentLinkResponse = await axios.post(`${BASE_URL}/payment-links`, {
merchantId: 'test-merchant-verify',
userId: 'test-user-verify',
name: 'Verify Test Business',
amount: '50.00',
currency: 'USD',
token: 'USDT',
selectedCurrency: 'USD',
paymentType: 'card',
description: 'Testing verify endpoints'
});
const paymentLink = paymentLinkResponse.data.data;
console.log(`✅ Payment link created: ${paymentLink.id}`);
const accessResponse = await axios.post(`${BASE_URL}/payment-links/${paymentLink.id}/access`, {
payerInfo: {
payername: 'Verify Test User',
payerphone: '+1555123456',
payeremail: 'verify.test@example.com'
}
});
const transactionReference = accessResponse.data.data.transactionId;
console.log(`✅ Transaction created: ${transactionReference}`);
// Step 2: Test GET /payment-links/:id/verify (correct usage)
console.log('\n2. Testing GET /payment-links/:id/verify (payment link verification)...');
try {
const paymentLinkVerifyResponse = await axios.get(`${BASE_URL}/payment-links/${paymentLink.id}/verify`);
console.log(`✅ Payment link verification successful`);
console.log(`📋 Payment link details retrieved for: ${paymentLinkVerifyResponse.data.data.name}`);
} catch (error) {
console.error(`❌ Payment link verification failed:`, error.response?.data || error.message);
}
// Step 3: Test POST /payment-links/:id/verify (incorrect usage - should return helpful error)
console.log('\n3. Testing POST /payment-links/:id/verify (incorrect usage)...');
try {
const incorrectVerifyResponse = await axios.post(
`${BASE_URL}/payment-links/${paymentLink.id}/verify`,
{
transactionReference: transactionReference,
senderName: 'Test User',
senderEmail: 'test@example.com'
},
{
headers: {
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
console.log(`❌ Should have returned an error message`);
} catch (error) {
console.log(`✅ Correct error response received:`);
console.log(` Status: ${error.response?.status}`);
console.log(` Message: ${error.response?.data?.message}`);
console.log(` Correct Endpoint: ${error.response?.data?.correctEndpoint}`);
console.log(` Note: ${error.response?.data?.note}`);
}
// Step 4: Test POST /transactions/:reference/verify (correct usage)
console.log('\n4. Testing POST /transactions/:reference/verify (correct usage)...');
const verificationData = {
senderName: 'Verify Test User',
senderPhone: '+1555123456',
senderEmail: 'verify.test@example.com',
currency: 'USD',
txid: `TORO_VERIFY_${Date.now()}`,
paymentType: 'card',
amount: '50.00',
successUrl: 'https://webhook.site/test-verify',
paymentLinkId: paymentLink.id
};
try {
const correctVerifyResponse = await axios.post(
`${BASE_URL}/transactions/${transactionReference}/verify`,
verificationData,
{
headers: {
'Content-Type': 'application/json',
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
console.log(`✅ Transaction verification started successfully`);
console.log(`📧 Email: ${correctVerifyResponse.data.data.email}`);
console.log(`⏱️ Phase: ${correctVerifyResponse.data.data.verificationPhase}`);
console.log(`🔄 Interval: ${correctVerifyResponse.data.data.checkInterval}`);
} catch (error) {
console.error(`❌ Transaction verification failed:`, error.response?.data || error.message);
}
// Step 5: Test status endpoint
console.log('\n5. Testing transaction status endpoint...');
try {
const statusResponse = await axios.get(
`${BASE_URL}/transactions/${transactionReference}/status`,
{
headers: {
'admin': ADMIN_CREDENTIALS.admin,
'adminpwd': ADMIN_CREDENTIALS.adminpwd
}
}
);
console.log(`✅ Status check successful`);
console.log(`📊 Current state: ${statusResponse.data.data.state}`);
console.log(`🔗 Toronet reference: ${statusResponse.data.data.toronetReference}`);
} catch (error) {
console.error(`❌ Status check failed:`, error.response?.data || error.message);
}
console.log('\n🎉 Verify endpoints test completed!');
console.log('\n📋 Summary:');
console.log('✅ GET /payment-links/:id/verify - Verifies payment link details (public)');
console.log('❌ POST /payment-links/:id/verify - Returns helpful error message');
console.log('✅ POST /transactions/:reference/verify - Starts payment verification (admin)');
console.log('✅ GET /transactions/:reference/status - Checks transaction status (admin)');
console.log('\n🔍 Key Differences:');
console.log('- Payment Link Verify: Uses payment link ID, returns link details');
console.log('- Transaction Verify: Uses transaction reference, starts verification process');
console.log('- Transaction Status: Uses transaction reference, returns current status');
} catch (error) {
console.error('❌ Test failed:', error.response?.data || error.message);
}
}
// Function to demonstrate the correct workflow
async function demonstrateCorrectWorkflow() {
console.log('📚 Demonstrating Correct Verification Workflow...\n');
console.log('1. 🔗 Create Payment Link');
console.log(' POST /payment-links');
console.log(' → Returns payment link with ID and linkUrl');
console.log('\n2. 👤 User Accesses Payment Link');
console.log(' POST /payment-links/:id/access');
console.log(' → Creates transaction, returns transaction reference');
console.log('\n3. 🔍 Verify Payment Link Details (Optional)');
console.log(' GET /payment-links/:id/verify');
console.log(' → Returns payment link details for display');
console.log('\n4. 💰 User Says "I\'ve Sent Money"');
console.log(' POST /transactions/:reference/verify');
console.log(' → Starts immediate verification (3s intervals for 15 min)');
console.log('\n5. 📊 Check Transaction Status');
console.log(' GET /transactions/:reference/status');
console.log(' → Returns current transaction state and details');
console.log('\n6. 🔄 Background Verification');
console.log(' Automatic hourly checks for transactions past 15 minutes');
console.log(' → Continues until confirmed or expired (24 hours)');
console.log('\n7. ✅ Payment Confirmed');
console.log(' → Email sent to user');
console.log(' → Webhook called to merchant');
console.log(' → Transaction state updated to PAID');
}
// Run tests based on command line argument
if (require.main === module) {
const command = process.argv[2];
switch (command) {
case 'workflow':
demonstrateCorrectWorkflow();
break;
default:
testVerifyEndpoints();
break;
}
}
module.exports = {
testVerifyEndpoints,
demonstrateCorrectWorkflow
};