Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 2 changed files Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 const request = require('request'); 3 const request = require('request');
4 4
5 const config = require('../config'); 5 const config = require('../config');
6 const logger = require('../logger'); 6 const logger = require('../logger');
7 const matrix = require('../matrix'); 7 const matrix = require('../matrix');
8 8
9 var partner; 9 var partner;
10 10
11 function setPartner(_partner) { 11 function setPartner(_partner) {
12 partner = _partner; 12 partner = _partner;
13 } 13 }
14 14
15 function pullTask() { 15 function pullTask() {
16 if (!partner) { 16 if (!partner) {
17 return; 17 return;
18 } 18 }
19 19
20 let options = { 20 let options = {
21 url: config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey), 21 url: config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey),
22 qs: { 22 qs: {
23 handler: config.handler_name, 23 handler: config.handler_name,
24 products: config.products.join(',') 24 products: config.products.join(',')
25 } 25 }
26 } 26 }
27 27
28 request(options, function(error, response, body) { 28 request(options, function(error, response, body) {
29 if (error) { 29 if (error) {
30 if (matrix.core_is_healthy) { 30 if (matrix.core_is_healthy) {
31 logger.warn('Error pulling task from CORE', {error: error}); 31 logger.warn('Error pulling task from CORE', {error: error});
32 } 32 }
33 matrix.core_is_healthy = false; 33 matrix.core_is_healthy = false;
34 return; 34 return;
35 } 35 }
36 36
37 if (response.statusCode != 200) { 37 if (response.statusCode != 200) {
38 if (matrix.core_is_healthy) { 38 if (matrix.core_is_healthy) {
39 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 39 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
40 } 40 }
41 matrix.core_is_healthy = false; 41 matrix.core_is_healthy = false;
42 return; 42 return;
43 } 43 }
44 44
45 if (!matrix.core_is_healthy) { 45 if (!matrix.core_is_healthy) {
46 logger.verbose('CORE is healthy'); 46 logger.verbose('CORE is healthy');
47 } 47 }
48 matrix.core_is_healthy = true; 48 matrix.core_is_healthy = true;
49 49
50 if (body == 'NONE') { 50 if (body == 'NONE') {
51 return; 51 return;
52 } 52 }
53 53
54 forwardCoreTaskToPartner(body); 54 forwardCoreTaskToPartner(body);
55 }); 55 });
56 } 56 }
57 57
58 function forwardCoreTaskToPartner(coreMessage) { 58 function forwardCoreTaskToPartner(coreMessage) {
59 let task; 59 let task;
60 60
61 try { 61 try {
62 task = JSON.parse(coreMessage); 62 task = JSON.parse(coreMessage);
63 } 63 }
64 catch(e) { 64 catch(e) {
65 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 65 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
66 } 66 }
67 67
68 task.remote_product = getRemoteProduct(task.product); 68 task.remote_product = getRemoteProduct(task.product);
69 69
70 partner.buy(task); 70 partner.buy(task);
71 } 71 }
72 72
73 function report(data) { 73 function report(data) {
74 reportUsingHttpPost(data); 74 reportUsingHttpPost(data);
75 } 75 }
76 76
77 function reportUsingHttpPost(data) { 77 function reportUsingHttpPost(data) {
78 let options = { 78 let options = {
79 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey), 79 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
80 form: { 80 form: {
81 trx_id: data.trx_id, 81 trx_id: data.trx_id,
82 rc: data.rc, 82 rc: data.rc,
83 message: data.message, 83 message: data.message,
84 handler: config.handler_name, 84 handler: config.handler_name,
85 sn: data.sn, 85 sn: data.sn,
86 amount: data.amount, 86 amount: data.amount,
87 raw: data.raw 87 raw: data.raw
88 } 88 }
89 } 89 }
90 90
91 logger.verbose('Report to CORE using HTTP POST');
91 logger.verbose('Report to CORE using HTTP POST'); 92 request.post(options, function(error, response, body) {
92 request.post(options, function(error, response, body) { 93 if (error) {
93 if (error) { 94 logger.warn('Error reporting to CORE', {error: error});
94 logger.warn('Error reporting to CORE', {error: error}); 95 }
95 } 96 else if (response.statusCode != 200) {
96 else if (response.statusCode != 200) { 97 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
97 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 98 }
98 } 99 else {
99 else { 100 logger.verbose('Report has been sent to CORE', {requestOptions: options});
100 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 101 }
101 } 102 });
102 }); 103 }
103 } 104
104 105 function reportUsingHttpGet(data) {
105 function reportUsingHttpGet(data) { 106 let options = {
106 let options = { 107 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
107 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey), 108 qs: {
108 qs: { 109 trx_id: data.trx_id,
109 trx_id: data.trx_id, 110 rc: data.rc,
110 rc: data.rc, 111 message: data.message,
111 message: data.message, 112 handler: config.handler_name,
112 handler: config.handler_name, 113 sn: data.sn,
113 sn: data.sn, 114 amount: data.amount
114 amount: data.amount 115 }
115 } 116 }
116 } 117
117 118 logger.verbose('Report to CORE using HTTP GET');
118 logger.verbose('Report to CORE using HTTP GET'); 119 request(options, function(error, response, body) {
119 request(options, function(error, response, body) { 120 if (error) {
120 if (error) { 121 logger.warn('Error reporting to CORE', {error: error});
121 logger.warn('Error reporting to CORE', {error: error}); 122 }
122 } 123 else if (response.statusCode != 200) {
123 else if (response.statusCode != 200) { 124 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
124 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 125 }
125 } 126 else {
126 else { 127 logger.verbose('Report has been sent to CORE', {requestOptions: options});
127 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 128 }
128 } 129 });
129 }); 130 }
130 } 131
131 132 function resendReport(data) {
132 function resendReport(data) { 133 let sleepBeforeResend = 1000;
133 let sleepBeforeResend = 1000; 134 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
134 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 135
135 136 setTimeout(
136 setTimeout( 137 function() {
137 function() { 138 report(data);
138 report(data); 139 },
139 }, 140 sleepBeforeResend
140 sleepBeforeResend 141 )
141 ) 142 }
142 } 143
143 144 function isPaused() {
144 function isPaused() { 145 return matrix.paused;
145 return matrix.paused; 146 }
146 } 147
147 148 function pause() {
148 function pause() { 149 matrix.paused = true;
149 matrix.paused = true; 150 }
150 } 151
151 152 function resume() {
152 function resume() { 153 matrix.pause = false;
153 matrix.pause = false; 154 }
154 } 155
155 156 function initMatrix() {
156 function initMatrix() { 157 if (!matrix) {
157 if (!matrix) { 158 matrix = {};
158 matrix = {}; 159 }
159 } 160
160 161 matrix.counter = {
161 matrix.counter = { 162 trx: 0
162 trx: 0 163 }
163 } 164 }
164 } 165
165 166 function incrementCounterTrx() {
166 function incrementCounterTrx() { 167 matrix.counter.trx++;
167 matrix.counter.trx++; 168 }
168 } 169
169 170 function getRemoteProduct(product) {
170 function getRemoteProduct(product) { 171 let remoteProduct = config.remote_products[product];
171 let remoteProduct = config.remote_products[product]; 172 return remoteProduct || product;
172 return remoteProduct || product; 173 }
173 } 174
174 175 initMatrix();
175 initMatrix(); 176 setInterval(pullTask, config.pull_interval_ms || 1000);
176 setInterval(pullTask, config.pull_interval_ms || 1000); 177
177 178 exports.setPartner = setPartner;
178 exports.setPartner = setPartner; 179 exports.isPaused = isPaused;
179 exports.isPaused = isPaused; 180 exports.pause = pause;
180 exports.pause = pause; 181 exports.resume = resume;
181 exports.resume = resume; 182 exports.report = report;
182 exports.report = report; 183
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.6.4", 3 "version": "1.6.5",
4 "description": "SDK for Komodo", 4 "description": "SDK for Komodo",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha", 7 "test": "mocha",
8 "postversion": "git push && git push --tags" 8 "postversion": "git push && git push --tags"
9 }, 9 },
10 "repository": { 10 "repository": {
11 "type": "git", 11 "type": "git",
12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" 12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git"
13 }, 13 },
14 "keywords": [ 14 "keywords": [
15 "ppob", 15 "ppob",
16 "payment", 16 "payment",
17 "komodo" 17 "komodo"
18 ], 18 ],
19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", 19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
20 "license": "ISC", 20 "license": "ISC",
21 "dependencies": { 21 "dependencies": {
22 "lru-cache": "^4.1.1", 22 "lru-cache": "^4.1.1",
23 "request": "^2.81.0", 23 "request": "^2.81.0",
24 "strftime": "^0.10.0", 24 "strftime": "^0.10.0",
25 "winston": "^2.3.1", 25 "winston": "^2.3.1",
26 "winston-daily-rotate-file": "^1.4.6" 26 "winston-daily-rotate-file": "^1.4.6"
27 } 27 }
28 } 28 }
29 29