@7365admin1/core 2.23.0 → 2.24.0

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/dist/index.js CHANGED
@@ -355,6 +355,9 @@ __export(src_exports, {
355
355
  usePriceRepo: () => usePriceRepo,
356
356
  usePromoCodeController: () => usePromoCodeController,
357
357
  usePromoCodeRepo: () => usePromoCodeRepo,
358
+ useRedDotPaymentController: () => useRedDotPaymentController,
359
+ useRedDotPaymentRepo: () => useRedDotPaymentRepo,
360
+ useRedDotPaymentSvc: () => useRedDotPaymentSvc,
358
361
  useRobotController: () => useRobotController,
359
362
  useRobotRepo: () => useRobotRepo,
360
363
  useRobotService: () => useRobotService,
@@ -28859,7 +28862,7 @@ function useSiteUnitBillingRepo() {
28859
28862
  value = MUnitBilling(value);
28860
28863
  const res = await collection.insertOne(value, { session });
28861
28864
  const acronym = createAcronym(value.billName || "NA");
28862
- const dateFormatted = formatDateString(/* @__PURE__ */ new Date());
28865
+ const dateFormatted = formatDateString2(/* @__PURE__ */ new Date());
28863
28866
  const newId = new import_mongodb81.ObjectId(res.insertedId).toString().slice(-6);
28864
28867
  const referenceNumber = `${acronym}${newId}${dateFormatted}`;
28865
28868
  await collection.updateOne(
@@ -29198,7 +29201,7 @@ function useSiteUnitBillingRepo() {
29198
29201
  function createAcronym(billName) {
29199
29202
  return billName.split(" ").map((word) => word.charAt(0).toUpperCase()).join("");
29200
29203
  }
29201
- function formatDateString(today) {
29204
+ function formatDateString2(today) {
29202
29205
  today = typeof today === "string" ? new Date(today) : today;
29203
29206
  let month = today.getMonth() + 1;
29204
29207
  let day = today.getDate();
@@ -43948,6 +43951,718 @@ async function manpowerEvents(io) {
43948
43951
  });
43949
43952
  });
43950
43953
  }
43954
+
43955
+ // src/services/reddot-payment.service.ts
43956
+ var import_axios3 = __toESM(require("axios"));
43957
+
43958
+ // src/utils/payment-signature.util.ts
43959
+ var crypto3 = __toESM(require("crypto"));
43960
+ function paymentSignature(params, secretKey) {
43961
+ const fieldOrder = ["mid", "order_id", "payment_type", "amount", "ccy", "card_no", "exp_date", "cvv2"];
43962
+ let concatenated = "";
43963
+ fieldOrder.forEach((v) => {
43964
+ if (!params.hasOwnProperty(v)) {
43965
+ return;
43966
+ } else if (v === "cvv2") {
43967
+ concatenated += (params[v] ?? "").toString().slice(-1);
43968
+ } else if (v === "card_no") {
43969
+ concatenated += (params[v] ?? "").substring(0, 6) + (params[v] ?? "").slice(-4);
43970
+ } else {
43971
+ concatenated += params[v];
43972
+ }
43973
+ });
43974
+ concatenated += secretKey;
43975
+ const signature = crypto3.createHash("sha512").update(concatenated).digest("hex");
43976
+ return signature;
43977
+ }
43978
+ function genericSignature(params, secretKey) {
43979
+ const paramsCopy = { ...params };
43980
+ delete paramsCopy["signature"];
43981
+ const sortedParams = Object.keys(paramsCopy).sort().reduce((obj, key) => {
43982
+ obj[key] = paramsCopy[key];
43983
+ return obj;
43984
+ }, {});
43985
+ const concatenated = Object.values(sortedParams).join("") + secretKey;
43986
+ const signature = crypto3.createHash("sha512").update(concatenated).digest("hex");
43987
+ return signature;
43988
+ }
43989
+
43990
+ // src/repositories/reddot-payment.repository.ts
43991
+ var import_mongodb120 = require("mongodb");
43992
+
43993
+ // src/utils/date-format.util.ts
43994
+ var import_moment_timezone6 = __toESM(require("moment-timezone"));
43995
+ function formatDateString(today, format, dateRange) {
43996
+ today = typeof today === "string" ? new Date(today) : today;
43997
+ let month = today.getMonth() + 1;
43998
+ let day = today.getDate();
43999
+ let year = today.getFullYear();
44000
+ let formattedDate;
44001
+ if (format === "mm-dd-yyyy") {
44002
+ formattedDate = `${month.toString().padStart(2, "0")}-${day.toString().padStart(2, "0")}-${year}`;
44003
+ } else if (format === "dd-mm-yyyy") {
44004
+ formattedDate = `${day.toString().padStart(2, "0")}-${month.toString().padStart(2, "0")}-${year}`;
44005
+ } else if (format === "for-ref") {
44006
+ formattedDate = `${month.toString().padStart(2, "0")}${day.toString().padStart(2, "0")}${year}`;
44007
+ } else if (format === "mm/dd/yyyy") {
44008
+ formattedDate = `${month.toString().padStart(2, "0")}/${day.toString().padStart(2, "0")}/${year}`;
44009
+ } else {
44010
+ formattedDate = `${year}-${month.toString().padStart(2, "0")}-${day.toString().padStart(2, "0")}`;
44011
+ }
44012
+ return formattedDate;
44013
+ }
44014
+
44015
+ // src/models/credit-card.model.ts
44016
+ var import_mongodb117 = require("mongodb");
44017
+ var MCardInfo = class {
44018
+ // this is coming from RDP transaction
44019
+ constructor({
44020
+ _id = new import_mongodb117.ObjectId(),
44021
+ userId,
44022
+ cardType,
44023
+ cardNumber,
44024
+ cardId,
44025
+ fullName,
44026
+ expiryDate,
44027
+ cvv,
44028
+ site,
44029
+ siteName,
44030
+ organization,
44031
+ createdBy,
44032
+ createdAt,
44033
+ updatedAt,
44034
+ transaction_id
44035
+ } = {}) {
44036
+ this._id = _id;
44037
+ this.userId = userId;
44038
+ this.cardType = cardType;
44039
+ this.cardNumber = cardNumber;
44040
+ this.cardId = cardId;
44041
+ this.fullName = fullName;
44042
+ this.expiryDate = expiryDate;
44043
+ this.cvv = cvv;
44044
+ this.site = site;
44045
+ this.siteName = siteName;
44046
+ this.organization = organization;
44047
+ this.createdBy = createdBy;
44048
+ this.createdAt = createdAt;
44049
+ this.updatedAt = updatedAt;
44050
+ this.transaction_id = transaction_id;
44051
+ }
44052
+ };
44053
+
44054
+ // src/models/cart.model.ts
44055
+ var import_mongodb118 = require("mongodb");
44056
+ var MUnitBillings = class {
44057
+ // transaction response messages
44058
+ constructor({
44059
+ _id = new import_mongodb118.ObjectId(),
44060
+ unit,
44061
+ unitId,
44062
+ unitBill,
44063
+ paymentStatus = "Processing" /* processing */,
44064
+ referenceNumber,
44065
+ amountPaid,
44066
+ paidBy,
44067
+ datePaid,
44068
+ billCategory,
44069
+ billFrom,
44070
+ billTo,
44071
+ site,
44072
+ organization,
44073
+ createdBy,
44074
+ createdAt,
44075
+ updatedAt,
44076
+ message
44077
+ } = {}) {
44078
+ this._id = _id;
44079
+ this.unit = unit;
44080
+ this.unitId = unitId;
44081
+ this.referenceNumber = referenceNumber;
44082
+ this.unitBill = unitBill;
44083
+ this.paymentStatus = paymentStatus;
44084
+ this.amountPaid = amountPaid;
44085
+ this.site = site;
44086
+ this.organization = organization;
44087
+ this.paidBy = paidBy;
44088
+ this.datePaid = datePaid;
44089
+ this.billCategory = billCategory;
44090
+ this.billFrom = billFrom;
44091
+ this.billTo = billTo;
44092
+ this.createdBy = createdBy;
44093
+ this.createdAt = createdAt;
44094
+ this.updatedAt = updatedAt;
44095
+ this.message = message;
44096
+ }
44097
+ };
44098
+
44099
+ // src/repositories/payment.repository.ts
44100
+ var import_mongodb119 = require("mongodb");
44101
+ var import_node_server_utils214 = require("@7365admin1/node-server-utils");
44102
+ var PaymentBillRepo = () => {
44103
+ const getDB = () => {
44104
+ const db = import_node_server_utils214.useAtlas.getDb();
44105
+ if (!db) {
44106
+ throw new import_node_server_utils214.InternalServerError("Unable to connect to server.");
44107
+ }
44108
+ return db;
44109
+ };
44110
+ const collection = () => {
44111
+ return getDB().collection("unit-bill");
44112
+ };
44113
+ const billCollection = () => {
44114
+ return getDB().collection("bill-records");
44115
+ };
44116
+ const creditCollection = () => {
44117
+ return getDB().collection("credit-info");
44118
+ };
44119
+ const cartCollection = () => {
44120
+ return getDB().collection("checkout-cart");
44121
+ };
44122
+ const payUnitBill = async (refId, payload) => {
44123
+ try {
44124
+ if (refId)
44125
+ refId = refId.toString();
44126
+ const unitBillInfo = await collection().findOne({ referenceNumber: refId });
44127
+ const unitBillId = unitBillInfo?._id;
44128
+ if (!unitBillInfo) {
44129
+ throw new Error("Unit bill info not found");
44130
+ }
44131
+ if (unitBillInfo?.status === "Inactive" /* inactive */) {
44132
+ throw new Error("This Bill is Inactive!");
44133
+ }
44134
+ const billId = new import_mongodb119.ObjectId(unitBillInfo?.billId);
44135
+ const billInfo = await billCollection().findOne({ _id: billId });
44136
+ if (!billInfo) {
44137
+ throw new Error("Bill info not found");
44138
+ }
44139
+ if (payload.paymentStatus === "Partial Payment" /* partial */ && payload.amountPaid) {
44140
+ const price = parseInt(billInfo?.price);
44141
+ const amount = payload.amountPaid;
44142
+ const numericPrice = typeof price === "string" ? parseInt(price) : price;
44143
+ const numericAmount = typeof amount === "string" ? parseInt(amount) : amount;
44144
+ if (typeof amount !== "number") {
44145
+ throw new Error("Amount is not a valid number");
44146
+ } else if (typeof price !== "number") {
44147
+ throw new Error("Price s not a valid number");
44148
+ }
44149
+ payload.balance = numericPrice - numericAmount;
44150
+ } else if (payload.paymentStatus === "Paid" /* paid */ && payload.amountPaid) {
44151
+ payload.datePaid = /* @__PURE__ */ new Date();
44152
+ payload.paymentStatus = "Paid" /* paid */;
44153
+ payload.amountPaid;
44154
+ payload.transaction_id;
44155
+ } else if (payload.paymentStatus === "Failed" /* failed */) {
44156
+ payload.amountPaid = 0;
44157
+ payload.paidBy = "";
44158
+ payload.datePaid = "";
44159
+ payload.paymentStatus = "Failed" /* failed */;
44160
+ payload.transaction_id;
44161
+ }
44162
+ const updatedAt = formatDateString(/* @__PURE__ */ new Date(), "mm-dd-yyyy");
44163
+ payload.updatedAt = updatedAt;
44164
+ const result = await collection().findOneAndUpdate({ _id: unitBillId }, { $set: payload }, { returnDocument: "after" });
44165
+ return result;
44166
+ } catch (error) {
44167
+ throw new Error(error.message || error || "Server Internal Error");
44168
+ }
44169
+ };
44170
+ const saveCreditCardInfo = async (payload) => {
44171
+ try {
44172
+ if (payload.userId)
44173
+ payload.userId = new import_mongodb119.ObjectId(payload.userId);
44174
+ if (payload.site)
44175
+ payload.site = new import_mongodb119.ObjectId(payload.site);
44176
+ if (payload.organization)
44177
+ payload.organization = new import_mongodb119.ObjectId(payload.organization);
44178
+ if (payload.createdBy)
44179
+ payload.createdBy = new import_mongodb119.ObjectId(payload.createdBy);
44180
+ const createdAt = formatDateString(/* @__PURE__ */ new Date(), "mm-dd-yyyy");
44181
+ payload.createdAt = createdAt;
44182
+ const result = await creditCollection().insertOne(new MCardInfo(payload));
44183
+ return result;
44184
+ } catch (error) {
44185
+ throw new Error(error.message || error || "Server Internal Error");
44186
+ }
44187
+ };
44188
+ const checkOutUnitBills = async (payload) => {
44189
+ try {
44190
+ if (payload.unitId)
44191
+ payload.unitId = new import_mongodb119.ObjectId(payload.unitId);
44192
+ if (payload.site)
44193
+ payload.site = new import_mongodb119.ObjectId(payload.site);
44194
+ if (payload.organization)
44195
+ payload.organization = new import_mongodb119.ObjectId(payload.organization);
44196
+ payload.createdAt = await formatDateString(/* @__PURE__ */ new Date(), "mm-dd-yyyy");
44197
+ const addToCart = await cartCollection().insertOne(new MUnitBillings(payload));
44198
+ const unitId = payload.unitId;
44199
+ const dateFormatted = formatDateString(/* @__PURE__ */ new Date(), "for-ref");
44200
+ const unit = unitId?.toString().slice(-4);
44201
+ const newId = new import_mongodb119.ObjectId(addToCart?.insertedId).toString().slice(-4);
44202
+ const referenceNumber = `CRT${unit}${newId}${dateFormatted}`;
44203
+ const result = await cartCollection().findOneAndUpdate({ _id: new import_mongodb119.ObjectId(addToCart.insertedId) }, { $set: { referenceNumber } }, { returnDocument: "after" });
44204
+ return result;
44205
+ } catch (error) {
44206
+ throw new Error(error.message || error || "Server Internal Error");
44207
+ }
44208
+ };
44209
+ const payUnitCart = async (refId, payload) => {
44210
+ try {
44211
+ if (refId)
44212
+ refId = refId.toString();
44213
+ const updatedAt = formatDateString(/* @__PURE__ */ new Date(), "mm-dd-yyyy");
44214
+ const cartInfo = await cartCollection().findOne({ referenceNumber: refId });
44215
+ const refNumber = cartInfo?.unitBill;
44216
+ if (payload.paymentStatus === "Paid" /* paid */ && payload.amountPaid) {
44217
+ payload.datePaid = /* @__PURE__ */ new Date();
44218
+ payload.paymentStatus = "Paid" /* paid */;
44219
+ payload.amountPaid;
44220
+ } else if (payload.paymentStatus === "Failed" /* failed */) {
44221
+ payload.amountPaid = 0;
44222
+ payload.paidBy = "";
44223
+ payload.datePaid = "";
44224
+ payload.paymentStatus = "Failed" /* failed */;
44225
+ }
44226
+ payload.updatedAt = updatedAt;
44227
+ const result = await cartCollection().findOneAndUpdate({ referenceNumber: refId }, { $set: payload }, { returnDocument: "after" });
44228
+ if (!result) {
44229
+ throw new Error("Failed to Process Transaction. Please Try Again Later");
44230
+ } else {
44231
+ for (const ref of refNumber) {
44232
+ const unitBillInfo = await collection().findOne({ referenceNumber: ref });
44233
+ const billId = unitBillInfo?.billId;
44234
+ const billInfo = await billCollection().findOne({ _id: new import_mongodb119.ObjectId(billId) });
44235
+ const billAmount = billInfo?.price;
44236
+ payload.updatedAt = updatedAt;
44237
+ payload.amountPaid = billAmount;
44238
+ const eachRef = await payUnitBill(ref, payload);
44239
+ }
44240
+ if (!cartInfo) {
44241
+ throw new Error("Cart info not found");
44242
+ }
44243
+ }
44244
+ return result;
44245
+ } catch (error) {
44246
+ throw new Error(error.message || error || "Server Internal Error");
44247
+ }
44248
+ };
44249
+ const getCheckedOutBillings = async (refId) => {
44250
+ try {
44251
+ if (refId)
44252
+ refId = refId.toString();
44253
+ const result = await cartCollection().aggregate([
44254
+ { $match: { referenceNumber: refId } },
44255
+ {
44256
+ $lookup: {
44257
+ from: "unit-bill",
44258
+ localField: "unitBill",
44259
+ foreignField: "referenceNumber",
44260
+ as: "unitBill"
44261
+ }
44262
+ },
44263
+ {
44264
+ $unwind: "$unitBill"
44265
+ },
44266
+ {
44267
+ $lookup: {
44268
+ from: "bill-records",
44269
+ localField: "unitBill.billId",
44270
+ foreignField: "_id",
44271
+ as: "billDetails"
44272
+ }
44273
+ },
44274
+ {
44275
+ $unwind: {
44276
+ path: "$billDetails",
44277
+ preserveNullAndEmptyArrays: true
44278
+ }
44279
+ },
44280
+ {
44281
+ $addFields: {
44282
+ "unitBill.billItemName": "$billDetails.billItem"
44283
+ }
44284
+ },
44285
+ {
44286
+ $project: {
44287
+ "unitBill.paidBy": 0,
44288
+ "unitBill.preDueDateAlert": 0,
44289
+ "unitBill.category": 0,
44290
+ "unitBill.contactNumber": 0,
44291
+ "unitBill.email": 0,
44292
+ "unitBill.site": 0,
44293
+ "unitBill.siteName": 0,
44294
+ "unitBill.organization": 0,
44295
+ "unitBill.billStatus": 0,
44296
+ "unitBill.createdBy": 0,
44297
+ "unitBill.createdAt": 0,
44298
+ "unitBill.updatedAt": 0,
44299
+ "billDetails": 0
44300
+ // Exclude the full billDetails as it's no longer needed
44301
+ }
44302
+ },
44303
+ {
44304
+ $group: {
44305
+ _id: "$_id",
44306
+ unitId: { $first: "$unitId" },
44307
+ referenceNumber: { $first: "$referenceNumber" },
44308
+ unitBill: { $push: "$unitBill" },
44309
+ paymentStatus: { $first: "$paymentStatus" },
44310
+ amountPaid: { $first: "$amountPaid" },
44311
+ site: { $first: "$site" },
44312
+ organization: { $first: "$organization" },
44313
+ paidBy: { $first: "$paidBy" },
44314
+ datePaid: { $first: "$referenceNumber" },
44315
+ billCategory: { $first: "$billCategory" },
44316
+ billFrom: { $first: "$billFrom" },
44317
+ billTo: { $first: "$billTo" },
44318
+ createdBy: { $first: "$createdBy" },
44319
+ createdAt: { $first: "$createdAt" },
44320
+ updatedAt: { $first: "$updatedAt" },
44321
+ transaction_id: { $first: "$transaction_id" }
44322
+ }
44323
+ }
44324
+ ]).toArray();
44325
+ if (!result || result.length === 0) {
44326
+ return { success: false, message: "Cart Info Not Found" };
44327
+ }
44328
+ return result[0];
44329
+ } catch (error) {
44330
+ throw new Error(error.message || error || "Server Internal Error");
44331
+ }
44332
+ };
44333
+ const cancelCheckedOutBillings = async (refId) => {
44334
+ try {
44335
+ if (refId)
44336
+ refId = refId.toString();
44337
+ const result = await cartCollection().findOneAndDelete({ referenceNumber: refId });
44338
+ if (!result) {
44339
+ return { success: false, message: "Cart Info Not Found" };
44340
+ }
44341
+ return { success: true, message: "Cart Deleted Successfully!", referenceNumber: result?.referenceNumber };
44342
+ } catch (error) {
44343
+ throw new Error(error.message || error || "Server Internal Error");
44344
+ }
44345
+ };
44346
+ return {
44347
+ saveCreditCardInfo,
44348
+ checkOutUnitBills,
44349
+ payUnitBill,
44350
+ payUnitCart,
44351
+ getCheckedOutBillings,
44352
+ cancelCheckedOutBillings
44353
+ };
44354
+ };
44355
+
44356
+ // src/repositories/reddot-payment.repository.ts
44357
+ var import_node_server_utils215 = require("@7365admin1/node-server-utils");
44358
+ var useRedDotPaymentRepo = () => {
44359
+ const getDB = () => {
44360
+ const db = import_node_server_utils215.useAtlas.getDb();
44361
+ if (!db) {
44362
+ throw new import_node_server_utils215.InternalServerError("Unable to connect to server.");
44363
+ }
44364
+ return db;
44365
+ };
44366
+ const collection = () => {
44367
+ return getDB().collection("unit-bill");
44368
+ };
44369
+ const orgCollection = () => {
44370
+ return getDB().collection("organizations");
44371
+ };
44372
+ const cartCollection = () => {
44373
+ return getDB().collection("checkout-cart");
44374
+ };
44375
+ const redDotMerchantCollection = () => {
44376
+ return getDB().collection("reddot-merchant");
44377
+ };
44378
+ const createRedDotAccount = async (_id, payload) => {
44379
+ try {
44380
+ const merchant = await redDotMerchantCollection().insertOne(payload);
44381
+ if (!merchant) {
44382
+ throw new Error("Failed to Add Merchant Details");
44383
+ }
44384
+ const merchantId = merchant?.insertedId;
44385
+ const orgId = new import_mongodb120.ObjectId(_id);
44386
+ const result = await orgCollection().findOneAndUpdate({ _id: orgId }, { $push: { merchant: merchantId } }, {
44387
+ returnDocument: "after"
44388
+ });
44389
+ if (!result) {
44390
+ throw new Error("Organization not found");
44391
+ }
44392
+ return {
44393
+ success: true,
44394
+ message: "Merchant Details successfully added."
44395
+ };
44396
+ } catch (error) {
44397
+ throw new Error(error.message || error || "Server Internal Error");
44398
+ }
44399
+ };
44400
+ const payUnitBillings = async (refId, payload) => {
44401
+ const checkCart = await cartCollection().findOne({ referenceNumber: refId });
44402
+ if (checkCart) {
44403
+ const multiple = await PaymentBillRepo().payUnitCart(refId, payload);
44404
+ if (multiple) {
44405
+ return await PaymentBillRepo().getCheckedOutBillings(refId);
44406
+ }
44407
+ }
44408
+ const single = await PaymentBillRepo().payUnitBill(refId, payload);
44409
+ return single;
44410
+ };
44411
+ const getMerchantDetailsById = async (_id) => {
44412
+ try {
44413
+ if (_id)
44414
+ _id = new import_mongodb120.ObjectId(_id);
44415
+ const result = await redDotMerchantCollection().aggregate([
44416
+ {
44417
+ $match: {
44418
+ _id
44419
+ }
44420
+ },
44421
+ {
44422
+ $facet: {
44423
+ totalCount: [{ $count: "count" }],
44424
+ items: [{ $sort: { _id: -1 } }]
44425
+ }
44426
+ }
44427
+ ]).toArray();
44428
+ const items = result[0].items;
44429
+ return items;
44430
+ } catch (error) {
44431
+ throw new Error(error.message || error || "Server Internal Error");
44432
+ }
44433
+ };
44434
+ return {
44435
+ createRedDotAccount,
44436
+ payUnitBillings,
44437
+ getMerchantDetailsById
44438
+ };
44439
+ };
44440
+
44441
+ // src/services/reddot-payment.service.ts
44442
+ var useRedDotPaymentSvc = () => {
44443
+ const createRedDotAccount = async (_id, payload) => {
44444
+ return useRedDotPaymentRepo().createRedDotAccount(_id, payload);
44445
+ };
44446
+ const redirectPaymentTransaction = async (payload) => {
44447
+ try {
44448
+ const SECRET_KEY2 = payload.merchant_key;
44449
+ if (!payload.mid && !payload.merchant_key) {
44450
+ return { success: false, message: "RDP Not Available on this account. Please Contact Management" };
44451
+ }
44452
+ const data = {
44453
+ mid: payload.mid,
44454
+ api_mode: "redirection_hosted",
44455
+ payment_type: "S",
44456
+ order_id: payload.order_id,
44457
+ ccy: "SGD",
44458
+ amount: payload.amount,
44459
+ back_url: payload.back_url,
44460
+ redirect_url: payload.redirect_url,
44461
+ notify_url: payload.notify_url
44462
+ };
44463
+ if (!SECRET_KEY2) {
44464
+ throw new Error("Merchant key is required but was not provided.");
44465
+ }
44466
+ data["signature"] = paymentSignature(data, SECRET_KEY2);
44467
+ const dataString = JSON.stringify(data);
44468
+ const headers = {
44469
+ "Content-Type": "application/json",
44470
+ "Content-Length": String(dataString.length)
44471
+ };
44472
+ if (!process.env.MERCHANT_PAYMENT) {
44473
+ throw new Error("MERCHANT_PAYMENT environment variable is not defined.");
44474
+ }
44475
+ const url = process.env.MERCHANT_PAYMENT;
44476
+ const response = await import_axios3.default.post(url, dataString, { headers });
44477
+ const result = response.data;
44478
+ return result;
44479
+ } catch (error) {
44480
+ return Promise.reject(error || error.message || "Server Internal Error!");
44481
+ }
44482
+ };
44483
+ const enquirePaymentTransaction = async (payload) => {
44484
+ try {
44485
+ const SECRET_KEY2 = payload.merchant_key;
44486
+ if (!payload.request_mid && !payload.merchant_key) {
44487
+ return { success: false, message: "RDP Not Available on this account. Please Contact Management" };
44488
+ }
44489
+ const data = {
44490
+ request_mid: payload.request_mid,
44491
+ transaction_id: payload.transaction_id
44492
+ };
44493
+ if (!SECRET_KEY2) {
44494
+ throw new Error("Merchant key is required but was not provided.");
44495
+ }
44496
+ data["signature"] = genericSignature(data, SECRET_KEY2);
44497
+ const dataString = JSON.stringify(data);
44498
+ const headers = {
44499
+ "Content-Type": "application/json"
44500
+ };
44501
+ if (!process.env.MERCHANT_ENQUIRY) {
44502
+ throw new Error("MERCHANT_PAYMENT environment variable is not defined.");
44503
+ }
44504
+ const url = process.env.MERCHANT_ENQUIRY;
44505
+ const response = await import_axios3.default.post(url, dataString, { headers });
44506
+ const result = response.data;
44507
+ const transactionId = result.transaction_id;
44508
+ const amount = result.request_amount || 0;
44509
+ const payer = result.payer_name || "";
44510
+ const refId = result.order_id;
44511
+ const message = result.response_msg;
44512
+ const invoiceDate = /* @__PURE__ */ new Date();
44513
+ if (result.response_code === "0") {
44514
+ const success = {
44515
+ method: payload.method,
44516
+ paymentStatus: "Paid" /* paid */,
44517
+ amountPaid: amount,
44518
+ paidBy: payer,
44519
+ updatedAt: invoiceDate,
44520
+ transaction_id: transactionId,
44521
+ message
44522
+ };
44523
+ await useRedDotPaymentRepo().payUnitBillings(refId, success);
44524
+ } else if (result.response_code !== "0") {
44525
+ const fail = {
44526
+ method: payload.method,
44527
+ paymentStatus: "Failed" /* failed */,
44528
+ updatedAt: invoiceDate,
44529
+ paidBy: "",
44530
+ amountPaid: "0",
44531
+ transaction_id: transactionId,
44532
+ message
44533
+ };
44534
+ await useRedDotPaymentRepo().payUnitBillings(refId, fail);
44535
+ }
44536
+ return result;
44537
+ } catch (error) {
44538
+ return Promise.reject(error || error.message || "Server Internal Error!");
44539
+ }
44540
+ };
44541
+ const payInvoice = async (payload) => {
44542
+ try {
44543
+ const data = {
44544
+ cardNumber: payload.cardNumber,
44545
+ rdpMid: payload.rdpMid,
44546
+ orderId: payload.orderId
44547
+ };
44548
+ const dataString = JSON.stringify(data);
44549
+ const headers = {
44550
+ "Content-Type": "application/json",
44551
+ "Content-Length": String(dataString.length)
44552
+ };
44553
+ if (!process.env.MERCHANT_PAYMENT) {
44554
+ throw new Error("MERCHANT_PAYMENT environment variable is not defined.");
44555
+ }
44556
+ const url = process.env.MERCHANT_PAYMENT;
44557
+ const response = await import_axios3.default.post(url, dataString, { headers });
44558
+ return response;
44559
+ } catch (error) {
44560
+ return Promise.reject(error || error.message || "Server Internal Error!");
44561
+ }
44562
+ };
44563
+ const getMerchantDetailsById = async (_id) => {
44564
+ return useRedDotPaymentRepo().getMerchantDetailsById(_id);
44565
+ };
44566
+ return {
44567
+ createRedDotAccount,
44568
+ redirectPaymentTransaction,
44569
+ enquirePaymentTransaction,
44570
+ payInvoice,
44571
+ getMerchantDetailsById
44572
+ };
44573
+ };
44574
+
44575
+ // src/controllers/reddot-payment.controller.ts
44576
+ var import_joi124 = __toESM(require("joi"));
44577
+ function useRedDotPaymentController() {
44578
+ const createRedDotAccount = async (req, res) => {
44579
+ try {
44580
+ const data = req.body;
44581
+ const id = req.params.id;
44582
+ const schema2 = import_joi124.default.object({
44583
+ id: import_joi124.default.string().hex().required(),
44584
+ // organization id
44585
+ paymentMethod: import_joi124.default.string().required(),
44586
+ // payment method (e.g. "Visa / Mastercard / PayNow QR Code")
44587
+ merchant_id: import_joi124.default.string().required(),
44588
+ // merchant id
44589
+ merchant_key: import_joi124.default.string().required()
44590
+ // secret key
44591
+ });
44592
+ const { error } = schema2.validate({ id, ...data });
44593
+ if (error) {
44594
+ return res.status(400).json({ data: null, message: error.message });
44595
+ }
44596
+ const result = await useRedDotPaymentSvc().createRedDotAccount(id, data);
44597
+ return res.json(result);
44598
+ } catch (error) {
44599
+ return res.status(500).json({ message: error.message || error });
44600
+ }
44601
+ };
44602
+ const redirectPaymentTransaction = async (req, res) => {
44603
+ try {
44604
+ const data = req.body;
44605
+ const schema2 = import_joi124.default.object({
44606
+ mid: import_joi124.default.string().optional().allow("", null),
44607
+ order_id: import_joi124.default.string().required(),
44608
+ amount: import_joi124.default.number().required(),
44609
+ merchant_key: import_joi124.default.string().required(),
44610
+ back_url: import_joi124.default.string().required(),
44611
+ redirect_url: import_joi124.default.string().required(),
44612
+ notify_url: import_joi124.default.string().required()
44613
+ });
44614
+ const { error } = schema2.validate({ ...data });
44615
+ if (error) {
44616
+ return res.status(400).json({ data: null, message: error.message });
44617
+ }
44618
+ const result = await useRedDotPaymentSvc().redirectPaymentTransaction(data);
44619
+ return res.json(result);
44620
+ } catch (error) {
44621
+ return res.status(500).json({ message: error.message || error });
44622
+ }
44623
+ };
44624
+ const enquirePaymentTransaction = async (req, res) => {
44625
+ try {
44626
+ const data = req.body;
44627
+ const schema2 = import_joi124.default.object({
44628
+ request_mid: import_joi124.default.string().optional().allow("", null),
44629
+ transaction_id: import_joi124.default.string().required(),
44630
+ merchant_key: import_joi124.default.string().optional().allow("", null),
44631
+ method: import_joi124.default.string().optional().allow("", null)
44632
+ });
44633
+ const { error } = schema2.validate({ ...data });
44634
+ if (error) {
44635
+ return res.status(400).json({ data: null, message: error.message });
44636
+ }
44637
+ const result = await useRedDotPaymentSvc().enquirePaymentTransaction(data);
44638
+ return res.json(result);
44639
+ } catch (error) {
44640
+ return res.status(500).json({ message: error.message || error });
44641
+ }
44642
+ };
44643
+ const getMerchantDetailsById = async (req, res, next) => {
44644
+ try {
44645
+ const _id = req.params.id;
44646
+ const schema2 = import_joi124.default.object({
44647
+ _id: import_joi124.default.string().hex().required()
44648
+ });
44649
+ const { error } = schema2.validate({ _id });
44650
+ if (error) {
44651
+ return res.status(400).json({ data: null, message: error.message });
44652
+ }
44653
+ const result = await useRedDotPaymentSvc().getMerchantDetailsById(_id);
44654
+ return res.json(result);
44655
+ } catch (error) {
44656
+ next(error);
44657
+ }
44658
+ };
44659
+ return {
44660
+ createRedDotAccount,
44661
+ redirectPaymentTransaction,
44662
+ enquirePaymentTransaction,
44663
+ getMerchantDetailsById
44664
+ };
44665
+ }
43951
44666
  // Annotate the CommonJS export names for ESM import in node:
43952
44667
  0 && (module.exports = {
43953
44668
  ANPRMode,
@@ -44275,6 +44990,9 @@ async function manpowerEvents(io) {
44275
44990
  usePriceRepo,
44276
44991
  usePromoCodeController,
44277
44992
  usePromoCodeRepo,
44993
+ useRedDotPaymentController,
44994
+ useRedDotPaymentRepo,
44995
+ useRedDotPaymentSvc,
44278
44996
  useRobotController,
44279
44997
  useRobotRepo,
44280
44998
  useRobotService,