nets-service-sdk 1.1.14 → 1.1.15

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.
Files changed (2) hide show
  1. package/dist/index.js +90 -55
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -6,7 +6,6 @@ var __commonJS = (cb, mod) => function __require() {
6
6
  // src/utils.helper.js
7
7
  var require_utils_helper = __commonJS({
8
8
  "src/utils.helper.js"(exports2, module2) {
9
- var { SerialPort } = require("serialport");
10
9
  var crypto = require("crypto");
11
10
  var _ = require("lodash");
12
11
  module2.exports.padWithLeadingZeros = (num, totalLength) => {
@@ -53,34 +52,6 @@ var require_utils_helper = __commonJS({
53
52
  ""
54
53
  ).trim();
55
54
  };
56
- SerialPort.prototype._disconnected = function(err) {
57
- this.paused = true;
58
- this.emit("disconnect", err);
59
- if (this.closing) {
60
- return;
61
- }
62
- if (this.fd === null) {
63
- return;
64
- }
65
- this.closing = true;
66
- if (process.platform !== "win32") {
67
- this.readable = false;
68
- if (this.serialPoller) {
69
- this.serialPoller.close();
70
- }
71
- }
72
- SerialPortBinding.close(
73
- this.fd,
74
- function(err2) {
75
- this.closing = false;
76
- if (err2) {
77
- debug("Disconnect close completed with error: ", err2);
78
- }
79
- this.fd = null;
80
- this.emit("close");
81
- }.bind(this)
82
- );
83
- };
84
55
  module2.exports.generateServiceKey = (payload) => {
85
56
  const key = `${process.env.user}:${process.env.password}`;
86
57
  const algorithm = "aes256";
@@ -950,6 +921,10 @@ var require_communication = __commonJS({
950
921
  });
951
922
  resendData();
952
923
  } else {
924
+ logger.log({
925
+ level: "warn",
926
+ message: `On ACK/NACK Listener: Terminal did not respond with ACK/NACK after max ${count} retries. Sending TERMINAL_ERROR to client.`
927
+ });
953
928
  sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
954
929
  success: true,
955
930
  action: "TERMINAL_ERROR"
@@ -1018,20 +993,6 @@ var require_communication = __commonJS({
1018
993
  });
1019
994
  global.port.on("data", (data) => {
1020
995
  logger.log({ level: "info", message: `Raw data: ${data.toString("hex")}` });
1021
- if (data.length == 1) {
1022
- console.log(data);
1023
- if (data[0] === 6 || data[0] === 6 || data[0] === 21 || data[0] === 21) {
1024
- const byte = data[0];
1025
- ackOrNack = Buffer.from([byte]);
1026
- data = data.slice(1);
1027
- logger.log({ level: "info", message: `Terminal sent ${byte === 6 ? "ACK" : "NACK"} (0x${byte.toString(16)})` });
1028
- if (byte === 21 || byte === 21) {
1029
- count--;
1030
- if (count >= 0) resendData();
1031
- else sendMessage2("clientRoom", "PAYMENT_MESSAGE", { success: true, action: "TERMINAL_ERROR" });
1032
- }
1033
- }
1034
- }
1035
996
  dataBuffer = Buffer.concat([dataBuffer, data]);
1036
997
  exports2.processIncomingData();
1037
998
  });
@@ -1060,17 +1021,42 @@ var require_communication = __commonJS({
1060
1021
  }
1061
1022
  };
1062
1023
  module2.exports.processIncomingData = () => {
1063
- while (dataBuffer.length >= 3) {
1064
- const stxIndex = dataBuffer.indexOf(2);
1065
- if (stxIndex === -1) {
1066
- dataBuffer = Buffer.alloc(0);
1067
- break;
1024
+ while (dataBuffer.length > 0) {
1025
+ if (dataBuffer[0] === 6 || dataBuffer[0] === 21) {
1026
+ const byte = dataBuffer[0];
1027
+ ackOrNack = Buffer.from([byte]);
1028
+ dataBuffer = dataBuffer.slice(1);
1029
+ logger.log({ level: "info", message: `Terminal sent ${byte === 6 ? "ACK" : "NACK"} (0x${byte.toString(16).padStart(2, "0")})` });
1030
+ if (ackTimeout) {
1031
+ clearTimeout(ackTimeout);
1032
+ ackTimeout = null;
1033
+ }
1034
+ if (byte === 21) {
1035
+ count--;
1036
+ if (count >= 0) {
1037
+ logger.log({ level: "info", message: `Terminal NACK received, retrying ${requestType}...` });
1038
+ resendData();
1039
+ } else {
1040
+ logger.log({
1041
+ level: "error",
1042
+ message: `Terminal responded with NACK multiple times. Max retries reached. Sending TERMINAL_ERROR.`
1043
+ });
1044
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", { success: true, action: "TERMINAL_ERROR" });
1045
+ }
1046
+ }
1047
+ continue;
1068
1048
  }
1069
- if (stxIndex > 0) {
1070
- dataBuffer = dataBuffer.slice(stxIndex);
1049
+ if (dataBuffer[0] !== 2) {
1050
+ const nextStart = dataBuffer.slice(1).findIndex((b) => b === 2 || b === 6 || b === 21);
1051
+ if (nextStart === -1) {
1052
+ dataBuffer = Buffer.alloc(0);
1053
+ break;
1054
+ } else {
1055
+ dataBuffer = dataBuffer.slice(nextStart + 1);
1056
+ continue;
1057
+ }
1071
1058
  }
1072
1059
  if (dataBuffer.length < 3) break;
1073
- if (dataBuffer.length < 3) break;
1074
1060
  const lenBytes = dataBuffer.slice(1, 3).toString("hex");
1075
1061
  const bodyLen = parseInt(lenBytes, 10);
1076
1062
  const expectedFrameSize = 1 + 2 + bodyLen + 2;
@@ -1086,7 +1072,10 @@ var require_communication = __commonJS({
1086
1072
  dataBuffer = dataBuffer.slice(etxIndex + 2);
1087
1073
  exports2.handleFullMessage(frame);
1088
1074
  } else {
1089
- if (dataBuffer.length > 1024) dataBuffer = Buffer.alloc(0);
1075
+ if (dataBuffer.length > 2048) {
1076
+ logger.log({ level: "warn", message: "Stale data in buffer exceeds limit, clearing." });
1077
+ dataBuffer = Buffer.alloc(0);
1078
+ }
1090
1079
  break;
1091
1080
  }
1092
1081
  }
@@ -1119,9 +1108,51 @@ var require_communication = __commonJS({
1119
1108
  global.port.write(NACK);
1120
1109
  }
1121
1110
  };
1122
- module2.exports.sentToTerminal = (type, body) => {
1111
+ module2.exports.checkPortConnection = async () => {
1112
+ const portPath = config.simulation ? config.simulationPort : config.com;
1113
+ try {
1114
+ const ports = await SerialPort.list();
1115
+ const availablePorts = ports.map((p) => p.path);
1116
+ if (!availablePorts.includes(portPath)) {
1117
+ logger.error({
1118
+ level: "error",
1119
+ message: `\u274C Port ${portPath} is NOT connected to the system.`
1120
+ });
1121
+ return { success: false, error: "PORT_DISCONNECTED" };
1122
+ }
1123
+ if (!global.port || !global.port.isOpen) {
1124
+ logger.error({
1125
+ level: "error",
1126
+ message: `\u274C Port ${portPath} is connected but NOT open.`
1127
+ });
1128
+ if (config.terminal === "enable" || !config.terminal) {
1129
+ logger.log({ level: "info", message: "Attempting to re-initialize port..." });
1130
+ exports2.initialize();
1131
+ }
1132
+ return { success: false, error: "PORT_NOT_OPEN" };
1133
+ }
1134
+ return { success: true };
1135
+ } catch (e) {
1136
+ logger.error({
1137
+ level: "error",
1138
+ message: `Error checking port connection: ${e.message}`
1139
+ });
1140
+ return { success: false, error: e.message };
1141
+ }
1142
+ };
1143
+ module2.exports.sentToTerminal = async (type, body) => {
1123
1144
  requestType = type;
1124
1145
  requestPayload = body;
1146
+ const portStatus = await exports2.checkPortConnection();
1147
+ if (!portStatus.success) {
1148
+ const msgType = requestType === "STATUS_CHECK" ? "STATUS_MESSAGE" : requestType === "LOGON" ? "LOGON_MESSAGE" : "PAYMENT_MESSAGE";
1149
+ sendMessage2("clientRoom", msgType, {
1150
+ status: "FAILED",
1151
+ action: "TERMINAL_ERROR",
1152
+ error: portStatus.error,
1153
+ message: "Port is not connected or not responding."
1154
+ });
1155
+ }
1125
1156
  if (requestType == "STATUS_CHECK") {
1126
1157
  calculated = generateStatusReq();
1127
1158
  sendMessage2("clientRoom", "STATUS_MESSAGE", {
@@ -1152,9 +1183,13 @@ var require_communication = __commonJS({
1152
1183
  lastPaymentTime = now;
1153
1184
  }
1154
1185
  exports2.reset();
1155
- const { setPaymentCloudUrl, setPaymentOrderId } = require_responseHandler();
1186
+ const {
1187
+ setPaymentCloudUrl,
1188
+ setPaymentOrderId
1189
+ } = require_responseHandler();
1156
1190
  if (body && body.cloud_url) setPaymentCloudUrl(body.cloud_url);
1157
- if (body && (body.reference || body.orderId)) setPaymentOrderId(body.reference || body.orderId);
1191
+ if (body && (body.reference || body.orderId))
1192
+ setPaymentOrderId(body.reference || body.orderId);
1158
1193
  calculated = generatePaymentRequest(body);
1159
1194
  console.log(calculated);
1160
1195
  sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nets-service-sdk",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "description": "Utility functions for Nets Service",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",