Commit 9c6735e3 by tdgiang

Create Sổ Bán Lẻ order before payment transaction in createTransaction

createTransaction now calls SobanleClient.createAutoOrder(customerData,
targetAmount, cb) before writing an AdminTransaction. The transaction's
amount is set from the real Sổ Bán Lẻ order total (order.orderTotal)
rather than the admin's raw input amount, and sobanleOrderId is stored
for reconciliation. On Sổ Bán Lẻ failure, returns 502
{code:"99", data:"SOBANLE_ORDER_FAILED"} with no DB row written. The
success response contract ({code:"00", data:{merTrxId, paymentUrl}}) is
unchanged.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017KPzWwuTEeX2vXGXvyGn4q
parent 80aebf49
......@@ -5,6 +5,7 @@ var config = require(__config_path + "/config");
var AdminTransaction = require("../models/AdminTransaction");
var AdminUser = require("../models/AdminUser");
var epaySign = require("../libs/epaySign");
var SobanleClient = require("../libs/SobanleClient");
exports.newTransactionForm = function (req, res) {
res.render("admin/transactions-new", {
......@@ -23,47 +24,68 @@ exports.createTransaction = function (req, res) {
return res.status(400).json({ code: "99", data: "MISSING_FIELDS" });
}
var amount = parseInt(rawAmount, 10);
if (!isFinite(amount) || amount <= 0 || String(amount) !== String(rawAmount).trim()) {
var targetAmount = parseInt(rawAmount, 10);
if (!isFinite(targetAmount) || targetAmount <= 0 || String(targetAmount) !== String(rawAmount).trim()) {
return res.status(400).json({ code: "99", data: "INVALID_AMOUNT" });
}
var timeStamp = moment().format("YYYYMMDDHHmmss");
var uniqueSuffix = uuidv4().split("-")[0];
var merTrxId = "HY_" + timeStamp + "_" + uniqueSuffix;
var transCode = "HY_RT_" + timeStamp + "_" + uniqueSuffix;
var merchantToken = epaySign.signRequest(
timeStamp,
merTrxId,
config.epay.merchant_id,
amount,
config.epay.encode_key
);
var customerData = {
customer_group_id: 1,
customer_type: 2,
phone_number: customerPhone,
tax_no: null,
name: customerName,
address: customerAddress,
id_card_number: null,
passport_number: null,
email: null,
};
AdminTransaction.create(
{
merTrxId: merTrxId,
transCode: transCode,
customerName: customerName,
customerPhone: customerPhone,
customerAddress: customerAddress,
amount: amount,
merchantToken: merchantToken,
timeStamp: timeStamp,
createdByUsername: req.session.username,
},
function (err, tx) {
if (err) {
console.error("createTransaction: DB error:", err.message);
return res.status(500).json({ code: "99", data: "DB_ERROR" });
}
var paymentUrl = config.epay.req_domain + "/admin/pay/" + tx.merTrxId;
return res.status(200).json({
code: "00",
data: { merTrxId: tx.merTrxId, paymentUrl: paymentUrl },
});
SobanleClient.createAutoOrder(customerData, targetAmount, function (sobanleErr, order) {
if (sobanleErr) {
console.error("createTransaction: SobanleClient error:", sobanleErr.message);
return res.status(502).json({ code: "99", data: "SOBANLE_ORDER_FAILED" });
}
);
var amount = order.orderTotal;
var timeStamp = moment().format("YYYYMMDDHHmmss");
var uniqueSuffix = uuidv4().split("-")[0];
var merTrxId = "HY_" + timeStamp + "_" + uniqueSuffix;
var transCode = "HY_RT_" + timeStamp + "_" + uniqueSuffix;
var merchantToken = epaySign.signRequest(
timeStamp,
merTrxId,
config.epay.merchant_id,
amount,
config.epay.encode_key
);
AdminTransaction.create(
{
merTrxId: merTrxId,
transCode: transCode,
customerName: customerName,
customerPhone: customerPhone,
customerAddress: customerAddress,
amount: amount,
sobanleOrderId: String(order.orderId),
merchantToken: merchantToken,
timeStamp: timeStamp,
createdByUsername: req.session.username,
},
function (err, tx) {
if (err) {
console.error("createTransaction: DB error:", err.message);
return res.status(500).json({ code: "99", data: "DB_ERROR" });
}
var paymentUrl = config.epay.req_domain + "/admin/pay/" + tx.merTrxId;
return res.status(200).json({
code: "00",
data: { merTrxId: tx.merTrxId, paymentUrl: paymentUrl },
});
}
);
});
};
exports.payPage = function (req, res) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment