Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when use accesskey from test env #25

Open
dobahoan2792000 opened this issue Nov 15, 2021 · 1 comment
Open

Error when use accesskey from test env #25

dobahoan2792000 opened this issue Nov 15, 2021 · 1 comment

Comments

@dobahoan2792000
Copy link

Hey everyone, I'm working with momo in test env but i have a problem
My code (the info i get from momo account test they provided):

var partnerCode = "MOMOIQA420180417";
var accessKey = "SvDmj2cOTYZmQQ3H";
var secretkey = "PPuDXq1KowPT1ftR8DvlQTHhC03aul17";
var requestId = partnerCode + new Date().getTime();
var orderId = requestId;
var orderInfo = "Thanh toán qua ví MoMo";
var redirectUrl = "https://momo.vn/return";
var ipnUrl = "https://callback.url/notify";
// var ipnUrl = redirectUrl = "https://webhook.site/454e7b77-f177-4ece-8236-ddf1c26ba7f8";
var amount = "50000";
var requestType = "captureWallet"
var extraData = ""; //pass empty value if your merchant does not have stores

//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
//puts raw signature
console.log("--------------------RAW SIGNATURE----------------")
console.log(rawSignature)
//signature
const crypto = require('crypto');
var signature = crypto.createHmac('sha256', secretkey)
    .update(rawSignature)
    .digest('hex');
console.log(signature)

//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'
});
//Create the HTTPS objects
const https = require('https');
const options = {
    hostname: 'test-payment.momo.vn',
    port: 443,
    path: '/v2/gateway/api/create',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(requestBody)
    }
}
//Send the request and get the response
const req = https.request(options, res => {
    console.log(`Status: ${res.statusCode}`);
    console.log(`Headers: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (body) => {
        console.log('Body: ');
        console.log(body);
        console.log('payUrl: ');
        console.log(JSON.parse(body).payUrl);
    });
    res.on('end', () => {
        console.log('No more data in response.');
    });
})

req.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
});
// write data to request body
console.log("Sending....")
req.write(requestBody);
req.end();

Response:

accessKey=SvDmj2cOTYZmQQ3H&amount=50000&extraData=&ipnUrl=https://callback.url/notify&orderId=MOMOIQA4201804171636971265214&orderInfo=Thanh toán qua ví MoMo&partnerCode=MOMOIQA420180417&redirectUrl=https://momo.vn/return&requestId=MOMOIQA4201804171636971265214&requestType=captureWallet
7f08bd28142116610de927c4bd29d56289f4765804189e03d9017428d5986aa3
Sending....
Status: 400
Headers: {"server":"nginx","date":"Mon, 15 Nov 2021 10:14:25 GMT","content-type":"application/json","transfer-encoding":"chunked","connection":"close","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","pragma":"no-cache","x-kong-upstream-latency":"7","x-kong-proxy-latency":"0","via":"kong/0.14.1"}
Body: 
{"responseTime":1636971265425,"message":"Bad format request.","resultCode":20,"subErrors":[{"field":"signature","message":"Invalid signature. Check raw signature before signed. Raw data before hash: accessKey=*****&amount=50000&extraData=&ipnUrl=https://callback.url/notify&orderId=MOMOIQA4201804171636971265214&orderInfo=Thanh toán qua ví MoMo&partnerCode=MOMOIQA420180417&redirectUrl=https://momo.vn/return&requestId=MOMOIQA4201804171636971265214&requestType=captureWallet."}]}
payUrl: 
undefined
No more data in response.

The account info:
Screenshot from 2021-11-15 17-14-54

When i change access key from test env to access key of production env:
Screenshot from 2021-11-15 17-19-44

Response work fine:

accessKey=Q8gbQHeDesB2Xs0t&amount=50000&extraData=&ipnUrl=https://callback.url/notify&orderId=MOMOIQA4201804171636971639731&orderInfo=Thanh toán qua ví MoMo&partnerCode=MOMOIQA420180417&redirectUrl=https://momo.vn/return&requestId=MOMOIQA4201804171636971639731&requestType=captureWallet
289b90b29913d4a8f74f1cff7070ef720432603e3f488201b21bd445785a9c51
Sending....
Status: 200
Headers: {"server":"nginx","date":"Mon, 15 Nov 2021 10:20:39 GMT","content-type":"application/json","transfer-encoding":"chunked","connection":"close","vary":"Accept-Encoding","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","pragma":"no-cache","x-kong-upstream-latency":"35","x-kong-proxy-latency":"1","via":"kong/0.14.1","strict-transport-security":"max-age=15768000"}
Body: 
{"partnerCode":"MOMOIQA420180417","orderId":"MOMOIQA4201804171636971639731","requestId":"MOMOIQA4201804171636971639731","amount":50000,"responseTime":1636971639947,"message":"Successful.","resultCode":0,"payUrl":"https://test-payment.momo.vn/v2/gateway/pay?t=TU9NT0lRQTQyMDE4MDQxN3xNT01PSVFBNDIwMTgwNDE3MTYzNjk3MTYzOTczMQ==","deeplink":"momo://?action=payWithAppToken&amount=50000&cardId=&cardType=&cashInId=&cashInIdPay=&comment=&createdAt=1636971639946&deeplinkCallback=&description=Thanh+to%C3%A1n+qua+v%C3%AD+MoMo&discountCampaignAmount=0&discountCode=&extra=&extraData=&extras=&gatewayMerchantCode=MOMOIQA420180417&gatewaySessionId=TU9NT0lRQTQyMDE4MDQxN3xNT01PSVFBNDIwMTgwNDE3MTYzNjk3MTYzOTczMQ==&gatewayVersion=3.0&giftIds=&isScanQR=false&language=en&merchantcode=MOMOIQA420180417&merchantname=MoMoTest&merchantnamelabel=Nh%C3%A0+cung+c%E1%BA%A5p&moneylimitPerTrans=&orderId=MOMOIQA4201804171636971639731&orderLabel=M%C3%A3+%C4%91%C6%A1n+h%C3%A0ng&partnerCode=MOMOIQA420180417&partnerName=MoMoTest&pin=&prepaidIds=&requestId=MOMOIQA4201804171636971639731&requestType=payment&serviceType=app&signature=5c2df0dad71d8cec8737a2a9103bd4b3a6a8775d7d172d1fc3971dd2414ac62f&storeId=MOMOIQA420180417&storeName=MoMoTest&type=&urlSubmitToken=https%3A%2F%2Fmomo.vn&callBackUrl=https%3A%2F%2Fmomo.vn%2Freturn&deeplinkCallback=https%3A%2F%2Fmomo.vn%2Freturn","qrCodeUrl":"https://test-payment.momo.vn/v2/gateway/app?isScanQr=true&t=TU9NT0lRQTQyMDE4MDQxN3xNT01PSVFBNDIwMTgwNDE3MTYzNjk3MTYzOTczMQ=="}
payUrl: 
https://test-payment.momo.vn/v2/gateway/pay?t=TU9NT0lRQTQyMDE4MDQxN3xNT01PSVFBNDIwMTgwNDE3MTYzNjk3MTYzOTczMQ==
No more data in response.
@gunner13
Copy link

I got the same issue and resolved it by creating an account on test-business. and using the keys from this environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants