nets-service-sdk 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/dist/index.js +167 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,10 +50,14 @@ Handle communication with the NETS terminal.
|
|
|
50
50
|
```javascript
|
|
51
51
|
const { communication } = require('nets-service-sdk');
|
|
52
52
|
|
|
53
|
-
// Initialize
|
|
54
|
-
// Ensure your config.json has 'com' (port) and 'terminal' settings
|
|
53
|
+
// Initialize WITHOUT socket (messages logged only)
|
|
55
54
|
communication.initialize();
|
|
56
55
|
|
|
56
|
+
// OR Initialize WITH socket (messages emitted via socket)
|
|
57
|
+
const io = require('socket.io-client');
|
|
58
|
+
const socket = io('http://localhost:3000');
|
|
59
|
+
communication.initialize(socket);
|
|
60
|
+
|
|
57
61
|
// Send a request to the terminal
|
|
58
62
|
// Types: 'STATUS_CHECK', 'LOGON', 'PAYMENT', 'CREDIT_PAYMENT'
|
|
59
63
|
communication.sentToTerminal('STATUS_CHECK', {});
|
|
@@ -66,6 +70,39 @@ communication.sentToTerminal('PAYMENT', {
|
|
|
66
70
|
});
|
|
67
71
|
```
|
|
68
72
|
|
|
73
|
+
### Socket.IO Integration (Optional)
|
|
74
|
+
|
|
75
|
+
The SDK supports optional Socket.IO integration for real-time event broadcasting.
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
const io = require('socket.io-client');
|
|
79
|
+
const sdk = require('nets-service-sdk');
|
|
80
|
+
|
|
81
|
+
// Connect to Socket.IO server
|
|
82
|
+
const socket = io('http://localhost:3000');
|
|
83
|
+
|
|
84
|
+
// Initialize with socket connection
|
|
85
|
+
sdk.communication.initialize(socket);
|
|
86
|
+
|
|
87
|
+
// Listen for events on client side
|
|
88
|
+
socket.on('PAYMENT_MESSAGE', (data) => {
|
|
89
|
+
console.log('Payment event:', data);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
socket.on('LOGON_MESSAGE', (data) => {
|
|
93
|
+
console.log('Logon event:', data);
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Socket Events Emitted:**
|
|
98
|
+
- `STATUS_MESSAGE` - Terminal status updates
|
|
99
|
+
- `LOGON_MESSAGE` - Terminal logon results
|
|
100
|
+
- `PAYMENT_MESSAGE` - Payment transaction updates
|
|
101
|
+
|
|
102
|
+
**Note:** Socket connection is **optional**. Without it, events are logged only.
|
|
103
|
+
|
|
104
|
+
See [Socket Integration Guide](docs/SOCKET_INTEGRATION.md) for detailed usage.
|
|
105
|
+
|
|
69
106
|
### Transaction Storage (New in v1.1.0)
|
|
70
107
|
|
|
71
108
|
All terminal requests and responses are automatically stored in a SQLite database.
|
package/dist/index.js
CHANGED
|
@@ -217,7 +217,18 @@ var require_sendMessage = __commonJS({
|
|
|
217
217
|
level: "info",
|
|
218
218
|
message: JSON.stringify(msg)
|
|
219
219
|
});
|
|
220
|
-
global.io
|
|
220
|
+
if (global.io) {
|
|
221
|
+
global.io.emit(tag, msg);
|
|
222
|
+
logger.log({
|
|
223
|
+
level: "debug",
|
|
224
|
+
message: `Socket message emitted: ${tag}`
|
|
225
|
+
});
|
|
226
|
+
} else {
|
|
227
|
+
logger.log({
|
|
228
|
+
level: "debug",
|
|
229
|
+
message: `Socket not available, message logged only: ${tag}`
|
|
230
|
+
});
|
|
231
|
+
}
|
|
221
232
|
};
|
|
222
233
|
}
|
|
223
234
|
});
|
|
@@ -1270,7 +1281,19 @@ var require_communication = __commonJS({
|
|
|
1270
1281
|
global.port.write(calculated.buffer);
|
|
1271
1282
|
exports2.checkACKorNACK();
|
|
1272
1283
|
};
|
|
1273
|
-
module2.exports.initialize = async () => {
|
|
1284
|
+
module2.exports.initialize = async (socketConnection = null) => {
|
|
1285
|
+
if (socketConnection) {
|
|
1286
|
+
global.io = socketConnection;
|
|
1287
|
+
logger.log({
|
|
1288
|
+
level: "info",
|
|
1289
|
+
message: "Socket connection initialized"
|
|
1290
|
+
});
|
|
1291
|
+
} else {
|
|
1292
|
+
logger.log({
|
|
1293
|
+
level: "info",
|
|
1294
|
+
message: "No socket connection provided, messages will be logged only"
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1274
1297
|
if (!config.terminal || config.terminal === "enable") {
|
|
1275
1298
|
global.port = new SerialPort({
|
|
1276
1299
|
path: config.simulation ? config.simulationPort : config.com,
|
|
@@ -1371,6 +1394,142 @@ var require_communication = __commonJS({
|
|
|
1371
1394
|
}
|
|
1372
1395
|
});
|
|
1373
1396
|
|
|
1397
|
+
// src/db/listTransactions.js
|
|
1398
|
+
var require_listTransactions = __commonJS({
|
|
1399
|
+
"src/db/listTransactions.js"(exports2, module2) {
|
|
1400
|
+
var { getRecentTransactions, getTransactionByEcn, getTransactionsByStatus } = require_transactionStore();
|
|
1401
|
+
var logger = require_winston()(module2);
|
|
1402
|
+
function listRecentTransactions(limit = 10) {
|
|
1403
|
+
try {
|
|
1404
|
+
const transactions = getRecentTransactions(limit);
|
|
1405
|
+
if (transactions.length === 0) {
|
|
1406
|
+
console.log("No transactions found in database.");
|
|
1407
|
+
return [];
|
|
1408
|
+
}
|
|
1409
|
+
console.log(`
|
|
1410
|
+
\u{1F4CA} Recent ${transactions.length} Transaction(s)
|
|
1411
|
+
`);
|
|
1412
|
+
console.log("=".repeat(80));
|
|
1413
|
+
transactions.forEach((txn, index) => {
|
|
1414
|
+
const time = new Date(txn.request_timestamp).toLocaleString();
|
|
1415
|
+
const duration = txn.response_timestamp ? `${txn.response_timestamp - txn.request_timestamp}ms` : "Pending";
|
|
1416
|
+
console.log(`
|
|
1417
|
+
${index + 1}. ECN: ${txn.ecn}`);
|
|
1418
|
+
console.log(` Type: ${txn.request_type}`);
|
|
1419
|
+
console.log(` Status: ${txn.status}`);
|
|
1420
|
+
console.log(` Time: ${time}`);
|
|
1421
|
+
console.log(` Duration: ${duration}`);
|
|
1422
|
+
if (txn.request_type === "PAYMENT" || txn.request_type === "CREDIT_PAYMENT") {
|
|
1423
|
+
const amount = txn.request_payload.amount ? `$${(txn.request_payload.amount / 100).toFixed(2)}` : "N/A";
|
|
1424
|
+
console.log(` Amount: ${amount}`);
|
|
1425
|
+
console.log(` Reference: ${txn.request_payload.reference || "N/A"}`);
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
console.log("\n" + "=".repeat(80) + "\n");
|
|
1429
|
+
return transactions;
|
|
1430
|
+
} catch (error) {
|
|
1431
|
+
if (error.code === "SQLITE_CANTOPEN") {
|
|
1432
|
+
console.log("\u2139\uFE0F Database not found. No transactions have been created yet.");
|
|
1433
|
+
console.log(" Transactions will be automatically stored when you send requests to the terminal.\n");
|
|
1434
|
+
return [];
|
|
1435
|
+
}
|
|
1436
|
+
logger.log({
|
|
1437
|
+
level: "error",
|
|
1438
|
+
message: `Failed to list transactions: ${error.message}`
|
|
1439
|
+
});
|
|
1440
|
+
throw error;
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
function listTransactionsByStatus(status, limit = 50) {
|
|
1444
|
+
try {
|
|
1445
|
+
const transactions = getTransactionsByStatus(status, limit);
|
|
1446
|
+
if (transactions.length === 0) {
|
|
1447
|
+
console.log(`No ${status} transactions found.`);
|
|
1448
|
+
return [];
|
|
1449
|
+
}
|
|
1450
|
+
console.log(`
|
|
1451
|
+
\u{1F4CA} ${status} Transactions (${transactions.length})
|
|
1452
|
+
`);
|
|
1453
|
+
console.log("=".repeat(80));
|
|
1454
|
+
transactions.forEach((txn, index) => {
|
|
1455
|
+
const time = new Date(txn.request_timestamp).toLocaleString();
|
|
1456
|
+
console.log(`
|
|
1457
|
+
${index + 1}. ECN: ${txn.ecn} | ${txn.request_type} | ${time}`);
|
|
1458
|
+
});
|
|
1459
|
+
console.log("\n" + "=".repeat(80) + "\n");
|
|
1460
|
+
return transactions;
|
|
1461
|
+
} catch (error) {
|
|
1462
|
+
if (error.code === "SQLITE_CANTOPEN") {
|
|
1463
|
+
console.log("\u2139\uFE0F Database not found. No transactions have been created yet.\n");
|
|
1464
|
+
return [];
|
|
1465
|
+
}
|
|
1466
|
+
logger.log({
|
|
1467
|
+
level: "error",
|
|
1468
|
+
message: `Failed to list transactions: ${error.message}`
|
|
1469
|
+
});
|
|
1470
|
+
throw error;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
function getTransactionDetails(ecn) {
|
|
1474
|
+
try {
|
|
1475
|
+
const transaction = getTransactionByEcn(ecn);
|
|
1476
|
+
if (!transaction) {
|
|
1477
|
+
console.log(`Transaction with ECN ${ecn} not found.`);
|
|
1478
|
+
return null;
|
|
1479
|
+
}
|
|
1480
|
+
console.log(`
|
|
1481
|
+
\u{1F4C4} Transaction Details: ${ecn}
|
|
1482
|
+
`);
|
|
1483
|
+
console.log("=".repeat(80));
|
|
1484
|
+
console.log(`Type: ${transaction.request_type}`);
|
|
1485
|
+
console.log(`Status: ${transaction.status}`);
|
|
1486
|
+
console.log(`Request Time: ${new Date(transaction.request_timestamp).toLocaleString()}`);
|
|
1487
|
+
if (transaction.response_timestamp) {
|
|
1488
|
+
console.log(`Response Time: ${new Date(transaction.response_timestamp).toLocaleString()}`);
|
|
1489
|
+
console.log(`Duration: ${transaction.response_timestamp - transaction.request_timestamp}ms`);
|
|
1490
|
+
}
|
|
1491
|
+
console.log(`
|
|
1492
|
+
Request Payload:`);
|
|
1493
|
+
console.log(JSON.stringify(transaction.request_payload, null, 2));
|
|
1494
|
+
if (transaction.response_data && Object.keys(transaction.response_data).length > 0) {
|
|
1495
|
+
console.log(`
|
|
1496
|
+
Response Data:`);
|
|
1497
|
+
console.log(JSON.stringify(transaction.response_data, null, 2));
|
|
1498
|
+
}
|
|
1499
|
+
console.log("\n" + "=".repeat(80) + "\n");
|
|
1500
|
+
return transaction;
|
|
1501
|
+
} catch (error) {
|
|
1502
|
+
if (error.code === "SQLITE_CANTOPEN") {
|
|
1503
|
+
console.log("\u2139\uFE0F Database not found. No transactions have been created yet.\n");
|
|
1504
|
+
return null;
|
|
1505
|
+
}
|
|
1506
|
+
logger.log({
|
|
1507
|
+
level: "error",
|
|
1508
|
+
message: `Failed to get transaction: ${error.message}`
|
|
1509
|
+
});
|
|
1510
|
+
throw error;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
module2.exports = {
|
|
1514
|
+
listRecentTransactions,
|
|
1515
|
+
listTransactionsByStatus,
|
|
1516
|
+
getTransactionDetails
|
|
1517
|
+
};
|
|
1518
|
+
if (require.main === module2) {
|
|
1519
|
+
const args = process.argv.slice(2);
|
|
1520
|
+
if (args[0] === "--status" && args[1]) {
|
|
1521
|
+
const limit = args[2] ? parseInt(args[2]) : 50;
|
|
1522
|
+
listTransactionsByStatus(args[1], limit);
|
|
1523
|
+
} else if (args[0] === "--ecn" && args[1]) {
|
|
1524
|
+
getTransactionDetails(args[1]);
|
|
1525
|
+
} else {
|
|
1526
|
+
const limit = args[0] ? parseInt(args[0]) : 10;
|
|
1527
|
+
listRecentTransactions(limit);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
});
|
|
1532
|
+
|
|
1374
1533
|
// src/index.js
|
|
1375
1534
|
var utilsHelper = require_utils_helper();
|
|
1376
1535
|
var winston = require_winston();
|
|
@@ -1383,6 +1542,7 @@ var httpRequest = require_httpRequest();
|
|
|
1383
1542
|
var parser = require_parser();
|
|
1384
1543
|
var responseHandler = require_responseHandler();
|
|
1385
1544
|
var transactionStore = require_transactionStore();
|
|
1545
|
+
var listTransactions = require_listTransactions();
|
|
1386
1546
|
module.exports = {
|
|
1387
1547
|
...utilsHelper,
|
|
1388
1548
|
logger: winston,
|
|
@@ -1402,5 +1562,9 @@ module.exports = {
|
|
|
1402
1562
|
// Auto-purge control
|
|
1403
1563
|
startAutoPurge: transactionStore.startAutoPurge,
|
|
1404
1564
|
stopAutoPurge: transactionStore.stopAutoPurge,
|
|
1405
|
-
isAutoPurgeRunning: transactionStore.isAutoPurgeRunning
|
|
1565
|
+
isAutoPurgeRunning: transactionStore.isAutoPurgeRunning,
|
|
1566
|
+
// Transaction listing functions
|
|
1567
|
+
listRecentTransactions: listTransactions.listRecentTransactions,
|
|
1568
|
+
listTransactionsByStatus: listTransactions.listTransactionsByStatus,
|
|
1569
|
+
getTransactionDetails: listTransactions.getTransactionDetails
|
|
1406
1570
|
};
|