Commit ebb83f38d5fedae18fd2ce0649a91060f6805782

Authored by Adhidarma Hadiwinoto
1 parent af37cc8280
Exists in master

alpha version

Showing 3 changed files with 153 additions and 2 deletions Side-by-side Diff

... ... @@ -4,7 +4,7 @@
4 4 "description": "ST24 to CJK",
5 5 "main": "index.js",
6 6 "scripts": {
7   - "test": "echo \"Error: no test specified\" && exit 1"
  7 + "test": "mocha"
8 8 },
9 9 "repository": {
10 10 "type": "git",
... ... @@ -19,9 +19,14 @@
19 19 "author": "Adhidarma Hadiwinoto <adhisimon@gmail.com>",
20 20 "license": "ISC",
21 21 "dependencies": {
  22 + "crypto": "0.0.3",
22 23 "ini": "^1.3.4",
23 24 "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git",
24 25 "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git",
25   - "winston": "^2.2.0"
  26 + "winston": "^2.2.0",
  27 + "xml": "^1.0.1"
  28 + },
  29 + "devDependencies": {
  30 + "should": "^8.3.1"
26 31 }
27 32 }
... ... @@ -0,0 +1,114 @@
  1 +var winston = require('winston');
  2 +var crypto = require('crypto');
  3 +var xml = require('xml');
  4 +var url = require('url');
  5 +
  6 +var config;
  7 +var callbackReport;
  8 +var aaa;
  9 +var logger;
  10 +var options;
  11 +
  12 +function start(_config, _callbackReport, options) {
  13 + config = _config;
  14 + callbackReport = _callbackReport
  15 +
  16 + if (options && options.aaa) {
  17 + aaa = options.aaa;
  18 + }
  19 +
  20 + if (options && options.logger) {
  21 + logger = options.logger;
  22 + } else {
  23 + logger = new winston.Logger({
  24 + transports: [
  25 + new (winston.transports.Console)()
  26 + ]
  27 + });
  28 + }
  29 +}
  30 +
  31 +function calculateSignature(params) {
  32 + return crypto.createHash('sha256').update(
  33 + params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid
  34 + ).digest().toString('hex');
  35 +}
  36 +
  37 +function createXmlPayload(params) {
  38 + return "<?xml version=\"1.0\" ?>\n" + xml({
  39 + ciwaru: [
  40 + {trxtype: params.trxtype},
  41 + {prdcode: params.prdcode},
  42 + {value: params.value},
  43 + {msisdn: params.msisdn},
  44 + {trxid: params.trxid},
  45 + {uid: params.uid},
  46 + {hash: calculateSignature(params)}
  47 + ]
  48 + });
  49 +}
  50 +
  51 +function getSNFromMessage(message) {
  52 + var sn_match = message.match(/SN (\w+) /);
  53 + return sn_match[1];
  54 +}
  55 +
  56 +function topupResponseHandler(body) {
  57 + logger.info('Got reply from partner', {body: body});
  58 + xml2js(body, function(err, result) {
  59 + if (err) {
  60 + logger.warn('XML parsing error', {err: err});
  61 + }
  62 + logger.info('XML message from partner', {result: result});
  63 + });
  64 +}
  65 +
  66 +function topupRequest(task, retry) {
  67 + var remoteProduct = task.remoteProduct.split(',');
  68 +
  69 + var params = {
  70 + trxtype: '01',
  71 + prdcode: remoteProduct[0],
  72 + value: remoteProduct[1],
  73 + msisdn: task.destination,
  74 + trxid: task.requestId,
  75 + uid: config.h2h_out.userid,
  76 + };
  77 +
  78 + var postBody = createXmlPayload(params);
  79 +
  80 + var partnerUrl = url.parse(config.h2h_out.partner);
  81 + var postRequest = {
  82 + host: partnerUrl.hostname,
  83 + path: partnerUrl.path,
  84 + port: partnerUrl.port,
  85 + method: "POST",
  86 + headers: {
  87 + 'Content-Type': 'text/xml',
  88 + 'Content-Length': Buffer.byteLength(postBody)
  89 + }
  90 + };
  91 +
  92 + logger.info('POST to partner', {postRequest: postRequest});
  93 + var req = http.request(postRequest, function( res ) {
  94 +
  95 + logger.info('Status code: ' + res.statusCode );
  96 + var buffer = "";
  97 + res.on( "data", function( data ) { buffer = buffer + data; } );
  98 + res.on( "end", function( data ) {
  99 + topupResponseHandler(buffer);
  100 + });
  101 + });
  102 +
  103 + req.on('error', function(e) {
  104 + logger.warn('problem with request: ' + e.message);
  105 + callbackReport(task['requestId'], '40', e.message);
  106 + });
  107 +
  108 + req.write(postBody);
  109 + req.end();
  110 +}
  111 +
  112 +exports.calculateSignature = calculateSignature;
  113 +exports.createXmlPayload = createXmlPayload;
  114 +exports.getSNFromMessage = getSNFromMessage;
... ... @@ -0,0 +1,32 @@
  1 +var should = require("should");
  2 +var partner = require("./partner-cjk");
  3 +
  4 +describe("#partner-cjk", function() {
  5 + var params = {
  6 + trxtype: '01',
  7 + prdcode: 'PU1TS10',
  8 + value: '10000',
  9 + msisdn: '082129777024',
  10 + trxid: '1237',
  11 + uid: '082129777025',
  12 + };
  13 +
  14 + describe("#createSign", function() {
  15 + it('should return correct sign', function() {
  16 + partner.calculateSignature(params).should.equal('825216ec9a4c20f2b43cb31d36fd2ce3ecfac5f58f1d6f73148d2063a357cfa8');
  17 + });
  18 + });
  19 +
  20 + describe('#createXmlPayload', function() {
  21 + it('should return correct xml', function() {
  22 + partner.createXmlPayload(params).should.equal('<?xml version="1.0" ?>\n<ciwaru><trxtype>01</trxtype><prdcode>PU1TS10</prdcode><value>10000</value><msisdn>082129777024</msisdn><trxid>1237</trxid><uid>082129777025</uid><hash>825216ec9a4c20f2b43cb31d36fd2ce3ecfac5f58f1d6f73148d2063a357cfa8</hash></ciwaru>');
  23 + });
  24 + });
  25 +
  26 + describe('#getSNFromMessage', function() {
  27 + it('should return correct sn', function() {
  28 + var message = '2014-­09-­16 01:40:10: SUKSES Topup ke 082129777024 sebesar 20000 dengan SN 0031000529115447 harga = 20056; Saldo = 221.492';
  29 + partner.getSNFromMessage(message).should.equal('0031000529115447');
  30 + });
  31 + });
  32 +});