Sample code for 30+ languages & platforms
React Native

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

Demonstrates how to call getToken SOAP request at palena.sii.cl

Chilkat React Native Downloads

React Native
import { Cert, Http, HttpResponse, StringBuilder, Xml, XmlDSigGen } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Create the XML to be signed...
  const xmlToSign = new Xml();
  xmlToSign.tag = 'getToken';
  // This is the seed obtained from palena.sii.cl getSeed
  xmlToSign.updateChildContent('item|Semilla', '033878794660');

  const gen = new XmlDSigGen();

  gen.sigLocation = 'getToken';
  gen.sigLocationMod = 0;
  gen.sigNamespacePrefix = '';
  gen.sigNamespaceUri = 'http://www.w3.org/2000/09/xmldsig#';
  gen.signedInfoCanonAlg = 'EXCL_C14N';
  gen.signedInfoDigestMethod = 'sha1';

  gen.addSameDocRef('', 'sha1', '', '', '');

  // Provide a certificate + private key. (PFX password is test123)
  const cert = new Cert();
  try {
    cert.loadPfxFile('qa_data/pfx/cert_test123.pfx', 'test123');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  gen.setX509Cert(cert, true);

  gen.keyInfoType = 'X509Data';
  gen.x509Type = 'Certificate';

  // Load XML to be signed...
  const sbXml = new StringBuilder();
  xmlToSign.emitXmlDecl = false;
  xmlToSign.getXmlSb(sbXml);

  gen.behaviors = 'IndentedSignature';

  // Sign the XML...
  try {
    await gen.createXmlDSigSbAsync(sbXml);
  } catch {
    console.log(gen.lastErrorText);
    return;
  }

  // -----------------------------------------------

  const http = new Http();

  http.uncommonOptions = 'AllowEmptyHeaders';
  http.setRequestHeader('SOAPAction', '');

  // The endpoint for this soap service is:
  const endPoint = 'https://palena.sii.cl/DTEWS/GetTokenFromSeed.jws';

  // Send the following SOAP XML

  // <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
  //    <soapenv:Header/>
  //    <soapenv:Body>
  //       <def:getToken soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  //          <pszXml>SIGNED_XML_GOES_HERE</pszXml>
  //       </def:getToken>
  //    </soapenv:Body>
  // </soapenv:Envelope>

  const xml = new Xml();
  xml.tag = 'soapenv:Envelope';
  xml.addAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  xml.addAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
  xml.addAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
  xml.addAttribute('xmlns:def', 'http://DefaultNamespace');
  xml.updateChildContent('soapenv:Header', '');
  xml.updateAttrAt('soapenv:Body|def:getToken', true, 'soapenv:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/');
  xml.updateChildContent('soapenv:Body|def:getToken|pszXml', 'SIGNED_XML_GOES_HERE');

  // We must replace the "SIGNED_XML_GOES_HERE" with the exact contents of the signed XML to ensure the signed XML is not modified in any way.
  const sbSoapXml = new StringBuilder();
  sbSoapXml.append(xml.getXml());
  let numReplaced = sbXml.replace('&', '&amp;');
  numReplaced = sbXml.replace('>', '&gt;');
  numReplaced = sbXml.replace('<', '&lt;');
  numReplaced = sbXml.replace('"', '&quot;');
  numReplaced = sbSoapXml.replace('SIGNED_XML_GOES_HERE', sbXml.getAsString());

  const xmlStr = sbSoapXml.getAsString();
  const resp = new HttpResponse();
  try {
    await http.httpStrAsync('POST', endPoint, xmlStr, 'utf-8', 'text/xml', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const responseStatusCode = resp.statusCode;

  console.log(`Response Status Code: ${responseStatusCode}`);

  // You may examine the exact HTTP header sent with the POST like this:
  console.log('LastHeader:');
  console.log(http.lastHeader);

  // Examine the XML returned by the web service:
  console.log('XML Response:');
  const xmlResp = new Xml();
  xmlResp.loadXml(resp.bodyStr);
  console.log(xmlResp.getXml());

  // Use this online tool to generate parsing code from response XML: 
  // Generate Parsing Code from XML
}