Forked from srsbiz/sprawdzenieStatusuPodmiotuWVAT.js
Last active
July 1, 2017 19:42
-
-
Save amnich/d20a1d8e6a7098f9f58c43c90b6a28dd to your computer and use it in GitHub Desktop.
Skrypt do sprawdzenia statusu płatnika VAT w PhantomJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function waitFor(testFunction, readyFunction, timeOutMillis) { | |
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, | |
start = new Date().getTime(), | |
condition = false, | |
interval = setInterval(function() { | |
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { | |
condition = (typeof(testFunction) === "string" ? eval(testFunction) : testFunction()); | |
} else { | |
if(!condition) { | |
phantom.exit(1); | |
} else { | |
typeof(readyFunction) === "string" ? eval(readyFunction) : readyFunction(); | |
clearInterval(interval); | |
} | |
} | |
}, 250); | |
}; | |
var system = require('system'), | |
args = system.args, | |
nip, | |
nieJest = "Podmiot o podanym identyfikatorze podatkowym NIP nie jest zarejestrowany jako podatnik VAT", | |
Jest = "NIP jest zarejestrowany", | |
page = require('webpage').create(); | |
if (1 === args.length) { | |
console.log('NIP is required'); | |
phantom.exit(1); | |
} else { | |
nip = args[1]; | |
} | |
page.open('https://ppuslugi.mf.gov.pl/', function(status) { | |
if (status !== "success") { | |
console.log("Unable to access network"); | |
phantom.exit(); | |
} else { | |
waitFor(function(){ | |
return page.evaluate(function() { return $('#SidebarActions_WebUMn li').length > 0; }); | |
}, function(){ | |
page.evaluate(function() { | |
FWDC.executeAction(1005, null, 'FLOW'); | |
}); | |
waitFor(function(){ | |
return page.evaluate(function() { return $('#b-7').length > 0; }); | |
}, function(){ | |
page.evaluate(function(nip) { | |
$('#b-7').val(nip); | |
$('#b-8').trigger('click'); | |
}, nip); | |
waitFor(function(){ | |
return page.evaluate(function() { return $('#container_b-3').is(':visible') }); | |
}, function() { | |
var response = { | |
"nip": nip, | |
"status": page.evaluate(function() { return $.trim($('#container_b-3').text()); }).indexOf(Jest) >= 0, | |
"message": page.evaluate(function() { return $.trim($('#container_b-3').text()); }) | |
}; | |
var status2 = page.evaluate(function() { return $.trim($('#container_b-3').text()); }) == nieJest ? false : true; | |
if (status2 === response.status){ | |
system.stdout.write(JSON.stringify(response)); | |
} | |
else{ | |
console.log("status Jest is not equal status NieJest"); | |
} | |
phantom.exit(); | |
}); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment