· 6 years ago · Apr 18, 2019, 04:34 AM
1module.exports = {
2 createSMTelSignature: function(partnerTrxId, trxTime, smtelData) {
3 if(!smtelData.posId) {
4 smtelData.posId = '';
5 }
6
7 const firstSignature = smtelData.pin + smtelData.secretKey + partnerTrxId;
8 const firstMd5 = LIBRARY.crypto
9 .createHash("md5")
10 .update(firstSignature)
11 .digest("hex");
12
13 const lockMethod = smtelData.channelId + smtelData.storeId + smtelData.posId;
14 const secondSignature = lockMethod + trxTime;
15 const secondMd5 = LIBRARY.crypto
16 .createHash("md5")
17 .update(secondSignature)
18 .digest("hex");
19
20 const finalSignature = firstMd5 + secondMd5;
21 const password = LIBRARY.crypto
22 .createHash("md5")
23 .update(finalSignature)
24 .digest("hex");
25 return password;
26 },
27
28 createStringDateFormat: function(dt) {
29 let currentTime;
30 if (dt) {
31 currentTime = new Date(dt);
32 } else {
33 currentTime = new Date();
34 }
35
36 let year = currentTime.getFullYear().toString();
37 let month =
38 currentTime.getMonth() + 1 < 10
39 ? "0" + (currentTime.getMonth() + 1)
40 : currentTime.getMonth() + 1;
41 month = month.toString();
42 let date =
43 currentTime.getDate() < 10
44 ? "0" + currentTime.getDate()
45 : currentTime.getDate();
46 date = date.toString();
47 let hour =
48 currentTime.getHours() < 10
49 ? "0" + currentTime.getHours()
50 : currentTime.getHours();
51 hour = hour.toString();
52 let minute =
53 currentTime.getMinutes() < 10
54 ? "0" + currentTime.getMinutes()
55 : currentTime.getMinutes();
56 minute = minute.toString();
57 let second =
58 currentTime.getSeconds() < 10
59 ? "0" + currentTime.getSeconds()
60 : currentTime.getSeconds();
61 second = second.toString();
62
63 let trxTime = year + month + date + hour + minute + second; // for smtel format
64
65 let dateObject = {
66 trxTime
67 };
68 return dateObject;
69 },
70
71 connectXmpp: function(){
72 LIBRARY.xmpp.connect({
73 jid: GLOBAL_CONFIG.xmpp.jid,
74 password: GLOBAL_CONFIG.xmpp.password,
75 host: GLOBAL_CONFIG.xmpp.host,
76 port: GLOBAL_CONFIG.xmpp.port
77 });
78
79 LIBRARY.xmpp.on('online', function(data) {
80 console.log('Terkoneksi dengan Jabber ID : ' + data.jid);
81 });
82 },
83
84 connectMysql__: function(){
85 var con = LIBRARY.mysql.createConnection({
86 host: GLOBAL_CONFIG.mysql.host,
87 port: GLOBAL_CONFIG.mysql.port,
88 database: GLOBAL_CONFIG.mysql.database,
89 user: GLOBAL_CONFIG.mysql.user,
90 password: GLOBAL_CONFIG.mysql.password
91 });
92
93 con.connect(function(err) {
94 if (err) throw err;
95 console.log("success connect to databse");
96 });
97 return con;
98 },
99
100 getChat: function (trxTime) {
101 LIBRARY.xmpp.on('chat', function(from, message) {
102 var arr = message.trim().split(".").map(val => String(val));
103 var productCode = arr[0];
104 var msidn = arr[1];
105 var pin = arr[2];
106 var agenId = arr[3];
107 if(productCode === undefined || msidn === undefined || pin === undefined || agenId === undefined){
108 LIBRARY.xmpp.send(from,'WRONG FORMAT')
109 return 'WRONG FORMAT';
110 }
111 agenId = agenId.replace(/(\n|\r)+$/, '');
112 var con = LIBRARY.mysql.createConnection({
113 host: GLOBAL_CONFIG.mysql.host,
114 port: GLOBAL_CONFIG.mysql.port,
115 database: GLOBAL_CONFIG.mysql.database,
116 user: GLOBAL_CONFIG.mysql.user,
117 password: GLOBAL_CONFIG.mysql.password
118 });
119
120 con.connect(function(err) {
121 if (err) throw err;
122 console.log("success connect to databse");
123 });
124
125 let sql = "SELECT pin,chan_suffix,channel_id,store_id,pos_id FROM channel_agen left join stockiest on stockiest.agenid = channel_agen.agen_id where channel_agen.agen_id='"+agenId+"'";
126 con.query(sql, function (err, result) {
127 if (err) throw err;
128 //CURL TO SMTEL
129
130 if(result.length <= 0){
131 LIBRARY.xmpp.send(from,'DATA NOT FOUND')
132 return 'DATA NOT FOUND';
133 }
134 var partnerTrxId = LIBRARY.uniqid(agenId+'-');
135
136 let smtelData = {
137 channelId: result[0].channel_id,
138 storeId:result[0].store_id,
139 posId:'',
140 secretKey: result[0].chan_suffix,
141 pin:pin
142 };
143
144 const password = LIBRARY.function.createSMTelSignature(
145 partnerTrxId,
146 trxTime,
147 smtelData
148 );
149
150 const smtelUrl =
151 GLOBAL_CONFIG.smtel.baseurl +
152 "?channelid=" +
153 result[0].channel_id +
154 "&password=" +
155 password +
156 "&cmd=" +
157 "topup" +
158 "&hp=" +
159 msidn +
160 "&vtype=" +
161 productCode +
162 "&trxtime=" +
163 trxTime +
164 "&partner_trxid=" +
165 partnerTrxId +
166 "&storeid=" +
167 result[0].store_id;
168
169 let isRequestTimeout = false;
170
171 const topupResponseXML = LIBRARY.axios
172 .get(smtelUrl, { timeout: 35000 })
173 .then(function(response) {
174 topupResponseJSON = LIBRARY.xml2json.toJson(response.data, {
175 object: true
176 });
177 var resMessage = 'STATUS:'+topupResponseJSON.response.rescode+','+
178 'TOPUP:'+topupResponseJSON.response.hp+' '+topupResponseJSON.response.scrmessage+','+
179 'SN:'+topupResponseJSON.response.sn +','+
180 'TRXID:'+topupResponseJSON.response.server_trxid +','+
181 'BALANCE:'+topupResponseJSON.response.balance +','+
182 'PRICE:'+topupResponseJSON.response.harga
183
184 LIBRARY.xmpp.send(from,resMessage)
185 })
186 .catch(function(err) {
187 if (err.code === "ECONNABORTED") {
188 isRequestTimeout = true;
189 } else {
190 throw err;
191 }
192 });
193 });
194 });
195 }
196}