IO-3355 Job Cash Discounting

Signed-off-by: Allan Carr <allan@imexsystems.ca>
This commit is contained in:
Allan Carr
2025-09-04 15:16:48 -07:00
parent 7f8c82b300
commit edaeb5d77a
2 changed files with 37 additions and 78 deletions

View File

@@ -20,35 +20,27 @@ export function JobTotalsCashDiscount({ bodyshop, amountDinero }) {
const notification = useNotification(); const notification = useNotification();
const fetchData = useCallback(async () => { const fetchData = useCallback(async () => {
if (amountDinero && bodyshop) { if (!amountDinero || !bodyshop) return;
setLoading(true);
let response;
try {
response = await axios.post("/intellipay/checkfee", {
bodyshop: { id: bodyshop.id, imexshopid: bodyshop.imexshopid, state: bodyshop.state },
amount: Dinero(amountDinero).toFormat("0.00")
});
if (response?.data?.error) { setLoading(true);
notification.open({ const errorMessage = "Error encountered when contacting IntelliPay service to determine cash discounted price.";
type: "error",
message: try {
response.data?.error || const { id, imexshopid, state } = bodyshop;
"Error encountered when contacting IntelliPay service to determine cash discounted price." const { data } = await axios.post("/intellipay/checkfee", {
}); bodyshop: { id, imexshopid, state },
} else { amount: Dinero(amountDinero).toUnit()
setFee(response.data?.fee || 0); });
}
} catch (error) { if (data?.error) {
notification.open({ notification.open({ type: "error", message: data.error || errorMessage });
type: "error", } else {
message: setFee(data?.fee ?? 0);
error.response?.data?.error ||
"Error encountered when contacting IntelliPay service to determine cash discounted price."
});
} finally {
setLoading(false);
} }
} catch (error) {
notification.open({ type: "error", message: error.response?.data?.error || errorMessage });
} finally {
setLoading(false);
} }
}, [amountDinero, bodyshop, notification]); }, [amountDinero, bodyshop, notification]);

View File

@@ -252,35 +252,27 @@ const generatePaymentUrl = async (req, res) => {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
const checkFee = async (req, res) => { const checkFee = async (req, res) => {
const logResponseMeta = { const { bodyshop = {}, amount } = req.body || {};
bodyshop: { const { id, imexshopid, shopname, state } = bodyshop;
id: req.body?.bodyshop?.id, const logResponseMeta = { bodyshop: { id, imexshopid, name: shopname, state }, amount };
imexshopid: req.body?.bodyshop?.imexshopid,
name: req.body?.bodyshop?.shopname,
state: req.body?.bodyshop?.state
},
amount: req.body?.amount
};
logger.log("intellipay-checkfee-request-received", "DEBUG", req.user?.email, null, logResponseMeta); logger.log("intellipay-checkfee-request-received", "DEBUG", req.user?.email, null, logResponseMeta);
if (!isNumber(req.body?.amount) || req.body?.amount <= 0) { if (!isNumber(amount) || amount <= 0) {
logger.log("intellipay-checkfee-skip", "DEBUG", req.user?.email, null, { logger.log("intellipay-checkfee-skip", "DEBUG", req.user?.email, null, {
message: "Amount is zero or undefined, skipping fee check.", message: "Amount is zero or undefined, skipping fee check.",
...logResponseMeta ...logResponseMeta
}); });
return res.json({ fee: 0 }); return res.json({ fee: 0 });
} }
const shopCredentials = await getShopCredentials(req.body.bodyshop); const shopCredentials = await getShopCredentials(bodyshop);
if (shopCredentials?.error) { if (shopCredentials?.error) {
logger.log("intellipay-checkfee-credentials-error", "ERROR", req.user?.email, null, { logger.log("intellipay-checkfee-credentials-error", "ERROR", req.user?.email, null, {
message: shopCredentials.error?.message, message: shopCredentials.error?.message,
...logResponseMeta ...logResponseMeta
}); });
return res.status(400).json({ error: shopCredentials.error?.message, ...logResponseMeta }); return res.status(400).json({ error: shopCredentials.error?.message, ...logResponseMeta });
} }
@@ -292,13 +284,10 @@ const checkFee = async (req, res) => {
{ {
method: "fee", method: "fee",
...shopCredentials, ...shopCredentials,
amount: req.body.amount, amount: String(amount), // Type cast to string as required by API
paymenttype: `CC`, paymenttype: "CC",
cardnum: "4111111111111111", // Required for compatibility with API cardnum: "4111111111111111", // Required for compatibility with API
state: state: state?.toUpperCase() || "ZZ"
req.body.bodyshop?.state && req.body.bodyshop.state.length === 2
? req.body.bodyshop.state.toUpperCase()
: "ZZ"
}, },
{ sort: false } // Ensure query string order is preserved { sort: false } // Ensure query string order is preserved
), ),
@@ -310,46 +299,24 @@ const checkFee = async (req, res) => {
...logResponseMeta ...logResponseMeta
}); });
const response = await axios(options); const { data } = await axios(options);
if (response.data?.error) { if (data?.error || data < 0) {
logger.log("intellipay-checkfee-api-error", "ERROR", req.user?.email, null, { const errorType = data?.error ? "intellipay-checkfee-api-error" : "intellipay-checkfee-negative-fee";
message: response.data?.error, const errorMessage = data?.error
...logResponseMeta ? data?.error
}); : "Fee amount negative. Check API credentials & account configuration.";
logger.log(errorType, "ERROR", req.user?.email, null, { message: errorMessage, data, ...logResponseMeta });
return res.status(400).json({ return res.status(400).json({ error: errorMessage, type: errorType, data, ...logResponseMeta });
error: response.data?.error,
type: "intellipay-checkfee-api-error",
...logResponseMeta
});
} }
if (response.data < 0) { logger.log("intellipay-checkfee-success", "DEBUG", req.user?.email, null, { fee: data, ...logResponseMeta });
logger.log("intellipay-checkfee-negative-fee", "ERROR", req.user?.email, null, { return res.json({ fee: data, ...logResponseMeta });
message: "Fee amount returned is negative.",
...logResponseMeta
});
return res.json({
error: "Fee amount negative. Check API credentials & account configuration.",
...logResponseMeta,
type: "intellipay-checkfee-negative-fee"
});
}
logger.log("intellipay-checkfee-success", "DEBUG", req.user?.email, null, {
fee: response.data,
...logResponseMeta
});
return res.json({ fee: response.data, ...logResponseMeta });
} catch (error) { } catch (error) {
logger.log("intellipay-checkfee-error", "ERROR", req.user?.email, null, { logger.log("intellipay-checkfee-error", "ERROR", req.user?.email, null, {
message: error?.message, message: error?.message,
...logResponseMeta ...logResponseMeta
}); });
return res.status(500).json({ error: error?.message, logResponseMeta }); return res.status(500).json({ error: error?.message, logResponseMeta });
} }
}; };