LacunaWebPKI: Send authenticated request Example

by Lacuna Software

HTML

<script src="https://get.webpkiplugin.com/Scripts/LacunaWebPKI/lacuna-web-pki-2.9.0.js"></script>
<h2>Web PKI - Send authenticated request Example</h2>
<select id="certificateSelect"></select>
<button id="refreshCertificates" type="button">Refresh Certificates</button><br/><br/>
<button id="sendRequest" type="button">Send</button><br/>

<h3>Response</h3>
<textarea id="responseArea" rows="18" cols="170"></textarea>
<div id="logPanel"></div>

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI: Send authenticated request Example

var pki = new LacunaWebPKI('AiEAKi5sYWN1bmFzb2Z0d2FyZS5jb20sanNmaWRkbGUubmV0AAADAFBybwgAABg/D+l74AgAAVP0dSRjd5vjlQ8EFzCfN2tOR/S2ENB6x1vRXssle+wxvbEeT3/vmzm7eKDMWr8kgl2W/BfUqYCOC0PnLOVKS8Em0OXtYD+F3WI1+3zQcvI2W34ppGVx1zizoRfyq+Q4KZ9rZPtFYyoBmr1SR5ZyWPqXWNY7p5XWFkxvFmQU11jho5Cd8VX6cv2OuOEw4eBptN5whzV+9XPHEmPav+TJB0hsYbw0TzwLC253i8WhJEQyIVenNGNNHT3foxsFmDTvXqF/HgjOwqgyJJ1hL0wIqsN0pB5weYhjGLwlRt1FqLV1xTScs5nP3xWTK/CPBpq3NDdTobQMsc7PEF46RT1qXbg=');

// Setup the request parameters
var request = {
    // Service URL
    url: 'https://nfe-homologacao.svrs.rs.gov.br/ws/NfeStatusServico/NfeStatusServico4.asmx',
    method: 'post',
    headers: {
        'Content-Type': 'application/soap+xml; charset=utf-8'
    },
    // Request SOAP envelope
    body: '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4"><consStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00"><tpAmb>2</tpAmb><cUF>53</cUF><xServ>STATUS</xServ></consStatServ></nfeDadosMsg></soap12:Body></soap12:Envelope>'
};
// ----------------------------------------

function sendRequest() {
    // Set the user certificate to authenticate the request
    request.certificateThumbprint = $('#certificateSelect').val();
    if (!request.certificateThumbprint) {
        alert('Please select a certificated');
        return;
    }
    
    log('Sending request')
    pki.sendAuthenticatedRequest(request).success(function (response) {
        log('Printing response');
        printResponse(response);
    });
}

function printResponse(response) {
    var contentText = '';
    if...