-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_pay_communication.js
83 lines (71 loc) · 1.71 KB
/
client_pay_communication.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
/*
* IOTA PAY - Payment client website communication part
*
*
* flashchannel - transport and status functions
*
* */
const RETRY_MAX = 3;
var retry_cnt = 0;
var topayserver = function(url_payserv, send_json, callback_finished)
{
if(retry_cnt > RETRY_MAX)
{
retry_cnt = 0;
callback_finished({accepted:false, commerr:true, error:'topayserver communication error.'});
return;
}
try
{
var xhr = new XMLHttpRequest();
if(xhr) {
xhr.open('POST', url_payserv, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) // Request done
{
try
{
if(xhr.status == 200)
{
let retObj;
try
{
retObj = JSON.parse(xhr.responseText);
}catch(e)
{
retry_cnt = RETRY_MAX + 1; // Dont retry if data sent is corrupted
topayserver(url_payserv, send_json, callback_finished);
return;
}
callback_finished(retObj);
return;
}else{
console.error('topayserver communication error. No1 Status:'+xhr.statusText);
// retry
retry_cnt++;
topayserver(url_payserv, send_json, callback_finished);
}
}catch(e)
{
console.error('topayserver communication error. No02'+e);
// retry
retry_cnt++;
topayserver(url_payserv, send_json, callback_finished);
}
}
};
// Send request
xhr.send(JSON.stringify(send_json));
}
}catch(e)
{
console.error('topayserver communication error. No3');
// retry
retry_cnt++;
topayserver(url_payserv, send_json, callback_finished);
}
}
module.exports = {
'topayserver' : topayserver
}