From 2111bea3445df7ab5d111e912ea980be863b2912 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 12 Sep 2017 02:43:24 +0300 Subject: [PATCH 1/2] Calculate commission method --- package.json | 3 +- src/RobokassaHelper.js | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ca70f9..51ef788 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/RobokassaHelper.js b/src/RobokassaHelper.js index 7c06537..cb89e03 100644 --- a/src/RobokassaHelper.js +++ b/src/RobokassaHelper.js @@ -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 = { @@ -281,6 +290,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; From 1897f64f270b4571f7a6ce8d46e38c53f376cd74 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 27 Sep 2017 23:55:46 +0300 Subject: [PATCH 2/2] cl --- src/RobokassaHelper.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/RobokassaHelper.js b/src/RobokassaHelper.js index cb89e03..03dc0d2 100644 --- a/src/RobokassaHelper.js +++ b/src/RobokassaHelper.js @@ -138,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; } /**