From 0ef2814de309b0f414a6fc20c5195ef25750f86c Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Tue, 1 Apr 2025 12:33:41 -0400 Subject: [PATCH] feature/IO-2885-IntelliPay-App-Postback-Support - Clean intellipay.js, add new route scaffolding --- server/intellipay/intellipay.js | 75 +++++++++++++++++++++++++------ server/routes/intellipayRoutes.js | 20 ++++++--- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/server/intellipay/intellipay.js b/server/intellipay/intellipay.js index 4af4a50cc..a58d45240 100644 --- a/server/intellipay/intellipay.js +++ b/server/intellipay/intellipay.js @@ -1,7 +1,3 @@ -const path = require("path"); -require("dotenv").config({ - path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`) -}); const queries = require("../graphql-client/queries"); const Dinero = require("dinero.js"); const qs = require("query-string"); @@ -10,18 +6,22 @@ const moment = require("moment"); const logger = require("../utils/logger"); const { sendTaskEmail } = require("../email/sendemail"); const generateEmailTemplate = require("../email/generateTemplate"); - -const domain = process.env.NODE_ENV ? "secure" : "test"; - const { SecretsManagerClient, GetSecretValueCommand } = require("@aws-sdk/client-secrets-manager"); const { InstanceRegion, InstanceEndpoints } = require("../utils/instanceMgr"); +const domain = process.env.NODE_ENV ? "secure" : "test"; + const client = new SecretsManagerClient({ region: InstanceRegion() }); const gqlClient = require("../graphql-client/graphql-client").client; +/** + * @description Get shop credentials from AWS Secrets Manager + * @param bodyshop + * @returns {Promise<{error}|{merchantkey: *, apikey: *}|any>} + */ const getShopCredentials = async (bodyshop) => { // Development only if (process.env.NODE_ENV === undefined) { @@ -49,16 +49,27 @@ const getShopCredentials = async (bodyshop) => { } }; +/** + * @description Decode the comment from base64 + * @param comment + * @returns {any|null} + */ const decodeComment = (comment) => { try { return comment ? JSON.parse(Buffer.from(comment, "base64").toString()) : null; - // eslint-disable-next-line no-unused-vars + // eslint-disable-next-line no-unused-vars } catch (error) { return null; // Handle malformed base64 string gracefully } }; -exports.lightbox_credentials = async (req, res) => { +/** + * @description Get lightbox credentials for the shop + * @param req + * @param res + * @returns {Promise} + */ +const lightboxCredentials = async (req, res) => { const decodedComment = decodeComment(req.body?.comment); const logMeta = { iPayData: req.body?.iPayData, @@ -119,7 +130,13 @@ exports.lightbox_credentials = async (req, res) => { } }; -exports.payment_refund = async (req, res) => { +/** + * @description Process payment refund + * @param req + * @param res + * @returns {Promise} + */ +const paymentRefund = async (req, res) => { const decodedComment = decodeComment(req.body.iPayData?.comment); const logResponseMeta = { iPayData: req.body?.iPayData, @@ -190,7 +207,13 @@ exports.payment_refund = async (req, res) => { } }; -exports.generate_payment_url = async (req, res) => { +/** + * @description Generate payment URL for the shop + * @param req + * @param res + * @returns {Promise} + */ +const generatePaymentUrl = async (req, res) => { const decodedComment = decodeComment(req.body.comment); const logResponseMeta = { iPayData: req.body?.iPayData, @@ -261,8 +284,14 @@ exports.generate_payment_url = async (req, res) => { } }; -//Reference: https://intellipay.com/dist/webapi26.html#operation/fee -exports.checkfee = async (req, res) => { +/** + * @description Check the fee for a given amount + * Reference: https://intellipay.com/dist/webapi26.html#operation/fee + * @param req + * @param res + * @returns {Promise} + */ +const checkFee = async (req, res) => { const logResponseMeta = { bodyshop: { id: req.body?.bodyshop?.id, @@ -359,7 +388,13 @@ exports.checkfee = async (req, res) => { } }; -exports.postback = async (req, res) => { +/** + * @description Handle the postback from Intellipay + * @param req + * @param res + * @returns {Promise} + */ +const postBack = async (req, res) => { const { body: values } = req; const decodedComment = decodeComment(values?.comment); @@ -474,6 +509,7 @@ exports.postback = async (req, res) => { const bodyshop = await gqlClient.request(queries.GET_BODYSHOP_BY_ID, { id: job.jobs_by_pk.shopid }); + const ipMapping = bodyshop.bodyshops_by_pk.intellipay_config?.payment_map; logger.log("intellipay-postback-invoice-job-fetched", "DEBUG", "api", null, { @@ -525,3 +561,14 @@ exports.postback = async (req, res) => { res.status(400).json({ successful: false, error: error.message, ...logResponseMeta }); } }; + +const postBackCallBack = async (req, res) => {}; + +module.exports = { + lightboxCredentials, + paymentRefund, + generatePaymentUrl, + checkFee, + postBack, + postBackCallBack +}; diff --git a/server/routes/intellipayRoutes.js b/server/routes/intellipayRoutes.js index ad0b87323..7c3a2ecec 100644 --- a/server/routes/intellipayRoutes.js +++ b/server/routes/intellipayRoutes.js @@ -1,12 +1,20 @@ const express = require("express"); const router = express.Router(); const validateFirebaseIdTokenMiddleware = require("../middleware/validateFirebaseIdTokenMiddleware"); -const { lightbox_credentials, payment_refund, generate_payment_url, postback, checkfee } = require("../intellipay/intellipay"); +const { + lightboxCredentials, + paymentRefund, + generatePaymentUrl, + postBack, + checkFee, + postBackCallBack +} = require("../intellipay/intellipay"); -router.post("/lightbox_credentials", validateFirebaseIdTokenMiddleware, lightbox_credentials); -router.post("/payment_refund", validateFirebaseIdTokenMiddleware, payment_refund); -router.post("/generate_payment_url", validateFirebaseIdTokenMiddleware, generate_payment_url); -router.post("/checkfee", validateFirebaseIdTokenMiddleware, checkfee); -router.post("/postback", postback); +router.post("/lightbox_credentials", validateFirebaseIdTokenMiddleware, lightboxCredentials); +router.post("/payment_refund", validateFirebaseIdTokenMiddleware, paymentRefund); +router.post("/generate_payment_url", validateFirebaseIdTokenMiddleware, generatePaymentUrl); +router.post("/checkfee", validateFirebaseIdTokenMiddleware, checkFee); +router.post("/postback", postBack); +router.post("/postback-callback", postBackCallBack); module.exports = router;