This repository was archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·120 lines (98 loc) · 3.29 KB
/
Copy pathindex.js
File metadata and controls
executable file
·120 lines (98 loc) · 3.29 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* jslint node: true, sub: true */
'use strict';
var HTMLParser = require('node-html-parser');
var request = require('request');
// Init the module
module.exports = (function () {
var defTimeout = 10000,
url = 'https://te.ge/webpay/';
var getFields = function (body) {
var ret = {
"name":'',
"address":'',
"balance":'',
"payable_amount":'',
"last_payment_date":''
};
const root = HTMLParser.parse(body);
var h3 = root.querySelectorAll('h3');
for (var i = 0; i < h3.length; ++i) {
if(h3[i].textContent == "აბონენტი:") {
ret.name = h3[++i].textContent;
} else if(h3[i].textContent == "მისამართი:") {
ret.address = h3[++i].textContent;
} else if(h3[i].textContent == "დავალიანება:") {
ret.balance = h3[++i].textContent;
} else if(h3[i].textContent == "ბოლო ვადა:") {
ret.last_payment_date = h3[++i].textContent;
} else if(h3[i].textContent == "გადასახდელი თანხა:") {
ret.payable_amount = ""; //h3[++i].textContent;
}
}
var payable_amount = root.querySelector('#amount');
if (payable_amount !== undefined) {
ret.payable_amount = payable_amount.getAttribute('value');
}
return ret;
};
var normallizeID = function (id) {
if (id.length === 10) {
if (id.indexOf("-") !== -1) {
return id;
}
} else if (id.length === 9) {
var firstDigits = id.substr(0, 6);
var secondDigits = id.substr(6, 3);
return firstDigits + '-' + secondDigits;
}
return undefined;
};
var get = function get(options, callback) {
if (typeof callback !== 'function')
callback = function callback(err, result) {
return err || result;
};
if (!options || typeof options !== 'object')
return callback('Invalid options');
if (!options.id)
return callback('Missing ID input');
var accountID = normallizeID(options.id);
if (accountID === undefined) {
return callback('Invalid account ID');
}
var timeout = options.timeout || defTimeout;
request.post({url: url, timeout: timeout, form: {abonentID: accountID}}, function (err, res, body) {
if (err) return callback(err);
if (res.statusCode !== 200) return callback(new Error('request failed (' + res.statusCode + ')'));
if (!body) return callback(new Error('failed to get body content'));
// Check body content
if (body.indexOf('<') !== 0) {
if (body.search(/not found/i) !== -1) {
return callback(null, result);
}
return callback(new Error('Invalid body content'));
}
var ret = getFields(body);
if (ret.name === undefined || ret.address === undefined) {
return callback(null, {
"balance": {}
});
}
// Parse body
var result = {
"balance": {
"id": accountID,
"name": ret.name,
"address": ret.address,
"balance": ret.balance,
"payable_amount": ret.payable_amount,
"currency": 'GEL'
}
};
return callback(null, result);
});
};
return {
get: get
};
})();