-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (88 loc) · 3.6 KB
/
index.js
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
const http = require("http");
const axios = require("axios");
const md5 = require('js-md5');
let pollingPort;
module.exports = class VKCOIN {
constructor (auth) {
this.key = auth.key;
this.id = auth.id;
}
async startServer (props) {
if (!props.server) throw new Error("Ошибка запуска сервера: не указан сервер для получения событий!");
pollingPort = props.port || 1234;
try {
return (await axios({
method: "POST",
url: "https://coin-without-bugs.vkforms.ru/merchant/set/",
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ callback: `http://${props.server}:${pollingPort}`, merchantId: this.id, key: this.key })
})).data;
} catch (e) { throw new Error(e.message || "Ошибка сервера!"); }
}
async startPolling (hand) {
try {
http.createServer((req, res) => {
if (req.method === "POST") {
let body = "";
req.on("data", chunk => {
body += chunk.toString();
if(body.length > 1e6) {
req.connection.destroy();
body = "";
};
});
req.on("end", () => {
const event = JSON.parse(body);
const key = md5(event.id + ';' + event.fromId + ';' + event.amount + ';' + event.payload + ';' + this.key);
if(event.key !== key) {
req.connection.destroy();
return;
};
res.writeHead(200, "OK");
res.end("OK");
hand(event);
});
}
}).listen(pollingPort);
} catch (e) { throw new Error(e.message || "Ошибка сервера!"); }
}
async send (toId, amount, fromShop) {
const dataJSON = fromShop ? { merchantId: this.id, key: this.key, toId, amount, markAsMerchant: true } : { merchantId: this.id, key: this.key, toId, amount };
try {
return (await axios({
method: "POST",
url: "https://coin-without-bugs.vkforms.ru/merchant/send/",
headers: { "Content-Type": "application/json" },
data: JSON.stringify(dataJSON)
})).data;
} catch (e) { throw new Error(e.message || "Ошибка сервера!"); }
}
async getBalance (id) {
try {
id = Array.isArray(id) ? id : [id];
if (id.length > 100) throw new Error("Ошибка: Длина массива должна быть не более 100!");
return (await axios({
method: "POST",
url: "https://coin-without-bugs.vkforms.ru/merchant/score/",
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ merchantId: this.id, key: this.key, userIds: id })
})).data;
} catch (e) { throw new Error(e.message || "Ошибка сервера!"); }
}
async shopName (name) {
try {
return (await axios({
method: "POST",
url: "https://coin-without-bugs.vkforms.ru/merchant/set/",
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ merchantId: this.id, key: this.key, name })
})).data;
} catch (e) { throw new Error(e.message || "Ошибка сервера!"); }
}
getLink (amount, payload, isFixed) {
if (!this.id) throw new Error("Ошибка: Для начала необходимо авторизироваться!");
if (isNaN(Number(amount))) throw new Error("Ошибка: Недопустимый формат VK Coin!");
return `vk.com/coin#x${this.id}_${Number(amount)}_${payload}${isFixed ? "" : "_1"}`;
}
format (amount) { return (Number(amount) / 1000).toLocaleString("ru-RU"); }
}