-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathqiwi.js
88 lines (71 loc) · 2.2 KB
/
qiwi.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
let libPrefix = 'LibQiwiPayment_';
function getPaymentLink(options = {}){
return 'https://qiwi.com/payment/form/99?' +
'extra%5B%27account%27%5D='+ options.account +
'&amountInteger=' + options.amount +
'&extra%5B%27comment%27%5D=' + options.comment +
'¤cy=643'+
'&blocked[0]=account&blocked[1]=comment'
}
function setQiwiApiTokent(token){
Bot.setProperty(libPrefix + 'ApiToken', token, 'string');
}
function accept(item){
if(item.status!='SUCCESS'){ return false }
if(item.type!='IN'){ return false }
let accepted_payments = Bot.getProperty(libPrefix + 'accepted_payments');
if(!accepted_payments){ accepted_payments = {} }
if(accepted_payments[item.txnId]==true){
/* already accepted */
return false;
}
accepted_payments[item.txnId]=true
Bot.setProperty(libPrefix + 'accepted_payments', accepted_payments, 'json');
return true;
}
function onAcceptPayment(){
if(http_status=='401'){
Bot.sendMessage('Please verify API token');
return
}
let history = JSON.parse(content).data;
let prms = params.split(' ');
let comment = prms[0];
let onSuccess = prms[1];
let onNoPaymentYet = prms[2];
let payment;
for(it in history){
if(history[it].comment==comment){
payment = history[it];
if(accept(payment)){
Bot.runCommand(onSuccess + ' ' + String(payment.sum.amount));
return
}
}
}
Bot.runCommand(onNoPaymentYet);
}
function acceptPayment(options){
let apiToken = Bot.getProperty(libPrefix + 'ApiToken');
let headers = { 'Authorization': 'Bearer ' + apiToken,
'Content-type': 'application/json',
'Accept': 'application/json'
}
let url = 'https://edge.qiwi.com/payment-history/v2/persons/' +
options.account + '/payments?'+
'rows=50&operation=IN'
HTTP.get( {
url: url,
success: libPrefix + 'onAcceptPayment ' +
options.comment + ' ' + options.onSuccess +
' ' + options.onNoPaymentYet,
error: options.onError,
headers: headers
} )
}
publish({
getPaymentLink: getPaymentLink,
setQiwiApiTokent: setQiwiApiTokent,
acceptPayment: acceptPayment
})
on(libPrefix + 'onAcceptPayment', onAcceptPayment );