Skip to content

Commit

Permalink
Payment with momo
Browse files Browse the repository at this point in the history
  • Loading branch information
augustus281 committed Jun 1, 2024
1 parent 5062f60 commit ec309df
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 3 deletions.
120 changes: 120 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"bignumber.js": "^9.1.2",
Expand Down
70 changes: 67 additions & 3 deletions server/src/controllers/payment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ let querystring = require('qs');
const crypto = require('crypto');
const { findTourById } = require('../services/tour.service');
const OrderItem = require('../models/order_item.model');
const OrderTour = require('../models/order_tour.model')
const User = require('../models/user.model')
const Order = require('../models/order.model');
const { StatusOrder } = require('../common/index');
const { updateTotalCart } = require('../services/cart.service');
const { findVoucherById } = require('../services/voucher.service');
const { Op } = require('sequelize');
const axios = require("axios")
const redis = require("redis")
let redisClient;
(async () => {
Expand Down Expand Up @@ -212,6 +210,72 @@ class PaymentController {
res.send({ code: "97" });
}
}

// PAYMENT WITH MOMO
paymentWithMomo = async (req, res, next) => {
try {
var partnerCode = process.env.PARTNER_CODE;
var accessKey = process.env.ACCESS_KEY;
var secretkey = process.env.SECRETKEY;
var requestId = partnerCode + new Date().getTime();
var orderId = requestId;
var orderInfo = "pay with MoMo";
var redirectUrl = "https://momo.vn/return";
var ipnUrl = "https://callback.url/notify";
var amount = "50000";
var requestType = "captureWallet"
var extraData = "";

//before sign HMAC SHA256 with format
//accessKey=$accessKey&amount=$amount&extraData=$extraData&ipnUrl=$ipnUrl&orderId=$orderId&orderInfo=$orderInfo&partnerCode=$partnerCode&redirectUrl=$redirectUrl&requestId=$requestId&requestType=$requestType
var rawSignature = "accessKey="+accessKey+"&amount=" + amount+"&extraData=" + extraData+"&ipnUrl=" + ipnUrl+"&orderId=" + orderId+"&orderInfo=" + orderInfo+"&partnerCode=" + partnerCode +"&redirectUrl=" + redirectUrl+"&requestId=" + requestId+"&requestType=" + requestType

//signature
const crypto = require('crypto');
var signature = crypto.createHmac('sha256', secretkey)
.update(rawSignature)
.digest('hex');

//json object send to MoMo endpoint
const requestBody = JSON.stringify({
partnerCode : partnerCode,
accessKey : accessKey,
requestId : requestId,
amount : amount,
orderId : orderId,
orderInfo : orderInfo,
redirectUrl : redirectUrl,
ipnUrl : ipnUrl,
extraData : extraData,
requestType : requestType,
signature : signature,
lang: 'en'
});



// options for axios
const options = {
method: 'POST',
url: 'https://test-payment.momo.vn/v2/gateway/api/create',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBody),
},
data: requestBody,
};


// Send the request and handle the response
let result;
result = await axios(options);
console.log("1")
return res.status(200).json(result.data);

} catch (error) {
return res.status(500).json({ message: error.message })
}
}
}

module.exports = new PaymentController()
1 change: 1 addition & 0 deletions server/src/routes/payment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const paymentController = require('../../controllers/payment.controller')
const { authenticate } = require("../../middlewares/authenticate")

router.post("/", authenticate, asyncHandler(paymentController.createPaymentUrl))
router.post("/momo", asyncHandler(paymentController.paymentWithMomo))
router.get("/vnpay_ipn", authenticate, asyncHandler(paymentController.getResultPayment))

module.exports = router

0 comments on commit ec309df

Please sign in to comment.