Skip to content

Commit

Permalink
add: refund for user
Browse files Browse the repository at this point in the history
  • Loading branch information
augustus281 committed Jun 2, 2024
1 parent 8c26f36 commit d53897a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions server/src/controllers/payment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,65 @@ class PaymentController {
}
}

refundPaymentForUser = async (req, res, next) => {
try {
const { order_id, payment_id } = req.body
const order = await Order.findOne({
where: {
order_id: order_id,
payment_id: payment_id
}
})

if (!order) {
return res.status(404).json({ message: "Not found order to refund!" })
}

let date = new Date();
let createDate = moment(date).format('YYYYMMDDHHmmss');
process.env.TZ = 'Asia/Ho_Chi_Minh';

let ipAddr = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;

let amount = parseFloat(order.total_to_pay) * 0.8;
let bankCode = 'NCB';

let vnpUrl = url;
let currCode = 'VND';
let vnp_Params = {};
vnp_Params['vnp_Version'] = '2.1.0';
vnp_Params['vnp_Command'] = 'pay';
vnp_Params['vnp_TmnCode'] = tmnCode;
vnp_Params['vnp_Locale'] = 'vn';
vnp_Params['vnp_CurrCode'] = currCode;
vnp_Params['vnp_TxnRef'] = payment_id;
vnp_Params['vnp_OrderInfo'] = `Hoàn tiên cho khách hàng ${order.name_customer}`;
vnp_Params['vnp_OrderType'] = 'other';
vnp_Params['vnp_Amount'] = amount * 100;
vnp_Params['vnp_ReturnUrl'] = returnUrl;
vnp_Params['vnp_IpAddr'] = ipAddr;
vnp_Params['vnp_CreateDate'] = createDate;
vnp_Params['vnp_BankCode'] = bankCode;

vnp_Params = sortObject(vnp_Params);

let signData = querystring.stringify(vnp_Params, { encode: false });
let hmac = crypto.createHmac("sha512", secretKey);
vnp_Params['vnp_SecureHash'] = hmac.update(Buffer.from(signData, 'utf-8')).digest("hex");
vnpUrl += '?' + querystring.stringify(vnp_Params, { encode: false });

return res.status(200).json({
link_payment: vnpUrl,
order: order
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}

getRefundPayment = async (req, res, next) => {
try {
const vnp_Params = req.query;
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/payment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { authenticate } = require("../../middlewares/authenticate")
router.post("/", authenticate, asyncHandler(paymentController.createPaymentUrl))
router.post("/momo", asyncHandler(paymentController.paymentWithMomo))
router.get("/refund", authenticate, asyncHandler(paymentController.getRefundPayment))
router.post("/refund", authenticate, asyncHandler(paymentController.refundPayment))
router.post("/refund/user", authenticate, asyncHandler(paymentController.refundPaymentForUser))
router.get("/vnpay_ipn", authenticate, asyncHandler(paymentController.getResultPayment))

module.exports = router

0 comments on commit d53897a

Please sign in to comment.