Commit 13829a751ee1974a0a45a6098dfddfa8da066e8e

Authored by Adhidarma Hadiwinoto
1 parent 86e71ad257
Exists in master

topupAdvice

Showing 2 changed files with 99 additions and 10 deletions Side-by-side Diff

... ... @@ -41,16 +41,12 @@ function callbackReport(requestId, rc, message, options) {
41 41 aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
42 42 }
43 43  
44   -function topupRequest(task) {
45   - aaa.insertTaskToMongoDb(task);
46   - _hitTopup(task);
47   -}
48   -
49 44 function splitRemoteProduct(remoteProduct) {
50 45 return remoteProduct.replace(/^\s+|\s+$/gm,'').split(/[\/\W]+/);
51 46 }
52 47  
53   -function _hitTopup(task, isCheckStatus) {
  48 +function topupRequest(task) {
  49 + aaa.insertTaskToMongoDb(task);
54 50  
55 51 const remoteProduct = splitRemoteProduct(task.remoteProduct);
56 52  
... ... @@ -80,7 +76,7 @@ function _hitTopup(task, isCheckStatus) {
80 76 if (error) {
81 77 let rc = '68';
82 78  
83   - if (!isCheckStatus && (error.syscall == 'connect')) {
  79 + if (!error.syscall == 'connect') {
84 80 rc = '91';
85 81 }
86 82  
... ... @@ -100,8 +96,8 @@ function _hitTopup(task, isCheckStatus) {
100 96 if (!body) {
101 97 let rc = '68';
102 98  
103   - logger.warn('Error processing response body', {task: task, responseBody: body});
104   - callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error processing response body', {task: task});
  99 + logger.warn('Missing response body', {task: task, responseBody: body});
  100 + callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing response body', {task: task});
105 101 return;
106 102 }
107 103  
... ... @@ -128,6 +124,67 @@ function _hitTopup(task, isCheckStatus) {
128 124  
129 125 }
130 126  
  127 +function topupAdvice(task) {
  128 +
  129 + let pathParams = {
  130 + noid: config.h2h_out.noid,
  131 + token: config.h2h_out.token,
  132 + trace_id: task.requestId
  133 + }
  134 +
  135 + const requestOptions = {
  136 + url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPathAdvice(pathParams).replace(/^\/+/, '');
  137 + }
  138 +
  139 + logger.verbose('Requeting advice to partner', {requestOptions: requestOptions});
  140 + request(requestOptions, function(error, response, body) {
  141 + if (error) {
  142 + let rc = '68';
  143 +
  144 + logger.warn('Error requesting to advice partner', {task: task, rc: rc, error: error});
  145 + callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting advice to partner. ' + error, {task: task});
  146 + return;
  147 + }
  148 +
  149 + if (response.statusCode != 200) {
  150 + let rc = '68';
  151 +
  152 + logger.warn('Advice HTTP status code is not 200', {task: task, http_status_code: response.statusCode});
  153 + callbackReport(task.requestId, rc, 'INTERNAL_MSG: Advice HTTP status code ' + response.statusCode, {task: task});
  154 + return;
  155 + }
  156 +
  157 + if (!body) {
  158 + let rc = '68';
  159 +
  160 + logger.warn('Missing advice response body', {task: task, responseBody: body});
  161 + callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing advice response body', {task: task});
  162 + return;
  163 + }
  164 +
  165 + if (body.trim() == 'invalid specs') {
  166 + let rc = '68';
  167 +
  168 + logger.warn('Invalid specs', {task: task, responseBody: body});
  169 + callbackReport(task.requestId, rc, body);
  170 + return;
  171 + }
  172 +
  173 + logger.verbose('Got advice response from partner', {task: task, responseBody: body});
  174 +
  175 + const responseData = parseResponseBody(body);
  176 + logger.verbose('Advice response body parsed as json value', {responseData: responseData});
  177 + const data = responseDataProcessor(responseData);
  178 +
  179 + if (data.balance && aaa.updateBalance) {
  180 + aaa.updateBalance(data.balance);
  181 + }
  182 +
  183 + callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task});
  184 + })
  185 +
  186 +}
  187 +
131 188 function createUrlPath(options) {
132 189 let urlPath = [
133 190 "get",
... ... @@ -146,6 +203,21 @@ function createUrlPath(options) {
146 203 return '/' + urlPath;
147 204 }
148 205  
  206 +function createUrlPathAdvice(options) {
  207 + // pattern: /get/advice/json/[noid]/[token]/[traceid]
  208 +
  209 + let urlPath = [
  210 + "get",
  211 + "advice",
  212 + "json",
  213 + options.noid,
  214 + options.token,
  215 + options.trace_id
  216 + ].join('/');
  217 +
  218 + return '/' + urlPath;
  219 +}
  220 +
149 221 function parseResponseBody(body) {
150 222 let data;
151 223  
... ... @@ -243,8 +315,10 @@ function responseDataProcessor(responseData) {
243 315 }
244 316  
245 317 exports.start = start;
  318 +exports.topupRequest = topupRequest;
  319 +exports.topupAdvice = topupAdvice;
246 320 exports.createUrlPath = createUrlPath;
  321 +exports.createUrlPathAdvice = createUrlPathAdvice;
247 322 exports.parseResponseBody = parseResponseBody;
248 323 exports.responseDataProcessor = responseDataProcessor;
249   -exports.topupRequest = topupRequest;
250 324 exports.splitRemoteProduct = splitRemoteProduct;
... ... @@ -4,6 +4,7 @@ const should = require("should");
4 4 const partner = require("./partner-bnisp");
5 5  
6 6 describe("#partner", function() {
  7 +
7 8 describe("#createUrlPath", function() {
8 9  
9 10 it('should return correct url path based on example', function() {
... ... @@ -21,6 +22,20 @@ describe("#partner", function() {
21 22  
22 23 })
23 24  
  25 + describe("#createUrlPathAdvice", function() {
  26 +
  27 + it('should return correct url path based on example', function() {
  28 + let options = {
  29 + noid: 1002003,
  30 + token: 'CIPY6t6ruy5UuGG0PCqu',
  31 + trace_id: 16
  32 + }
  33 +
  34 + partner.createUrlPathAdvice(options).should.equal('/get/advice/json/1002003/CIPY6t6ruy5UuGG0PCqu/16')
  35 + })
  36 +
  37 + })
  38 +
24 39 const responseBody = '{"trxId":"064024","response_code":"0000","response_message":"SUKSES","detail":{"jmlTagihan":"1","kodeThree":"012001","nilaiPulsa":"000000005000","noHp":"0895385381299","transactionId":"0612143023138365102 ","voucherSerialNumber":"20170612143620980249"},"reff":"20170612143620980249","loadTime":"0.0107","tipe_request":"purchase","prv":"hpay","userID":"1002003","idpel":"0895385381299","product":"PULSA","produk":"PULSA","product_detail":"THREE5000","produk_tipe":"THREE5000","traceId":16,"tagihan":5050,"total_tagihan":5050,"amount":5050,"saldo":94950,"waktu":"2017-07-17 18:43:32"}';
25 40 const responseData = partner.parseResponseBody(responseBody);
26 41