-
Notifications
You must be signed in to change notification settings - Fork 7
/
invoiceListener
executable file
·73 lines (65 loc) · 2.53 KB
/
invoiceListener
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
#!/usr/bin/env node
// vi: ft=javascript
/*
©2012-2014 BITPAY, INC.
Permission is hereby granted to any person obtaining a copy of this software
and associated documentation for use and/or modification in association with
the bitpay.com service.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var https = require('https')
, fs = require('fs')
, querystring = require('querystring')
, config = require('./config');
var port = process.argv[2]; // The port to listen on
if(!port) port = config.listenerPort || 8000;
var handleNotification = function(notification) {
// Each notification is written to stdout, one line at a time, as a json
// encoded object. A client can process and parse stdout. Errors are written to
// stderr and are also in json notation. Alternatively, this function can
// be altered and customized to handle incoming notifications
console.log(JSON.stringify(notification));
};
var options = {
key: fs.readFileSync(config.sslKey || './ssl-key.pem'),
cert: fs.readFileSync(config.sslCert || './ssl-cert.pem'),
agent: false,
requestCert: true,
rejectUnauthorized: false,
};
var parseBody = function(data, encoding) {
if(encoding == 'application/json') {
return JSON.parse(data);
} else {
return querystring.parse(data);
}
};
https.createServer(options, function (req, res) {
var data = '', contentType = req.headers['content-type'];
if((contentType == 'application/json') || (contentType == 'application/x-www-form-urlencoded')) {
var contentType = req.headers['content-type'];
req.on('data', function(chunk) {data += chunk});
req.on('end', function() {
var notification;
try {
notification = parseBody(data.toString(), contentType);
} catch(e) {
console.error(JSON.stringify({error: ('parsing error: '+e)}));
}
if(notification) handleNotification(notification);
res.writeHead(200);
res.end();
});
} else {
console.error('{"warning": "malformed request"}');
res.writeHead(400);
res.write(JSON.stringify({error: {type: 'badRequest', message: 'unsupported encoding'}}))
res.end();
}
}).listen(port);