Compare View

switch
from
...
to
 
Commits (3)

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 }
14
15 function pullTask() {
16 if (!partner) {
17 return; 13 }
18 } 14
19 15 function pullTask() {
16 if (!partner) {
17 return;
18 }
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(trx_id, rc, message, sn) { 73 //function report(trx_id, rc, message, sn) {
74 function report(data) { 74 function report(data) {
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: data.trx_id, 78 trx_id: data.trx_id,
79 rc: data.rc, 79 rc: data.rc,
80 message: data.message, 80 message: data.message,
81 handler: config.handler_name, 81 handler: config.handler_name,
82 sn: data.sn, 82 sn: data.sn,
83 amount: data.amount 83 amount: data.amount
84 } 84 }
85 } 85 }
86 86
87 87
88 request(options, function(error, response, body) { 88 request(options, function(error, response, body) {
89 if (error) { 89 if (error) {
90 logger.warn('Error reporting to CORE', {error: error}); 90 logger.warn('Error reporting to CORE', {error: error});
91 } 91 }
92 else if (response.statusCode != 200) { 92 else if (response.statusCode != 200) {
93 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 93 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
94 } 94 }
95 else { 95 else {
96 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 96 logger.verbose('Report has been sent to CORE', {requestOptions: options});
97 } 97 }
98 }); 98 });
99 } 99 }
100 100
101 function resendReport(data) { 101 function resendReport(data) {
102 let sleepBeforeResend = 1000; 102 let sleepBeforeResend = 1000;
103 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 103 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
104 104
105 setTimeout( 105 setTimeout(
106 function() { 106 function() {
107 report(data); 107 report(data);
108 }, 108 },
109 sleepBeforeResend 109 sleepBeforeResend
110 ) 110 )
111 } 111 }
112 112
113 function isPaused() { 113 function isPaused() {
114 return matrix.paused; 114 return matrix.paused;
115 } 115 }
116 116
117 function pause() { 117 function pause() {
118 matrix.paused = true; 118 matrix.paused = true;
119 } 119 }
120 120
121 function resume() { 121 function resume() {
122 matrix.pause = false; 122 matrix.pause = false;
123 } 123 }
124 124
125 function initMatrix() { 125 function initMatrix() {
126 if (!matrix) { 126 if (!matrix) {
127 matrix = {}; 127 matrix = {};
128 } 128 }
129 129
130 matrix.counter = { 130 matrix.counter = {
131 trx: 0 131 trx: 0
132 } 132 }
133 } 133 }
134 134
135 function incrementCounterTrx() { 135 function incrementCounterTrx() {
136 matrix.counter.trx++; 136 matrix.counter.trx++;
137 } 137 }
138 138
139 function getRemoteProduct(product) { 139 function getRemoteProduct(product) {
140 let remoteProduct = config.remote_products[product]; 140 let remoteProduct = config.remote_products[product];
141 return remoteProduct || product; 141 return remoteProduct || product;
142 } 142 }
143 143
144 initMatrix(); 144 initMatrix();
145 setInterval(pullTask, config.pull_interval_ms || 1000);
146
147 exports.setPartner = setPartner;
145 setInterval(pullTask, config.pull_interval_ms || 1000); 148 exports.isPaused = isPaused;
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.6.1", 3 "version": "1.6.2",
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