partner-kospinjasa.js 3.87 KB
var winston = require('winston');
var soap = require('soap');
var crypto = require('crypto');
var strftime = require('strftime');
var easysoap = require('easysoap');

var max_retry = 10;
var sleep_before_retry = 5000;

var config;
var callbackReport;
var aaa;
var logger;
var options;

function start(_config, _callbackReport, options) {
    config = _config;
    callbackReport = _callbackReport

    if (options && options.aaa) {
            aaa = options.aaa;
    }

    if (options && options.logger) {
        logger = options.logger;
    } else {
        logger = new winston.Logger({
            transports: [
              new (winston.transports.Console)()
            ]
        });
    }
}

function topupRequest(task, retry) {
    if (retry === undefined) {
        retry = max_retry;
    }

    var remoteProduct = task.remoteProduct.split(',');
    var params = {
        userName: config.h2h_out.userid,
        productCode: remoteProduct[0] ,
        terminal: 'terminal0',
        transactionType: '50',
        billNumber: createBillNumber(task.destination),
        amount: remoteProduct[1],
        bit61: createBillNumber(task.destination),
        reff: task.requestId,
        timeStamp: strftime('%d-%m-%Y %H:%M:%S', new Date())
    }

    var signature = createSignature(params, config.h2h_out.password);
    params.signature = signature;

    topupRequestSoap(task, params, retry);
}

function topupRequestEasySoap(task, args, retry) {
    //partner=http://203.130.243.155/ApiH2H/index.php?wsdl

    var clientParams = {
        host: '203.130.243.155',
        path: '/ApiH2H/index.php',
        wsdl: '/ApiH2H/index.php?wsdl'
    }

    var clientOptions = {
        secure: false,
    }

    var soapClient = new easysoap.Client(clientParams, clientOptions);
    soapClient.call(
        {method: 'billpayment', params: args}
    ).done(
        //success
        function(res) {
            res.data        // response data as array
            res.response    // full response data (including xml)
            res.header      // response header
        },

        //method fail
        function(err) {
            console.log(err);
        }
    );

    callbackReport(task.requestId, '68', 'debug');
}

function topupRequestSoap(task, params, retry) {

    soap.createClient(config.h2h_out.partner, function(err, soapClient) {

        logger.info('Requesting to service', {url: config.h2h_out.partner, params: params});

        soapClient.apih2h.apih2hPort.billpayment({inputCheck: params}, function(err, result) {

            if (err) {
                logger.warn('Error requesting service', {err: err});
                callbackReport(task.requestId, '68', 'something wrong');
                return;
            }

            var responseMessageToST24 = result.outputParameter.resultCode.$value + ' - ' + result.outputParameter.resultDesc.$value;

            logger.info('Got result: ' + responseMessageToST24, {result: result});

            callbackReport(task.requestId, '68', responseMessageToST24);
        });
    });
}



function createSignature(params, password) {
    var passwordHash = crypto.createHash('sha256').update(password).digest().toString('hex');

    var plain =
        params.userName
        + passwordHash
        + params.productCode
        + params.terminal
        + params.transactionType
        + params.billNumber
        + params.amount
        + params.reff
        + params.timeStamp;

    var result = crypto.createHash('sha1').update(plain).digest().toString('hex');

    if (logger) {
        logger.verbose('Calculating signature', {plain: plain, result: result, params: params, password: password});
    }

    return result;
}

function createBillNumber(destination) {
    return ("0000000000000" + destination).slice(-13);
}

exports.start = start;
exports.topupRequest = topupRequest;
exports.createSignature = createSignature;
exports.createBillNumber = createBillNumber;