@konplit-services/common 1.0.126 → 1.0.130

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.
@@ -0,0 +1,6 @@
1
+ import { PLAN_INTERVAL } from "./plan-types";
2
+ export declare const formatDate: (dataString: string) => string;
3
+ export declare const formatDateTime: (dataString: string) => string;
4
+ declare type PlanInterval = keyof typeof PLAN_INTERVAL;
5
+ export declare const getDuration: (duration: PlanInterval) => number;
6
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getDuration = exports.formatDateTime = exports.formatDate = void 0;
7
+ const dayjs_1 = __importDefault(require("dayjs"));
8
+ const plan_types_1 = require("./plan-types");
9
+ const formatDate = (dataString) => {
10
+ return (0, dayjs_1.default)(dataString).format("YYYY-MM-DD");
11
+ };
12
+ exports.formatDate = formatDate;
13
+ const formatDateTime = (dataString) => {
14
+ return (0, dayjs_1.default)(dataString).format("YYYY-MM-DD HH:mm:ss");
15
+ };
16
+ exports.formatDateTime = formatDateTime;
17
+ const getDuration = (duration) => {
18
+ const currentDate = (0, dayjs_1.default)();
19
+ switch (duration) {
20
+ case plan_types_1.PLAN_INTERVAL.HOURLY:
21
+ const newDurationHourly = currentDate.add(2, "hour").valueOf();
22
+ return newDurationHourly;
23
+ case plan_types_1.PLAN_INTERVAL.DAILY:
24
+ const newDurationDaily = currentDate.add(1, "day").valueOf();
25
+ return newDurationDaily;
26
+ case plan_types_1.PLAN_INTERVAL.WEEKLY:
27
+ const newDurationWeekly = currentDate.add(1, "week").valueOf();
28
+ return newDurationWeekly;
29
+ case plan_types_1.PLAN_INTERVAL.MONTHLY:
30
+ const newDurationMonthly = currentDate.add(1, "month").valueOf();
31
+ return newDurationMonthly;
32
+ case plan_types_1.PLAN_INTERVAL.QUARTERLY:
33
+ const newDurationQuarterly = currentDate.add(3, "month").valueOf();
34
+ return newDurationQuarterly;
35
+ case plan_types_1.PLAN_INTERVAL["6MONTHS"]:
36
+ const newDuration6Months = currentDate.add(6, "month").valueOf();
37
+ return newDuration6Months;
38
+ case plan_types_1.PLAN_INTERVAL.YEARLY:
39
+ const newDurationYearly = currentDate.add(1, "year").valueOf();
40
+ return newDurationYearly;
41
+ default:
42
+ throw new Error("Invalid duration");
43
+ }
44
+ };
45
+ exports.getDuration = getDuration;
@@ -39,3 +39,6 @@ export * from "./api-config-types";
39
39
  export * from "./settings-types";
40
40
  export * from "./business-types";
41
41
  export * from "./card-validate";
42
+ export * from "./date-processing";
43
+ export * from "./timestamp";
44
+ export * from "./keys";
@@ -55,3 +55,6 @@ __exportStar(require("./api-config-types"), exports);
55
55
  __exportStar(require("./settings-types"), exports);
56
56
  __exportStar(require("./business-types"), exports);
57
57
  __exportStar(require("./card-validate"), exports);
