buy.js
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// const moment = require('moment');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const commands = require('../command-group');
const commandError = require('./error');
const destinationCorrector = require('../destination-corrector');
const coreapi = require('../coreapi');
const coreEndpointAuto = '/buy-or-pay';
const coreEndpointBuy = '/prepaid/buy';
/*
function generateRequestId(product, destination) {
return `AUTO_${ product.toUpperCase() }_${ destination }_${ moment().format('YYYYMMDD') }`;
}
*/
function help() {
return 'Untuk pembelian, ketik perintah dengan format: <KODEPRODUK>.<NOMORTUJUAN>.<PIN>';
}
function execute(tokens, params, cb) {
if (!tokens || tokens.length < 3) {
const responseParams = {
body: `${commandError.ERR_INVALID_FORMAT}. ${help()}`,
};
cb(null, null, responseParams);
return;
}
if (commands[tokens[0]] !== 'buy') {
tokens.unshift('buy');
logger.verbose('Rearrange tokens', { tokens });
}
const destination = destinationCorrector((tokens[2] || '').trim());
const coreParams = {
origin: params.origin,
report_ip: params.report_ip,
report_port: params.report_port,
terminal_name: params.from,
product_name: (tokens[1] || '').trim().toUpperCase(),
destination,
password: tokens[3],
request_id_suffix: tokens[4] || '',
postpaid: 0,
};
const coreEndpoint = config.buy_only_prepaid ? coreEndpointBuy : coreEndpointAuto;
coreapi(coreEndpoint, coreParams, 'GET', cb);
}
module.exports = execute;