-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsc2-pay.js
122 lines (94 loc) · 3.98 KB
/
sc2-pay.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
var sc2_pay = (function() {
var win = null;
var options = {
check_transfer: true
};
function requestPayment(title, to, amount, currency, memo, callback) {
if(currency != 'STEEM' && currency != 'SBD') {
console.log('Unsupported currency "' + currency + '". Supported currencies are "STEEM" or "SBD".');
return;
}
var url = sc2.sign('transfer', {
to: to,
amount: amount.toFixed(3) + ' ' + currency,
memo: memo
});
win = popupCenter(url, 'sc2-pay-test', 500, 560);
if(options.check_transfer)
checkSteemTransfer(to, amount, currency, memo, new Date(), callback);
}
function requestPaymentVessel(title, to, amount, currency, memo, callback) {
if(currency != 'STEEM' && currency != 'SBD') {
console.log('Unsupported currency "' + currency + '". Supported currencies are "STEEM" or "SBD".');
return;
}
var transaction = JSON.stringify([['transfer', {
from: '',
to: to,
amount: amount.toFixed(3) + ' ' + currency,
memo: memo
}]]);
var url = 'steem://sign/tx/' + btoa(transaction) + '#eyJhbW91bnQiOnsicHJvbXB0IjpmYWxzZSwidHlwZSI6ImFzc2V0IiwibGFiZWwiOiJEb25hdGlvbiJ9LCJtZW1vIjp7InByb21wdCI6ZmFsc2UsInR5cGUiOiJ0ZXh0IiwibGFiZWwiOiJNZXNzYWdlIChPcHRpb25hbCkifSwidG8iOnsicHJvbXB0IjpmYWxzZSwidHlwZSI6InRleHQifX0=';
window.location = url;
if(options.check_transfer)
checkSteemTransfer(to, amount, currency, memo, new Date(), callback, 0);
}
function setOptions(new_options) {
options = new_options;
}
function getCurrency(amount) {
return amount.substr(amount.indexOf(' ') + 1);
}
var cancel_check = false;
function checkSteemTransfer(to, amount, currency, memo, date, callback, retries) {
if (cancel_check || retries > 60) {
cancel_check = false;
win = null;
if (callback)
callback(null);
return;
}
console.log('Checking Steem Transfer...' + memo + ', amount: ' + amount + ' ' + currency);
steem.api.getAccountHistory(to, -1, 10, function (err, result) {
var confirmed = false;
for (var i = 0; i < result.length; i++)
{
var trans = result[i];
var op = trans[1].op;
var ts = new Date((trans[1].timestamp) + 'Z');
if (ts > date && op[1].memo == memo && op[0] == 'transfer' && op[1].to == to && parseFloat(op[1].amount) == amount && getCurrency(op[1].amount) == currency) {
// The payment went through successfully!
confirmed = true;
// Close the popup window
if(win)
win.close();
if(callback)
callback(trans);
break;
}
}
if (!confirmed)
setTimeout(function () { checkSteemTransfer(to, amount, currency, memo, date, callback, retries + 1); }, 2000);
});
}
function close() {
if(win)
win.close();
}
function popupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
return newWindow;
}
return { requestPayment: requestPayment, requestPaymentVessel: requestPaymentVessel, setOptions: setOptions, close: close };
})();