58
+ __exportStar(require("./date-processing"), exports);
59
+ __exportStar(require("./timestamp"), exports);
60
+ __exportStar(require("./keys"), exports);
@@ -0,0 +1,70 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Hashing a secret using sha256
4
+ * @param secret - the secret to hash
5
+ * @returns hashed secret
6
+ */
7
+ declare const hashingKey: (secret: string) => Buffer;
8
+ /**
9
+ * Generates an MD5 signature from the provided data
10
+ * @param data - the data to sign
11
+ * @returns MD5 hash of the data
12
+ */
13
+ declare const generateSignMD5: (data: Record<string, string>) => string;
14
+ /**
15
+ * Generates an MD5 signature for Fidelity transactions
16
+ * @param request_ref - the request reference
17
+ * @param client_secret - the client secret
18
+ * @returns MD5 hash of the data
19
+ */
20
+ declare const generateSignMD5Fedilty: (request_ref: string, client_secret: string) => string;
21
+ /**
22
+ * Generates the signature cipher for a request
23
+ * @param httpMethod - the request method (e.g., GET, POST)
24
+ * @param url - the request URL
25
+ * @param timestamp - the timestamp of the request
26
+ * @param nonce - unique ID
27
+ * @param clientId - the client ID
28
+ * @param clientSecret - the client secret
29
+ * @param amount - the amount to be charged
30
+ * @param authData - the card auth data
31
+ * @returns the generated signature cipher string
32
+ */
33
+ declare const generateSignatureCipher: (httpMethod: string, url: string, timestamp: string, nonce: string, clientId: string, clientSecret: string, amount?: string, authData?: string) => string;
34
+ /**
35
+ * Generates authentication data by encrypting card information using RSA
36
+ * @param options - card details and public key modulus and exponent
37
+ * @returns encrypted authentication data in Base64
38
+ */
39
+ declare const authData: (options: {
40
+ card: string;
41
+ pin: string;
42
+ exp: string;
43
+ cvv: string;
44
+ publicKeyModulus: string;
45
+ publicKeyExponent: string;
46
+ }) => string;
47
+ /**
48
+ * Generates a unique order ID based on the current year and month
49
+ * @returns the generated order ID
50
+ */
51
+ declare const generateOrderId: () => string;
52
+ /**
53
+ * Generates a unique transaction ID
54
+ * @param clientCode - the client code
55
+ * @returns the generated transaction ID
56
+ */
57
+ declare const generateTransactionID: (clientCode: string) => string;
58
+ /**
59
+ * Generates a Base64-encoded key from client ID and secret key
60
+ * @param client_id - the client ID
61
+ * @param secret_key - the secret key
62
+ * @returns Base64 encoded key
63
+ */
64
+ declare const generateBase64Key: (client_id: string, secret_key: string) => string;
65
+ /**
66
+ * Generates a Base64-encoded key for BVN using NIBSS details
67
+ * @returns Base64 encoded key for BVN
68
+ */
69
+ declare const generateBase64KeyNibssBVN: () => string;
70
+ export { hashingKey, generateSignMD5, generateSignMD5Fedilty, authData, generateOrderId, generateBase64Key, generateSignatureCipher, generateBase64KeyNibssBVN, generateTransactionID, };
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.generateTransactionID = exports.generateBase64KeyNibssBVN = exports.generateSignatureCipher = exports.generateBase64Key = exports.generateOrderId = exports.authData = exports.generateSignMD5Fedilty = exports.generateSignMD5 = exports.hashingKey = void 0;
27
+ const crypto = __importStar(require("crypto"));
28
+ const forge = __importStar(require("node-forge"));
29
+ const timestamp_1 = require("./timestamp");
30
+ const util_1 = require("../util");
31
+ const app_configs_1 = require("../app.configs");
32
+ /**
33
+ * Hashing a secret using sha256
34
+ * @param secret - the secret to hash
35
+ * @returns hashed secret
36
+ */
37
+ const hashingKey = (secret) => {
38
+ return crypto.createHash("sha256").update(secret).digest();
39
+ };
40
+ exports.hashingKey = hashingKey;
41
+ /**
42
+ * Converts a card string to its hexadecimal representation
43
+ * @param card_string - the card string
44
+ * @throws Error if the card string is invalid
45
+ * @returns the hexadecimal representation of the card string
46
+ */
47
+ const toHex = (card_string) => {
48
+ if (typeof card_string !== "string") {
49
+ throw new Error("Invalid card string");
50
+ }
51
+ return Array.from(card_string)
52
+ .map((char) => char.charCodeAt(0).toString(16))
53
+ .join("");
54
+ };
55
+ /**
56
+ * Generates an MD5 signature from the provided data
57
+ * @param data - the data to sign
58
+ * @returns MD5 hash of the data
59
+ */
60
+ const generateSignMD5 = (data) => {
61
+ const { nqrApiKey } = app_configs_1.appConfigs.getNIBSSDetails().nqrCode;
62
+ const keys = Object.keys(data).sort();
63
+ let signStrtemp = "";
64
+ keys.forEach((element, index) => {
65
+ if (index === 0) {
66
+ signStrtemp += `${element}=${data[element]}`;
67
+ }
68
+ else {
69
+ signStrtemp += `&${element}=${data[element]}`;
70
+ }
71
+ });
72
+ signStrtemp += nqrApiKey;
73
+ const hash = crypto.createHash("md5").update(signStrtemp).digest("hex");
74
+ return hash.toUpperCase();
75
+ };
76
+ exports.generateSignMD5 = generateSignMD5;
77
+ /**
78
+ * Generates an MD5 signature for Fidelity transactions
79
+ * @param request_ref - the request reference
80
+ * @param client_secret - the client secret
81
+ * @returns MD5 hash of the data
82
+ */
83
+ const generateSignMD5Fedilty = (request_ref, client_secret) => {
84
+ const dataform = `${request_ref};${client_secret}`;
85
+ return crypto.createHash("md5").update(dataform).digest("hex");
86
+ };
87
+ exports.generateSignMD5Fedilty = generateSignMD5Fedilty;
88
+ /**
89
+ * Generates the signature cipher for a request
90
+ * @param httpMethod - the request method (e.g., GET, POST)
91
+ * @param url - the request URL
92
+ * @param timestamp - the timestamp of the request
93
+ * @param nonce - unique ID
94
+ * @param clientId - the client ID
95
+ * @param clientSecret - the client secret
96
+ * @param amount - the amount to be charged
97
+ * @param authData - the card auth data
98
+ * @returns the generated signature cipher string
99
+ */
100
+ const generateSignatureCipher = (httpMethod, url, timestamp, nonce, clientId, clientSecret, amount, authData) => {
101
+ let signatureCipher = `${httpMethod}&${url}&${timestamp}&${nonce}&${clientId}&${clientSecret}`;
102
+ if (amount !== undefined && amount !== null) {
103
+ signatureCipher += `&${amount}`;
104
+ }
105
+ if (authData !== undefined && authData !== null) {
106
+ signatureCipher += `&${authData}`;
107
+ }
108
+ return signatureCipher;
109
+ };
110
+ exports.generateSignatureCipher = generateSignatureCipher;
111
+ /**
112
+ * Generates authentication data by encrypting card information using RSA
113
+ * @param options - card details and public key modulus and exponent
114
+ * @returns encrypted authentication data in Base64
115
+ */
116
+ const authData = (options) => {
117
+ const authString = `1Z${options.card}Z${options.pin}Z${options.exp}Z${options.cvv}`;
118
+ const vv = toHex(authString);
119
+ const authDataBytes = forge.util.hexToBytes(vv);
120
+ const clearSecureBytes = forge.util.createBuffer();
121
+ const { rsa } = forge.pki;
122
+ const modulos = new forge.jsbn.BigInteger(options.publicKeyModulus, 16);
123
+ const exp = new forge.jsbn.BigInteger(options.publicKeyExponent, 16);
124
+ const publicKey = rsa.setPublicKey(modulos, exp);
125
+ clearSecureBytes.putBytes(authDataBytes);
126
+ const vvvv = clearSecureBytes.getBytes();
127
+ const authBytes = publicKey.encrypt(vvvv);
128
+ return forge.util.encode64(authBytes);
129
+ };
130
+ exports.authData = authData;
131
+ /**
132
+ * Generates a unique order ID based on the current year and month
133
+ * @returns the generated order ID
134
+ */
135
+ const generateOrderId = () => {
136
+ const yyyymm = (0, timestamp_1.getYearMonth)();
137
+ const id = (0, util_1.uniqueId)("abcdefkpvlxnm0123456789", 24);
138
+ return `${yyyymm}${id}`;
139
+ };
140
+ exports.generateOrderId = generateOrderId;
141
+ /**
142
+ * Generates a unique transaction ID
143
+ * @param clientCode - the client code
144
+ * @returns the generated transaction ID
145
+ */
146
+ const generateTransactionID = (clientCode) => {
147
+ const now = new Date();
148
+ const year = now.getFullYear().toString().slice(-2);
149
+ const month = String(now.getMonth() + 1).padStart(2, "0");
150
+ const day = String(now.getDate()).padStart(2, "0");
151
+ const hours = String(now.getHours()).padStart(2, "0");
152
+ const minutes = String(now.getMinutes()).padStart(2, "0");
153
+ const seconds = String(now.getSeconds()).padStart(2, "0");
154
+ const random12Digits = Math.floor(100000000000 + Math.random() * 900000000000);
155
+ return `${clientCode}${year}${month}${day}${hours}${minutes}${seconds}${random12Digits}`;
156
+ };
157
+ exports.generateTransactionID = generateTransactionID;
158
+ /**
159
+ * Generates a Base64-encoded key from client ID and secret key
160
+ * @param client_id - the client ID
161
+ * @param secret_key - the secret key
162
+ * @returns Base64 encoded key
163
+ */
164
+ const generateBase64Key = (client_id, secret_key) => {
165
+ const dataToString = `${client_id}:${secret_key}`;
166
+ return Buffer.from(dataToString).toString("base64");
167
+ };
168
+ exports.generateBase64Key = generateBase64Key;
169
+ /**
170
+ * Generates a Base64-encoded key for BVN using NIBSS details
171
+ * @returns Base64 encoded key for BVN
172
+ */
173
+ const generateBase64KeyNibssBVN = () => {
174
+ const { bvnclientid, bvnSecret } = app_configs_1.appConfigs.getNIBSSDetails().bvn;
175
+ const dataToString = `${bvnclientid}:${bvnSecret}`;
176
+ return Buffer.from(dataToString).toString("base64");
177
+ };
178
+ exports.generateBase64KeyNibssBVN = generateBase64KeyNibssBVN;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @param month The month from 1 - 12
3
+ * @returns The formatted month as a string from 01 - 12
4
+ */
5
+ export declare const monthFormat: (month: number) => string;
6
+ /**
7
+ * @returns The current timestamp in milliseconds
8
+ */
9
+ export declare const getCurrentTimeStampMill: () => number;
10
+ /**
11
+ * @returns The current timestamp in seconds
12
+ */
13
+ export declare const getCurrentTimeStampSeconds: () => number;
14
+ /**
15
+ * @returns The current Year and Month as a string
16
+ */
17
+ export declare const getYearMonth: () => string;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getYearMonth = exports.getCurrentTimeStampSeconds = exports.getCurrentTimeStampMill = exports.monthFormat = void 0;
4
+ /**
5
+ * @param month The month from 1 - 12
6
+ * @returns The formatted month as a string from 01 - 12
7
+ */
8
+ const monthFormat = (month) => {
9
+ const months = {
10
+ 1: "01",
11
+ 2: "02",
12
+ 3: "03",
13
+ 4: "04",
14
+ 5: "05",
15
+ 6: "06",
16
+ 7: "07",
17
+ 8: "08",
18
+ 9: "09",
19
+ 10: "10",
20
+ 11: "11",
21
+ 12: "12",
22
+ };
23
+ if (!(month in months))
24
+ throw new Error("Invalid month");
25
+ return months[month];
26
+ };
27
+ exports.monthFormat = monthFormat;
28
+ /**
29
+ * @returns The current timestamp in milliseconds
30
+ */
31
+ const getCurrentTimeStampMill = () => {
32
+ const date = new Date();
33
+ return date.getTime();
34
+ };
35
+ exports.getCurrentTimeStampMill = getCurrentTimeStampMill;
36
+ /**
37
+ * @returns The current timestamp in seconds
38
+ */
39
+ const getCurrentTimeStampSeconds = () => {
40
+ const millionSecond = (0, exports.getCurrentTimeStampMill)();
41
+ return Math.floor(millionSecond / 1000);
42
+ };
43
+ exports.getCurrentTimeStampSeconds = getCurrentTimeStampSeconds;
44
+ /**
45
+ * @returns The current Year and Month as a string
46
+ */
47
+ const getYearMonth = () => {
48
+ const date = new Date();
49
+ const year = date.getFullYear();
50
+ const month = (0, exports.monthFormat)(date.getMonth() + 1);
51
+ return `${year}${month}`;
52
+ };
53
+ exports.getYearMonth = getYearMonth;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.126",
3
+ "version": "1.0.130",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -22,9 +22,11 @@
22
22
  "@types/express": "^4.17.13",
23
23
  "@types/jsonwebtoken": "^8.5.8",
24
24
  "@types/node": "^18.6.1",
25
+ "@types/node-forge": "^1.3.11",
25
26
  "@types/swagger-jsdoc": "^6.0.4",
26
27
  "@types/swagger-ui-express": "^4.1.6",
27
28
  "axios": "^1.7.2",
29
+ "dayjs": "^1.11.13",
28
30
  "express": "^4.18.1",
29
31
  "express-validator": "^6.14.2",
30
32
  "ioredis": "^5.4.1",
@@ -33,6 +35,7 @@
33
35
  "mongoose": "^6.9.2",
34
36
  "nanoid": "^3.3.7",
35
37
  "nats": "^2.25.0",
38
+ "node-forge": "^1.3.1",
36
39
  "node-ipinfo": "^3.5.0",
37
40
  "swagger-jsdoc": "^6.2.8",
38
41
  "swagger-ui-express": "^5.0.0",