UNPKG

eppgate

Version:

Domain Registrar

147 lines (136 loc) 3.83 kB
var http = require("http"); var qs = require("querystring"); var sessionID = "payment_epp_sessionid"; var sessionValue, dbh, host, port, path; var nocb = function(err, rows) {}; var cache = {}; function init(__registry, __dbh, __pay) { var expire = 1000*60*5; sessionValue = __registry; dbh = __dbh; host = __pay.host; port = __pay.port; path = __pay.path; setInterval(function() { var timestamp = new Date().getTime() - expire; for(var clID in cache) { if(cache[clID].timestamp < timestamp) delete cache[clID]; } dbh.query("select * from bills where ack='no'", function(err, rows) { if(err || !rows.length) return; for(var i in rows) __done(rows[i].id, rows[i]); }); }, expire); } function post(command, json, cb) { json.command = command; var content = qs.stringify(json); var options = { host: host, port: port, path: path, method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": content.length, "Cookie": sessionID + "=" + sessionValue } }; var req = http.request(options, function(res) { var content = ''; res.on('data', function(data) { content+= data; }); res.on('end', function() { try { (res.statusCode===200) ? cb(JSON.parse(content)) : cb(null); } catch(e) { cb(null); } }); }); req.on('error', function(e) { cb(null); }); req.write(content); req.end(); } function getACC(clID, pass, ok_cb, err_cb) { var timestamp = new Date().getTime(); if(cache[clID] && cache[clID].pass===pass) { cache[clID].timestamp = timestamp; ok_cb(cache[clID].user, cache[clID].acl); } else { post('login', { user: clID, pass: pass }, function(info) { if(info!==null) { cache[clID] = { user: info.user, pass: pass, acl : info.acl, timestamp: timestamp }; ok_cb(info.user, info.acl); } else err_cb(); }); } } function passwd(clID, pass) { post('passwd', { user: clID, pass: pass }, function(ack) { if(!ack) return; if(cache[clID]) cache[clID].pass = pass; }); } function cando(user, op, domain, period, ok_cb, err_cb) { post('cando', { user: user, op: op, domain: domain, period: period }, function(price) { (price!==null) ? ok_cb(price) : err_cb(); }); } function __done(id, r) { function DATE(d) { return (typeof(d)==='string') ? d : d.toISOString(); } post('done', { user: r.user, op: r.op, domain: r.domain, appID: r.appID, registrant: r.registrant, opDate: DATE(r.opDate), price: r.price, period: r.period, exDate: DATE(r.exDate), oldID: r.oldID, uniID: r.uniID }, function(ack) { if(!ack) return; dbh.query("update bills set ack='yes' where id=?", [id], nocb); }); } function done(rec) { dbh.query("insert into bills values(0,?,?,?,?,?,?,?,?,?,?,?,'no')", [rec.user, rec.op, rec.domain, rec.appID, rec.registrant, rec.opDate, rec.price, rec.period, rec.exDate, rec.oldID, rec.uniID], function(err, result) { !err && __done(result.insertId, rec); }); } exports.init = init; exports.getACC = getACC; exports.passwd = passwd; exports.cando = cando; exports.done = done;