-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (164 loc) · 6.81 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const axios = require('axios');
const baseURL = "https://donatebot.io/api/v1";
const userAgent = "Donate-Bot-Node-API/1.0.3";
/**
* Create a new instance of Donate Bot Node API
* @param {Object} options
* @param {string} options.serverID Your server ID
* @param {string} options.apiKey Your Donate Bot API key
* @return {Object} An object of functions to call
*/
module.exports = function(options) {
// Options {
// serverID: "Your Discord Server ID",
// apiKey: "Your Donate Bot API key from the panel"
// }
//
var checkOptions = function () {
if (options.serverID === "" || options.serverID === undefined || options.serverID === null || typeof options.serverID !== "string") {
return "Please provide a valid Discord server ID as a string."
}
if (options.apiKey === "" || options.apiKey === undefined || options.apiKey === null || typeof options.serverID !== "string") {
return "Please provide a valid Donate Bot API key."
}
return "";
}
/**
* Get new donations that have not been marked as processed
* @param {Object} getOptions
* @param {string[]} getOptions.find An array containing statuses Ex: "Completed", "Reversed", "Refunded"
* @return {Promise.<Object[]>} An array of transactions that have not been marked
*/
var getNewDonations = function(getOptions) {
return new Promise(async (resolve, reject) => {
var checked = checkOptions();
if (checked !== "") {
return reject(checked);
}
// getOptions: {
// find: ["Reversed", "Refunded", "Completed"]
// }
var findParams = "";
if (typeof getOptions === 'object' && getOptions !== null && Array.isArray(getOptions.find)) {
getOptions.find = getOptions.find.filter(x => {
if (x === "Reversed" || x === "Refunded" || x === "Completed") {
return true;
}
});
findParams = getOptions.find.join(',');
}
axios({
method: 'get',
url: `${baseURL}/donations/${options.serverID}/new`,
params: {
find: findParams
},
headers: {
"Authorization": options.apiKey,
"User-Agent": userAgent
}
})
.then(function (response) {
// Return the array of donations
return resolve(response.data.donations);
})
.catch(function (error) {
if (error.response && error.response.data && error.response.data['Error']) {
return reject(error.response.data['Error']);
}
return reject(error);
});
});
}
/**
* Get an array of ended subscriptions
* @return {Promise.<Object[]>} Array of ended subscriptions
*/
var getEndedSubscriptions = function() {
return new Promise(async (resolve, reject) => {
var checked = checkOptions();
if (checked !== "") {
return reject(checked);
}
axios({
method: 'get',
url: `${baseURL}/donations/${options.serverID}/endedsubscriptions`,
headers: {
"Authorization": options.apiKey,
"User-Agent": userAgent
}
})
.then(function (response) {
// Return the array of donations
return resolve(response.data.endedSubscriptions);
})
.catch(function (error) {
if (error.response && error.response.data && error.response.data['Error']) {
return reject(error.response.data['Error']);
}
return reject(error);
});
});
}
/**
* Mark a donation or subscription ended as processed after you have done something with it
* @param {Object} markOptions
* @param {Boolean} markOptions.isEndedSubscription If you are marking an ended subscription
* @param {Boolean} markOptions.markProcessed
* @return {Promise.<void>} The transaction was completed
*/
var markDonation = function(markOptions) {
return new Promise(async (resolve, reject) => {
var checked = checkOptions();
if (checked !== "") {
return reject(checked);
}
var defaultMarkOptions = {
isEndedSubscription: false,
markProcessed: true
}
if (typeof markOptions === 'object' && markOptions !== null) {
if (markOptions.isEndedSubscription && typeof markOptions.isEndedSubscription === "boolean") {
defaultMarkOptions.isEndedSubscription = markOptions.isEndedSubscription;
}
if (markOptions.markProcessed && typeof markOptions.markProcessed === "boolean") {
defaultMarkOptions.markProcessed = markOptions.markProcessed;
}
if (markOptions.txnID && typeof markOptions.txnID === "string") {
defaultMarkOptions.txnID = markOptions.txnID;
} else {
return reject("txnID must be provided as a string inside options.")
}
} else {
return reject("An object of options are required for this function. See documentation at https://developers.donatebot.io")
}
axios({
method: 'post',
url: `${baseURL}/donations/${options.serverID}/${defaultMarkOptions.txnID}/mark`,
headers: {
"Authorization": options.apiKey,
"User-Agent": userAgent
},
data: {
isEndedSubscription: defaultMarkOptions.isEndedSubscription,
markProcessed: defaultMarkOptions.markProcessed
}
})
.then(function (response) {
// Return nothing as the API does not return anything
return resolve();
})
.catch(function (error) {
if (error.response && error.response.data && error.response.data['Error']) {
return reject(error.response.data['Error']);
}
return reject(error);
});
});
}
return {
getNewDonations,
getEndedSubscriptions,
markDonation
}
}