Commit aefd596682f5c25a6d8d0197fbf576540d522f51

Authored by Adhidarma Hadiwinoto
1 parent f4ad0f9a45
Exists in master

ready to test

Showing 2 changed files with 135 additions and 0 deletions Side-by-side Diff

partner-simplepay.js
... ... @@ -0,0 +1,117 @@
  1 +"use strict";
  2 +
  3 +const request = require('request');
  4 +const crypto = require('crypto');
  5 +const moment = require('moment');
  6 +
  7 +const http = require('http');
  8 +http.globalAgent.maxSockets = Infinity;
  9 +
  10 +var config;
  11 +var aaa;
  12 +var logger;
  13 +
  14 +function start(options) {
  15 + if (!options) {
  16 + console.log('Undefined options, terminating....');
  17 + process.exit(1);
  18 + }
  19 +
  20 + if (options.config) {
  21 + config = options.config;
  22 + } else {
  23 + console.log('Undefined options.config, terminating....')
  24 + process.exit(1);
  25 + }
  26 +
  27 + if (options.aaa) {
  28 + aaa = options.aaa;
  29 + } else {
  30 + console.log('Undefined options.aaa, terminating....')
  31 + process.exit(1);
  32 + }
  33 +
  34 + if (options && options.logger) {
  35 + logger = options.logger;
  36 + } else {
  37 + console.log('Undefined options.logger, terminating....')
  38 + process.exit(1);
  39 + }
  40 +}
  41 +
  42 +function callbackReport(requestId, rc, message) {
  43 + aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
  44 +}
  45 +
  46 +function calculateSign(dt, trx_ref_id, cust_num, username, password) {
  47 + const cryptoHashPassword = crypto.createHash('sha1');
  48 + const cryptoHashUsernamePassword = crypto.createHash('sha1');
  49 + const cryptoHashOutest = crypto.createHash('sha1');
  50 +
  51 + cryptoHashPassword.update(password);
  52 + const hashPassword = cryptoHashPassword.digest('hex');
  53 +
  54 + cryptoHashUsernamePassword.update(username + hashPassword);
  55 + const hashUsernamePassword = cryptoHashUsernamePassword.digest('hex');
  56 +
  57 + cryptoHashOutest.update(dt + trx_ref_id + cust_num + hashUsernamePassword);
  58 + return cryptoHashOutest.digest('hex');
  59 +}
  60 +
  61 +function _hitTopup(task, pendingOnErrorConnect) {
  62 +
  63 + const dt = moment().format('YYYY-MM-DD HH:mm:ss');
  64 + const username = config.h2h_out.username || config.h2h_out.userid;
  65 + const password = config.h2h_out.password || config.h2h_out.pin;
  66 + const sign = calculateSign(dt, task.requestId, task.destination, username, password);
  67 +
  68 + const requestOptions = {
  69 + url: config.h2h_out.partner,
  70 + qs: {
  71 + username: username,
  72 + datetime: dt,
  73 + code: task.remoteProduct,
  74 + trx_ref_id: task.requestId,
  75 + cust_num: task.destination,
  76 + sign: sign
  77 + }
  78 + }
  79 +
  80 + request(requestOptions, function(error, response, body) {
  81 + if (error) {
  82 + let rc = '68';
  83 +
  84 + if (!pendingOnErrorConnect && (error.syscall == 'connect')) {
  85 + rc = '91';
  86 + }
  87 +
  88 + logger.warn('Error requesting to partner', {task: task, rc: rc, error: error});
  89 + callbackReport(task.requestId, rc, 'Error requesting to partner. ' + error);
  90 + return;
  91 + }
  92 +
  93 + if (response.statusCode != 200) {
  94 + let rc = '68';
  95 +
  96 + logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode});
  97 + callbackReport(task.requestId, rc, 'HTTP status code ' + response.statusCode);
  98 + return;
  99 + }
  100 +
  101 + logger.info('Transaksi sedang diproses', {task: task, response_body: body});
  102 + callbackReport(task.requestId, '68', 'Traksaksi sedang diproses');
  103 + })
  104 +}
  105 +
  106 +function topupRequest(task) {
  107 + _hitTopup(task);
  108 +}
  109 +
  110 +function checkStatus(task) {
  111 + _hitTopup(task, true);
  112 +}
  113 +
  114 +exports.calculateSign = calculateSign;
  115 +exports.start = start;
  116 +exports.topupRequest = topupRequest;
  117 +exports.checkStatus = checkStatus;
... ... @@ -0,0 +1,18 @@
  1 +"use strict";
  2 +
  3 +var should = require('should');
  4 +var partner = require('./partner-simplepay');
  5 +
  6 +describe('#partner', function() {
  7 + describe('_calculateSign', function() {
  8 + it('using api example parameter should return correct sign', function() {
  9 + let datetime = '2017-02-10 18:02:21';
  10 + let trx_ref_id = '56789';
  11 + let cust_num = '08123456789';
  12 + let username = 'toko-A';
  13 + let password = '1234';
  14 +
  15 + partner.calculateSign(datetime, trx_ref_id, cust_num, username, password).should.equal('e7851fc59cee66a7b99b5624f7f1573f149ee3f6');
  16 + })
  17 + })
  18 +})