Commit 8dcdfabb by tdgiang

Add admin return/IPN handlers with signature verification

parent 6dffeac6
......@@ -71,3 +71,91 @@ exports.payPage = function (req, res) {
});
});
};
exports.epayReturn = function (req, res) {
var q = req.query;
var resultCd = q.resultCd;
var merTrxId = q.merTrxId;
if (resultCd !== "00_000") {
return res.render("admin/pay-result", {
success: false,
message: q.resultMsg || "Giao dịch không thành công",
});
}
AdminTransaction.findOne({ merTrxId: merTrxId }, function (err, tx) {
if (err || !tx) {
return res.render("admin/pay-result", {
success: false,
message: "Không tìm thấy giao dịch",
});
}
var expected = epaySign.signResponse(
resultCd,
tx.timeStamp,
tx.merTrxId,
q.trxId,
config.epay.merchant_id,
tx.amount,
config.epay.encode_key,
q.payToken
);
if (expected !== q.merchantToken) {
return res.render("admin/pay-result", {
success: false,
message: "Chữ ký không đúng",
});
}
return res.render("admin/pay-result", {
success: true,
message: "Thanh toán thành công",
});
});
};
exports.epayIPN = function (req, res) {
var b = req.body;
var resultCd = b.resultCd;
var merTrxId = b.merTrxId;
AdminTransaction.findOne({ merTrxId: merTrxId }, function (err, tx) {
if (err || !tx) {
return res.status(400).json({ code: "99", data: "TRX_NOT_FOUND" });
}
var expected = epaySign.signResponse(
resultCd,
tx.timeStamp,
tx.merTrxId,
b.trxId,
config.epay.merchant_id,
tx.amount,
config.epay.encode_key,
b.payToken
);
if (expected !== b.merchantToken) {
return res.status(400).json({ code: "99", data: "INVALID_SIGNATURE" });
}
var newStatus = resultCd === "00_000" ? "success" : "failed";
AdminTransaction.updateOne(
{ merTrxId: merTrxId },
{
status: newStatus,
resultMsg: b.resultMsg || "",
paidAt: newStatus === "success" ? new Date() : tx.paidAt,
},
function (updateErr) {
if (updateErr) {
return res.status(500).json({ code: "99", data: "DB_ERROR" });
}
return res.status(200).json({ code: "00", data: "Success" });
}
);
});
};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Kết quả thanh toán</title>
<style>
body { font-family: Arial, sans-serif; max-width: 480px; margin: 60px auto; text-align: center; }
</style>
</head>
<body>
{% if success %}
<h2 style="color:#276b45">Thanh toán thành công</h2>
{% else %}
<h2 style="color:#a3352a">Thanh toán thất bại</h2>
<p>{{message}}</p>
{% endif %}
</body>
</html>
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