Sample code for 30+ languages & platforms
Dart

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

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

  final gen = CkXmlDSigGen();

  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)
  final cert = CkCert();
  try {
    cert.loadPfxFile('qa_data/pfx/cert_test123.pfx', 'test123');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  gen.setX509Cert(cert, true);

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

  // Load XML to be signed...
  final sbXml = CkStringBuilder();
  xmlToSign.emitXmlDecl = false;
  xmlToSign.getXmlSb(sbXml);

  gen.behaviors = 'IndentedSignature';

  // Sign the XML...
  try {
    gen.createXmlDSigSb(sbXml);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  final http = CkHttp();

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

  // The endpoint for this soap service is:
  final 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>

  final xml = CkXml();
  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.
  final sbSoapXml = CkStringBuilder();
  sbSoapXml.append(xml.getXml());
  var 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());

  final xmlStr = sbSoapXml.getAsString();
  final resp = CkHttpResponse();
  try {
    http.httpStr('POST', endPoint, xmlStr, 'utf-8', 'text/xml', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final responseStatusCode = resp.statusCode;

  print('Response Status Code: $responseStatusCode');

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

  // Examine the XML returned by the web service:
  print('XML Response:');
  final xmlResp = CkXml();
  xmlResp.loadXml(resp.bodyStr);
  print(xmlResp.getXml());

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