nets-service-sdk 1.1.19 → 1.1.20

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 +124 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -280,6 +280,17 @@ var require_hexRequest = __commonJS({
280
280
  console.log("buffer", buffer);
281
281
  return { buffer, ecn: body.ecn, hexString };
282
282
  };
283
+ module2.exports.generateFunction56Request = () => {
284
+ const date = moment().format("DDMMYYHHmmss");
285
+ const paddedValue = padWithLeadingZeros(date, "12");
286
+ const hexValue = paddedValue.toHexString();
287
+ const calculated = `0018${hexValue}35363031301C03`;
288
+ const calculatedXor = xorCalculation(calculated);
289
+ const hexString = `02${calculated}` + calculatedXor;
290
+ console.log("Recovery Function 56 Request:", hexString);
291
+ const buffer = Buffer.from(hexString, "hex");
292
+ return { buffer, ecn: date };
293
+ };
283
294
  }
284
295
  });
285
296
 
@@ -930,6 +941,32 @@ var require_responseHandler = __commonJS({
930
941
  }
931
942
  return isMatch;
932
943
  };
944
+ module2.exports.terminalRecovery = (hexFrame) => {
945
+ const ecn = activePaymentEcn;
946
+ const parsedValue = netsPaymentParser(hexFrame, ecn);
947
+ const response = jsonProcessor(parsedValue);
948
+ if (response.translated) {
949
+ activePaymentEcn = null;
950
+ }
951
+ if (response.translated && (response.translated.category == "SUCCESS" || response.translated.status == "APPROVED")) {
952
+ logger.log({ level: "info", message: "\u2705 Recovery SUCCESS: Last transaction was APPROVED." });
953
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
954
+ success: true,
955
+ status: "SUCCESS",
956
+ response,
957
+ action: "COMPLETED"
958
+ });
959
+ sendPaymentResponseToCloud(response.translated);
960
+ } else {
961
+ logger.log({ level: "warn", message: "\u26A0\uFE0F Recovery FAILED: Transaction status could not be confirmed as APPROVED." });
962
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
963
+ status: "FAILED",
964
+ action: "CANCELLED",
965
+ message: "Payment needs verification / Failed",
966
+ reason: "RECOVERY_NOT_CONFIRMED"
967
+ });
968
+ }
969
+ };
933
970
  }
934
971
  });
935
972
 
@@ -959,13 +996,14 @@ var require_communication = __commonJS({
959
996
  var { initialiseRequest } = require_httpRequest();
960
997
  var requestType;
961
998
  var calculated;
962
- var MAX_COUNT = 3;
999
+ var MAX_COUNT = 2;
963
1000
  var WRONG_LRC_MAX_COUNT = 3;
964
1001
  var count = MAX_COUNT;
965
1002
  var lrcCount = WRONG_LRC_MAX_COUNT;
966
1003
  var ackOrNack = null;
967
1004
  var requestPayload = null;
968
1005
  var ackTimeout = null;
1006
+ var dataTimeout = null;
969
1007
  var ACK = Buffer.alloc(1, 6, "hex");
970
1008
  var NACK = Buffer.alloc(1, 21, "hex");
971
1009
  var dataBuffer = Buffer.alloc(0);
@@ -981,6 +1019,10 @@ var require_communication = __commonJS({
981
1019
  clearTimeout(ackTimeout);
982
1020
  ackTimeout = null;
983
1021
  }
1022
+ if (dataTimeout) {
1023
+ clearTimeout(dataTimeout);
1024
+ dataTimeout = null;
1025
+ }
984
1026
  };
985
1027
  module2.exports.checkACKorNACK = () => {
986
1028
  if (ackTimeout) clearTimeout(ackTimeout);
@@ -1000,12 +1042,12 @@ var require_communication = __commonJS({
1000
1042
  message: `On ACK/NACK Listener: Terminal did not respond with ACK/NACK after max ${count} retries. Sending TERMINAL_ERROR to client.`
1001
1043
  });
1002
1044
  sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
1003
- success: true,
1045
+ status: "FAILED",
1004
1046
  action: "TERMINAL_ERROR"
1005
1047
  });
1006
1048
  }
