Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"bluebird": "^3.4.6",
"lodash": "^4.17.2"
"lodash": "^4.17.2",
"xml2js": "^0.4.19"
},
"devDependencies": {},
"keywords": [
Expand Down
73 changes: 71 additions & 2 deletions src/RobokassaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ const crypto = require('crypto');
const url = require('url');
const _ = require('lodash');
const Promise = require('bluebird');
const https = require('https');
const xml2js = require('xml2js');

var parser = new xml2js.Parser({
trim:true,
normalize:true,
normalizeTags:true,
explicitRoot:false
});
parseString = parser.parseString;

const DEFAULT_CONFIG = {

Expand Down Expand Up @@ -129,10 +138,12 @@ class RobokassaHelper {
values = values.concat(userData.sort());
}

return this.calculateHash(
let hash = this.calculateHash(
values.join(':')
);

console.log(`Values: ${values.join(':')}`)
console.log(`Hash: ${hash}`)
return hash;
}

/**
Expand Down Expand Up @@ -281,6 +292,64 @@ class RobokassaHelper {

}

/**
* Returns your summ plus service commission
*
* @param {number} outSum
* @param {string} [lang=en]
* @param [callback]
*
*/
calculateCommission (outSum,lang='en',callback){
return new Promise((resolve,reject)=>{
if(typeof lang==='function'){
callback=lang;
lang='en';
}
if(!callback){
callback = function(err,result){
if(err) return reject(err);
return resolve(result);
};
}
if(typeof outSum==='undefined') return callback(new TypeError());

let req = https.request(`https://auth.robokassa.ru/Merchant/WebService/Service.asmx/GetRates?MerchantLogin=${this.config.merchantLogin}&IncCurrLabel=&OutSum=${outSum}&Language=${lang}`,res=>{
let body = '';
res.setEncoding('utf8');
res.on('data',chunk => {body+=chunk});
res.on('end', () => {
parseString(body, (err,parsed)=>{
if(err) return callback(err);
try{
var result = parsed.groups[0].group.map(group=>{
return {
code:group.$.Code,
description:group.$.Description,
items:group.items[0].currency.map(currency=>{
return {
label:currency.$.Label,
alias:currency.$.Alias,
name:currency.$.Name,
minValue:currency.$.MinValue,
maxValue:currency.$.MaxValue,
incSum:currency.rate[0].$.IncSum
}
})
};
});
}catch(err){
return callback(err);
}
callback(null,result)
});
});
});
req.on('error',callback);
req.end();
})
}

}

module.exports = RobokassaHelper;