-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-background-verification.js
More file actions
215 lines (179 loc) · 8.48 KB
/
Copy pathtest-background-verification.js
File metadata and controls
215 lines (179 loc) · 8.48 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
/**
* Test script for background payment verification system
*/
const axios = require('axios');
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api/v1';
async function testBackgroundVerification() {
console.log('🧪 Testing Background Payment 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-bg',
userId: 'test-user-bg',
name: 'Background Test Business',
amount: '100.00',
currency: 'USD',
token: 'USDT',
selectedCurrency: 'USD',
paymentType: 'card',
description: 'Testing background verification',
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: 'Test User Background',
payerphone: '+1234567890',
payeremail: 'test.background@example.com'
}
});
const transactionId = accessResponse.data.data.transactionId;
console.log(`✅ Transaction created: ${transactionId}`);
// Step 3: Get transaction details to verify it's in PENDING state
console.log('\n3. Checking initial transaction state...');
const transactionResponse = await axios.get(`${BASE_URL}/transactions/${transactionId}`);
const transaction = transactionResponse.data.data;
console.log(`📋 Transaction Details:`);
console.log(` ID: ${transaction.id}`);
console.log(` State: ${transaction.state}`);
console.log(` Amount: ${transaction.amount} ${transaction.currency}`);
console.log(` Expires At: ${transaction.expiresAt || 'Not set'}`);
console.log(` Last Check: ${transaction.lastVerificationCheck || 'Never'}`);
if (transaction.state !== 'PENDING') {
console.log(`⚠️ Expected PENDING state, got ${transaction.state}`);
}
// Step 4: Test record transaction with email
console.log('\n4. Testing record transaction with email...');
const recordResponse = await axios.post(`${BASE_URL}/record-transaction/${transactionId}`, {
amount: '100.00',
currency: 'USD',
senderName: 'Test User Background',
senderPhone: '+1234567890',
senderEmail: 'test.background@example.com',
paidAt: new Date().toISOString()
});
console.log(`✅ Transaction recorded successfully`);
console.log(`📧 Email should be saved in payerInfo.email`);
// Step 5: Verify the email was saved
console.log('\n5. Verifying email was saved...');
const updatedTransactionResponse = await axios.get(`${BASE_URL}/transactions/${transactionId}`);
const updatedTransaction = updatedTransactionResponse.data.data;
console.log(`📧 Payer Email: ${updatedTransaction.payerInfo?.email || 'Not found'}`);
console.log(`👤 Payer Name: ${updatedTransaction.payerInfo?.name || 'Not found'}`);
console.log(`📱 Payer Phone: ${updatedTransaction.payerInfo?.phone || 'Not found'}`);
console.log(`🏷️ Final State: ${updatedTransaction.state}`);
// Step 6: Test getting transactions by state
console.log('\n6. Testing get transactions by state...');
const pendingResponse = await axios.get(`${BASE_URL}/transactions?state=PENDING`);
const completedResponse = await axios.get(`${BASE_URL}/transactions?state=COMPLETED`);
console.log(`📊 PENDING transactions: ${pendingResponse.data.data.total}`);
console.log(`📊 COMPLETED transactions: ${completedResponse.data.data.total}`);
// Step 7: Create a transaction that will remain PENDING for background verification
console.log('\n7. Creating transaction for background verification test...');
const testAccessResponse = await axios.post(`${BASE_URL}/payment-links/${paymentLink.id}/access`, {
payerInfo: {
payername: 'Background Verification Test',
payerphone: '+1987654321',
payeremail: 'background.test@example.com'
}
});
const testTransactionId = testAccessResponse.data.data.transactionId;
console.log(`✅ Test transaction created: ${testTransactionId}`);
console.log(`⏳ This transaction will remain PENDING for background verification`);
console.log(`🔍 The background service will check this every 5 minutes`);
console.log('\n🎉 Background verification test completed successfully!');
console.log('\n📋 Summary:');
console.log(`- Payment link created: ${paymentLink.id}`);
console.log(`- Completed transaction: ${transactionId}`);
console.log(`- Pending transaction for verification: ${testTransactionId}`);
console.log(`- Email functionality tested and working`);
console.log('\n🔍 Background Verification Process:');
console.log('- Service runs every 5 minutes');
console.log('- Checks PENDING transactions with Toronet API');
console.log('- Updates transaction state when payment confirmed');
console.log('- Sends confirmation emails to payers');
console.log('- Notifies merchant webhooks');
console.log('- Handles expired transactions (24 hours)');
console.log('\n📧 Email Configuration Required:');
console.log('- Set SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS in .env');
console.log('- For Gmail: Enable 2FA and generate App Password');
console.log('- Set SMTP_FROM and SUPPORT_EMAIL');
} catch (error) {
console.error('❌ Test failed:', error.response?.data || error.message);
}
}
// Function to test email service configuration
async function testEmailConfiguration() {
console.log('📧 Testing Email Service Configuration...\n');
const requiredEnvVars = [
'SMTP_HOST',
'SMTP_PORT',
'SMTP_USER',
'SMTP_PASS',
'SMTP_FROM',
'SUPPORT_EMAIL'
];
console.log('🔍 Checking environment variables:');
for (const envVar of requiredEnvVars) {
const value = process.env[envVar];
console.log(`${envVar}: ${value ? '✅ Set' : '❌ Missing'}`);
}
console.log('\n📋 Gmail Setup Instructions:');
console.log('1. Enable 2-Factor Authentication on your Gmail account');
console.log('2. Go to https://myaccount.google.com/apppasswords');
console.log('3. Generate an App Password for "Mail"');
console.log('4. Use the generated password as SMTP_PASS');
console.log('5. Set SMTP_USER to your Gmail address');
}
// Function to monitor background verification
async function monitorBackgroundVerification() {
console.log('👀 Monitoring Background Verification...\n');
try {
// Get all pending transactions
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:');
pendingTransactions.forEach((tx, index) => {
console.log(`${index + 1}. Transaction ${tx.id}`);
console.log(` Amount: ${tx.amount} ${tx.currency}`);
console.log(` Created: ${new Date(tx.createdAt).toLocaleString()}`);
console.log(` Expires: ${tx.expiresAt ? new Date(tx.expiresAt).toLocaleString() : 'Not set'}`);
console.log(` Last Check: ${tx.lastVerificationCheck ? new Date(tx.lastVerificationCheck).toLocaleString() : 'Never'}`);
console.log(` Email: ${tx.payerInfo?.email || 'Not provided'}`);
console.log('');
});
console.log('🔍 Background verification will process these transactions every 5 minutes');
console.log('⏰ Transactions expire after 24 hours if payment not confirmed');
} catch (error) {
console.error('❌ Failed to monitor background verification:', error.response?.data || error.message);
}
}
// Run tests based on command line argument
if (require.main === module) {
const command = process.argv[2];
switch (command) {
case 'email':
testEmailConfiguration();
break;
case 'monitor':
monitorBackgroundVerification();
break;
default:
testBackgroundVerification();
break;
}
}
module.exports = {
testBackgroundVerification,
testEmailConfiguration,
monitorBackgroundVerification
};