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('AiEAKi5sYWN1bmFzb2Z0d2FyZS5jb20sanNmaWRkbGUubmV0AAACAENBCAAAGD8P6XvgCAABL3ZCfMfZGteSXNb2XNHBws9WZcTv0FCVe4qlxUXgaMzx4iymA73nVrD55o+S83b/alfQHs2xoIoPd6qiBDqbefk4GDnc84efZAX4UV7sHYr5x6+IRZ+RjW1DVZkJ9vo2/3/JeSRnj1Yvn3jtDf77jz2osQkFC5jSLABzCMAC/LXqPhOdZcK8hoyWOaghcsGvb5eezEFeJl+nkjDgZ6PsggdDpyWwLBFru4IeSRLE1HeP6TE6g5RQFjFHqNNCFRpP23/FQGD9/YDi2mR07lUxznt92z8GYygkp6O0Nv3ltj3eDGzN89LlgTBgsA2upsm0tTIgkWTDsKvqY6TwfsQ5tA==');

// 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...