Commit ba1957a813368229a7d4894b39387af5a7ae3598

Authored by Adhidarma Hadiwinoto
1 parent 0bb34ba529
Exists in master

resync npm packages

Showing 2 changed files with 4 additions and 3 deletions Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 const request = require('request'); 3 const request = require('request');
4 const logger = require('./logger').get(); 4 const logger = require('./logger').get();
5 5
6 var config; 6 var config;
7 var matrix; 7 var matrix;
8 var partner; 8 var partner;
9 9
10 function init(options) { 10 function init(options) {
11 config = options.config; 11 config = options.config;
12 matrix = options.matrix; 12 matrix = options.matrix;
13 partner = options.partner; 13 partner = options.partner;
14 14
15 initMatrix(); 15 initMatrix();
16 16
17 setInterval(pullTask, config.pull_interval_ms || 1000); 17 setInterval(pullTask, config.pull_interval_ms || 1000);
18 } 18 }
19 19
20 function pullTask() { 20 function pullTask() {
21 let options = { 21 let options = {
22 url: config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey), 22 url: config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey),
23 qs: { 23 qs: {
24 handler: config.handler_name, 24 handler: config.handler_name,
25 products: config.products.join(',') 25 products: config.products.join(',')
26 } 26 }
27 } 27 }
28 28
29 request(options, function(error, response, body) { 29 request(options, function(error, response, body) {
30 if (error) { 30 if (error) {
31 if (matrix.core_is_healthy) { 31 if (matrix.core_is_healthy) {
32 logger.warn('Error pulling task from CORE', {error: error}); 32 logger.warn('Error pulling task from CORE', {error: error});
33 } 33 }
34 matrix.core_is_healthy = false; 34 matrix.core_is_healthy = false;
35 return; 35 return;
36 } 36 }
37 37
38 if (response.statusCode != 200) { 38 if (response.statusCode != 200) {
39 if (matrix.core_is_healthy) { 39 if (matrix.core_is_healthy) {
40 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 40 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
41 } 41 }
42 matrix.core_is_healthy = false; 42 matrix.core_is_healthy = false;
43 return; 43 return;
44 } 44 }
45 45
46 if (!matrix.core_is_healthy) { 46 if (!matrix.core_is_healthy) {
47 logger.verbose('CORE is healthy'); 47 logger.verbose('CORE is healthy');
48 } 48 }
49 matrix.core_is_healthy = true; 49 matrix.core_is_healthy = true;
50 50
51 if (body == 'NONE') { 51 if (body == 'NONE') {
52 return; 52 return;
53 } 53 }
54 54
55 forwardCoreTaskToPartner(body); 55 forwardCoreTaskToPartner(body);
56 }); 56 });
57 } 57 }
58 58
59 function forwardCoreTaskToPartner(coreMessage) { 59 function forwardCoreTaskToPartner(coreMessage) {
60 let task; 60 let task;
61 61
62 try { 62 try {
63 task = JSON.parse(coreMessage); 63 task = JSON.parse(coreMessage);
64 } 64 }
65 catch(e) { 65 catch(e) {
66 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 66 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
67 } 67 }
68 68
69 task.remote_product = getRemoteProduct(task.product); 69 task.remote_product = getRemoteProduct(task.product);
70 70
71 partner.buy(task); 71 partner.buy(task);
72 } 72 }
73 73
74 function report(trx_id, rc, message, sn) { 74 function report(trx_id, rc, message, sn) {
75 let options = { 75 let options = {
76 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey), 76 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
77 qs: { 77 qs: {
78 trx_id: trx_id, 78 trx_id: trx_id,
79 rc: rc, 79 rc: rc,
80 amount: config.product_price ? config.product_price : null,
80 message: message, 81 message: message,
81 handler: config.handler_name 82 handler: config.handler_name
82 } 83 }
83 } 84 }
84 85
85 if (sn) { 86 if (sn) {
86 options.qs.sn = sn; 87 options.qs.sn = sn;
87 } 88 }
88 89
89 request(options, function(error, response, body) { 90 request(options, function(error, response, body) {
90 if (error) { 91 if (error) {
91 logger.warn('Error reporting to CORE', {error: error}); 92 logger.warn('Error reporting to CORE', {error: error});
92 } 93 }
93 else if (response.statusCode != 200) { 94 else if (response.statusCode != 200) {
94 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 95 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
95 } 96 }
96 else { 97 else {
97 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 98 logger.verbose('Report has been sent to CORE', {requestOptions: options});
98 } 99 }
99 }); 100 });
100 } 101 }
101 102
102 function resendReport(trx_id, rc, message, sn) { 103 function resendReport(trx_id, rc, message, sn) {
103 let sleepBeforeResend = 1000; 104 let sleepBeforeResend = 1000;
104 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 105 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
105 106
106 setTimeout( 107 setTimeout(
107 function() { 108 function() {
108 report(trx_id, rc, message, sn); 109 report(trx_id, rc, message, sn);
109 }, 110 },
110 sleepBeforeResend 111 sleepBeforeResend
111 ) 112 )
112 } 113 }
113 114
114 function isPaused() { 115 function isPaused() {
115 return matrix.paused; 116 return matrix.paused;
116 } 117 }
117 118
118 function pause() { 119 function pause() {
119 matrix.paused = true; 120 matrix.paused = true;
120 } 121 }
121 122
122 function resume() { 123 function resume() {
123 matrix.pause = false; 124 matrix.pause = false;
124 } 125 }
125 126
126 function initMatrix() { 127 function initMatrix() {
127 if (!matrix) { 128 if (!matrix) {
128 matrix = {}; 129 matrix = {};
129 } 130 }
130 131
131 matrix.counter = { 132 matrix.counter = {
132 trx: 0 133 trx: 0
133 } 134 }
134 } 135 }
135 136
136 function incrementCounterTrx() { 137 function incrementCounterTrx() {
137 matrix.counter.trx++; 138 matrix.counter.trx++;
138 } 139 }
139 140
140 function getRemoteProduct(product) { 141 function getRemoteProduct(product) {
141 let remoteProduct = config.remote_products[product]; 142 let remoteProduct = config.remote_products[product];
142 return remoteProduct || product; 143 return remoteProduct || product;
143 } 144 }
144 145
145 exports.init = init; 146 exports.init = init;
146 exports.isPaused = isPaused; 147 exports.isPaused = isPaused;
147 exports.pause = pause; 148 exports.pause = pause;
148 exports.resume = resume; 149 exports.resume = resume;
149 exports.report = report; 150 exports.report = report;
150 151
1 { 1 {
2 "name": "komodo-gw-oddeven", 2 "name": "komodo-gw-oddeven",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 "description": "Contoh dummy gateway / handler komodo", 4 "description": "Contoh dummy gateway / handler komodo",
5 "main": "index.js", 5 "main": "index.js",
6 "dependencies": { 6 "dependencies": {
7 "moment": "^2.17.1", 7 "moment": "^2.22.2",
8 "request": "^2.79.0", 8 "request": "^2.87.0",
9 "strftime": "^0.9.2", 9 "strftime": "^0.9.2",
10 "winston": "^2.3.0", 10 "winston": "^2.4.3",
11 "winston-daily-rotate-file": "^1.4.0" 11 "winston-daily-rotate-file": "^1.4.0"
12 }, 12 },
13 "devDependencies": {}, 13 "devDependencies": {},
14 "scripts": { 14 "scripts": {
15 "test": "mocha" 15 "test": "mocha"
16 }, 16 },
17 "repository": { 17 "repository": {
18 "type": "git", 18 "type": "git",
19 "url": "git@gitlab.kodesumber.com:komodo/komodo-gw-oddeven.git" 19 "url": "git@gitlab.kodesumber.com:komodo/komodo-gw-oddeven.git"
20 }, 20 },
21 "keywords": [ 21 "keywords": [
22 "komodo", 22 "komodo",
23 "ppob" 23 "ppob"
24 ], 24 ],
25 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", 25 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>",
26 "license": "ISC" 26 "license": "ISC"
27 } 27 }
28 28