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) {
74 reportUsingHttpPost(data); 73 function report(data) {
74 reportUsingHttpPost(data);
75 }
76
77 function reportUsingHttpPost(data) {
78 let options = {
79 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
80 form: {
81 trx_id: data.trx_id,
82 rc: data.rc,
83 message: data.message,
84 handler: config.handler_name,
85 sn: data.sn,
86 amount: data.amount
87 }
88 }
89
90 request.post(options, function(error, response, body) {
91 if (error) {
92 logger.warn('Error reporting to CORE', {error: error});
93 }
94 else if (response.statusCode != 200) {
95 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
96 }
97 else {
98 logger.verbose('Report has been sent to CORE', {requestOptions: options});
99 }
100 });
101 }
102
103 function reportUsingHttpGet(data) {
75 } 104 let options = {
76 105 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
77 function reportUsingHttpPost(data) { 106 qs: {
78 let options = { 107 trx_id: data.trx_id,
79 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey), 108 rc: data.rc,
80 form: { 109 message: data.message,
81 trx_id: data.trx_id, 110 handler: config.handler_name,
82 rc: data.rc, 111 sn: data.sn,
83 message: data.message, 112 amount: data.amount
84 handler: config.handler_name, 113 }
85 sn: data.sn, 114 }
86 amount: data.amount 115
87 } 116
88 } 117 request(options, function(error, response, body) {
89 118 if (error) {
90 request.post(options, function(error, response, body) { 119 logger.warn('Error reporting to CORE', {error: error});
91 if (error) { 120 }
92 logger.warn('Error reporting to CORE', {error: error}); 121 else if (response.statusCode != 200) {
93 } 122 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
94 else if (response.statusCode != 200) { 123 }
95 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 124 else {
96 } 125 logger.verbose('Report has been sent to CORE', {requestOptions: options});
97 else { 126 }
98 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 127 });
99 } 128 }
100 }); 129
101 } 130 function resendReport(data) {
102 131 let sleepBeforeResend = 1000;
103 function reportUsingHttpGet(data) { 132 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
104 let options = { 133
105 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey), 134 setTimeout(
106 qs: { 135 function() {
107 trx_id: data.trx_id, 136 report(data);
108 rc: data.rc, 137 },
109 message: data.message, 138 sleepBeforeResend
110 handler: config.handler_name, 139 )
111 sn: data.sn, 140 }
112 amount: data.amount 141
113 } 142 function isPaused() {
114 } 143 return matrix.paused;
115 144 }
116 145
117 request(options, function(error, response, body) { 146 function pause() {
118 if (error) { 147 matrix.paused = true;
119 logger.warn('Error reporting to CORE', {error: error}); 148 }
120 } 149
121 else if (response.statusCode != 200) { 150 function resume() {
122 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 151 matrix.pause = false;
123 } 152 }
124 else { 153
125 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 154 function initMatrix() {
126 } 155 if (!matrix) {
127 }); 156 matrix = {};
128 } 157 }
129 158
130 function resendReport(data) { 159 matrix.counter = {
131 let sleepBeforeResend = 1000; 160 trx: 0
132 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 161 }
133 162 }
134 setTimeout( 163
135 function() { 164 function incrementCounterTrx() {
136 report(data); 165 matrix.counter.trx++;
137 }, 166 }
138 sleepBeforeResend 167
139 ) 168 function getRemoteProduct(product) {
140 } 169 let remoteProduct = config.remote_products[product];
141 170 return remoteProduct || product;
142 function isPaused() { 171 }
143 return matrix.paused; 172
144 } 173 initMatrix();
145 174 setInterval(pullTask, config.pull_interval_ms || 1000);
146 function pause() { 175
147 matrix.paused = true; 176 exports.setPartner = setPartner;
148 } 177 exports.isPaused = isPaused;
149 178 exports.pause = pause;
150 function resume() { 179 exports.resume = resume;
151 matrix.pause = false; 180 exports.report = report;
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.6.2", 3 "version": "1.6.3",
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