1007
1049
  }
1008
- }, 4e3);
1050
+ }, 2e3);
1009
1051
  };
1010
1052
  var resendData = () => {
1011
1053
  if (requestType == "STATUS_CHECK") {
@@ -1105,6 +1147,7 @@ var require_communication = __commonJS({
1105
1147
  clearTimeout(ackTimeout);
1106
1148
  ackTimeout = null;
1107
1149
  }
1150
+ exports2.startDataTimeout();
1108
1151
  if (byte === 21) {
1109
1152
  count--;
1110
1153
  if (count >= 0) {
@@ -1115,7 +1158,7 @@ var require_communication = __commonJS({
1115
1158
  level: "error",
1116
1159
  message: `Terminal responded with NACK multiple times. Max retries reached. Sending TERMINAL_ERROR.`
1117
1160
  });
1118
- sendMessage2("clientRoom", "PAYMENT_MESSAGE", { success: true, action: "TERMINAL_ERROR" });
1161
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", { status: "FAILED", action: "TERMINAL_ERROR" });
1119
1162
  }
1120
1163
  }
1121
1164
  continue;
@@ -1171,18 +1214,37 @@ var require_communication = __commonJS({
1171
1214
  }
1172
1215
  }
1173
1216
  };
1217
+ module2.exports.triggerRecovery = () => {
1218
+ const { generateFunction56Request } = require_hexRequest();
1219
+ exports2.reset();
1220
+ lrcCount = WRONG_LRC_MAX_COUNT;
1221
+ count = MAX_COUNT;
1222
+ const req = generateFunction56Request();
1223
+ requestType = "RECOVERY";
1224
+ calculated = req;
1225
+ logger.log({ level: "info", message: "\u{1F680} Triggering Recovery (Function 56) due to LRC failures..." });
1226
+ global.port.write(req.buffer);
1227
+ exports2.checkACKorNACK();
1228
+ };
1174
1229
  module2.exports.handleFullMessage = (fullFrame) => {
1175
- const { checkLrc: checkLrc2, terminalStatus: terminalStatus2, terminalLogon: terminalLogon2, terminalPayment: terminalPayment2, terminalCreditPayment: terminalCreditPayment2 } = require_responseHandler();
1230
+ const { checkLrc: checkLrc2, terminalStatus: terminalStatus2, terminalLogon: terminalLogon2, terminalPayment: terminalPayment2, terminalCreditPayment: terminalCreditPayment2, terminalRecovery } = require_responseHandler();
1176
1231
  if (ackTimeout) {
1177
1232
  clearTimeout(ackTimeout);
1178
1233
  ackTimeout = null;
1179
1234
  }
1235
+ if (dataTimeout) {
1236
+ clearTimeout(dataTimeout);
1237
+ dataTimeout = null;
1238
+ }
1180
1239
  ackOrNack = Buffer.from([6]);
1181
1240
  const hexFrame = fullFrame.toString("hex").toUpperCase();
1182
1241
  logger.log({ level: "info", message: `Full frame processed: ${hexFrame}` });
1183
- if (checkLrc2(hexFrame)) {
1242
+ const isLrcCorrect = checkLrc2(hexFrame);
1243
+ if (isLrcCorrect) {
1184
1244
  logger.log({ level: "info", message: `LRC Correct. Sending ACK (0x06).` });
1185
1245
  global.port.write(ACK);
1246
+ lrcCount = WRONG_LRC_MAX_COUNT;
1247
+ count = MAX_COUNT;
1186
1248
  logger.log({ level: "info", message: `requestType: ${requestType}` });
1187
1249
  if (requestType === "STATUS_CHECK") {
1188
1250
  terminalStatus2(calculated?.ecn, hexFrame);
@@ -1192,12 +1254,65 @@ var require_communication = __commonJS({
1192
1254
  terminalPayment2(hexFrame, calculated?.ecn);
1193
1255
  } else if (requestType === "CREDIT_PAYMENT") {
1194
1256
  terminalCreditPayment2(hexFrame, calculated?.ecn);
1257
+ } else if (requestType === "RECOVERY") {
1258
+ terminalRecovery(hexFrame);
1195
1259
  }
1196
1260
  } else {
1197
- logger.log({ level: "error", message: `LRC Incorrect. Sending NACK (0x15).` });
1198
- global.port.write(NACK);
1261
+ lrcCount--;
1262
+ if (lrcCount > 0) {
1263
+ logger.log({ level: "error", message: `LRC Incorrect. Sending NACK (0x15). Retries left: ${lrcCount}` });
1264
+ global.port.write(NACK);
1265
+ } else {
1266
+ if (hexFrame.indexOf("415050524F564544") > -1) {
1267
+ logger.log({ level: "info", message: "\u2705 LRC Incorrect 3 times, but APPROVED found in hex payload. Accepting with ACK." });
1268
+ global.port.write(ACK);
1269
+ lrcCount = WRONG_LRC_MAX_COUNT;
1270
+ count = MAX_COUNT;
1271
+ if (requestType === "PAYMENT") terminalPayment2(hexFrame, calculated?.ecn);
1272
+ else if (requestType === "CREDIT_PAYMENT") terminalCreditPayment2(hexFrame, calculated?.ecn);
1273
+ else if (requestType === "RECOVERY") terminalRecovery(hexFrame);
1274
+ else if (requestType === "LOGON") terminalLogon2(calculated?.ecn, hexFrame);
1275
+ else if (requestType === "STATUS_CHECK") terminalStatus2(calculated?.ecn, hexFrame);
1276
+ } else {
1277
+ logger.log({ level: "warn", message: `LRC Incorrect 3 times and NO approval found. Stopping.` });
1278
+ global.port.write(NACK);
1279
+ if (ackTimeout) {
1280
+ clearTimeout(ackTimeout);
1281
+ ackTimeout = null;
1282
+ }
1283
+ if (requestType !== "RECOVERY") {
1284
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
1285
+ status: "UNKNOWN",
1286
+ action: "VERIFYING_STATUS",
1287
+ message: "Payment status uncertain. Please wait while we verify...",
1288
+ reason: "LRC_FAILURE"
1289
+ });
1290
+ setTimeout(() => exports2.triggerRecovery(), 2e3);
1291
+ } else {
1292
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
1293
+ status: "FAILED",
1294
+ action: "CANCELLED",
1295
+ message: "Payment needs manual verification / Recovery failed.",
1296
+ reason: "RECOVERY_LRC_FAILURE"
1297
+ });
1298
+ }
1299
+ }
1300
+ }
1199
1301
  }
1200
1302
  };
1303
+ module2.exports.startDataTimeout = () => {
1304
+ if (dataTimeout) clearTimeout(dataTimeout);
1305
+ dataTimeout = setTimeout(() => {
1306
+ dataTimeout = null;
1307
+ logger.error({ level: "error", message: `Terminal response time out (60 seconds) after ACK.` });
1308
+ sendMessage2("clientRoom", "PAYMENT_MESSAGE", {
1309
+ status: "FAILED",
1310
+ action: "TERMINAL_ERROR",
1311
+ message: "Terminal response time out (60 seconds)"
1312
+ });
1313
+ exports2.reset();
1314
+ }, 6e4);
1315
+ };
1201
1316
  module2.exports.checkPortConnection = async () => {
1202
1317
  const portPath = config.simulation ? config.simulationPort : config.com;
1203
1318
  try {
@@ -1300,7 +1415,7 @@ var require_package = __commonJS({
1300
1415
  "package.json"(exports2, module2) {
1301
1416
  module2.exports = {
1302
1417
  name: "nets-service-sdk",
1303
- version: "1.1.19",
1418
+ version: "1.1.20",
1304
1419
  description: "Utility functions for Nets Service",
1305
1420
  source: "src/index.js",
1306
1421
  main: "dist/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nets-service-sdk",
3
- "version": "1.1.19",
3
+ "version": "1.1.20",
4
4
  "description": "Utility functions for Nets Service",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",