Sample code for 30+ languages & platforms
SQL Server

SOAP Request to Issue Documents in Facto.cl

See more HTTP Misc Examples

Demonstrates how to make a SOAP HTTP request to issue a document using the facto.cl webservice.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- --------------------------------------------------------------------------------
    -- Also see Chilkat's Online WSDL Code Generator
    -- to generate code and SOAP Request and Response XML for each operation in a WSDL.
    -- --------------------------------------------------------------------------------

    -- Here is an example of an HTTP POST Request we'll be sending:

    -- POST /documento.php HTTP/1.1
    -- Content-Type: text/xml;charset=UTF-8
    -- SOAPAction: https://conexion.facto.cl/documento.php/emitirDocumento
    -- 
    -- <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:urn="urn:server">
    --    <soapenv:Header/>
    --    <soapenv:Body>
    --       <urn:emitirDocumento soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    --          <documento xsi:type="urn:emitir_dte">
    --             <encabezado xsi:type="urn:encabezado">
    --                <tipo_dte xsi:type="xsd:string">39</tipo_dte>
    --                <fecha_emision xsi:type="xsd:date">2019-08-03</fecha_emision>
    --                <condiciones_pago xsi:type="xsd:string">0</condiciones_pago>
    --                <receptor_email xsi:type="xsd:string">example@gmail.com</receptor_email>
    --             </encabezado>
    --             <detalles xsi:type="urn:detalles">
    --                <detalle xsi:type="urn:detalle">
    --                   <cantidad xsi:type="xsd:int">3</cantidad>
    --                   <glosa xsi:type="xsd:string">Recarga Agua Purificada</glosa>
    --                   <monto_unitario xsi:type="xsd:decimal">1932.773109</monto_unitario>
    --                   <exento_afecto xsi:type="xsd:boolean">1</exento_afecto>
    --                </detalle>
    --             </detalles>
    --             <totales xsi:type="urn:totales">
    --                <total_exento xsi:type="xsd:int">0</total_exento>
    --                <total_afecto xsi:type="xsd:int">5798</total_afecto>
    --                <total_iva xsi:type="xsd:int">1102</total_iva>
    --                <total_final xsi:type="xsd:Int">6900</total_final>
    --             </totales>
    --          </documento>
    --       </urn:emitirDocumento>
    --    </soapenv:Body>
    -- </soapenv:Envelope>

    -- Use this online tool to generate code from sample XML: 
    -- Generate Code to Create XML

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @xml, 'Tag', 'soapenv:Envelope'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:urn', 'urn:server'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header', ''
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento', 1, 'soapenv:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento', 1, 'xsi:type', 'urn:emitir_dte'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|encabezado', 1, 'xsi:type', 'urn:encabezado'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte', 1, 'xsi:type', 'xsd:string'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|tipo_dte', '39'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision', 1, 'xsi:type', 'xsd:date'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|fecha_emision', '2019-08-03'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago', 1, 'xsi:type', 'xsd:string'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|condiciones_pago', '0'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email', 1, 'xsi:type', 'xsd:string'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|encabezado|receptor_email', 'example@gmail.com'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles', 1, 'xsi:type', 'urn:detalles'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle', 1, 'xsi:type', 'urn:detalle'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad', 1, 'xsi:type', 'xsd:int'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|cantidad', '3'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa', 1, 'xsi:type', 'xsd:string'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|glosa', 'Recarga Agua Purificada'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario', 1, 'xsi:type', 'xsd:decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|monto_unitario', '1932.773109'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto', 1, 'xsi:type', 'xsd:boolean'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|detalles|detalle|exento_afecto', '1'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|totales', 1, 'xsi:type', 'urn:totales'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_exento', 1, 'xsi:type', 'xsd:int'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_exento', '0'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto', 1, 'xsi:type', 'xsd:int'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_afecto', '5798'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_iva', 1, 'xsi:type', 'xsd:int'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_iva', '1102'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_final', 1, 'xsi:type', 'xsd:Int'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|urn:emitirDocumento|documento|totales|total_final', '6900'

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', '9.999.999-9/mylogin'
    EXEC sp_OASetProperty @http, 'Password', 'mypassword'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'SoapAction', 'https://conexion.facto.cl/documento.php/emitirDocumento'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', 'https://conexion.facto.cl/documento.php', @sTmp0, 'utf-8', 'text/xml', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- A status code = 200 indicates success.

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'Response status code = ' + @iTmp0

    DECLARE @xmlResp int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlResp OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @xmlResp, 'LoadXml', @success OUT, @sTmp0

    PRINT 'Response body text:'
    EXEC sp_OAMethod @xmlResp, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    -- The response will look like this:

    -- <?xml version="1.0" encoding="UTF-8"?>
    -- <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    --     <SOAP-ENV:Body>
    --         <ns1:emitirDocumentoResponse xmlns:ns1="urn:server">
    --             <return xsi:type="xsd:complexType">
    --                 <resultado>
    --                     <status>0</status>
    --                     <mensaje_error/>
    --                 </resultado>
    --                 <encabezado>
    --                     <tipo_dte>39</tipo_dte>
    --                     <folio>109999</folio>
    --                     <fecha_emision>2019-08-03</fecha_emision>
    --                     <condiciones_pago>0</condiciones_pago>
    --                     <receptor_email>example@gmail.com</receptor_email>
    --                 </encabezado>
    --                 <detalles>
    --                     <cantidad>3</cantidad>
    --                     <glosa>Recarga Agua Purificada</glosa>
    --                     <monto_unitario>1932.773109</monto_unitario>
    --                     <exento_afecto>1</exento_afecto>
    --                 </detalles>
    --                 <totales>
    --                     <total_exento>0</total_exento>
    --                     <total_afecto>5798</total_afecto>
    --                     <total_iva>1102</total_iva>
    --                     <total_final>6900</total_final>
    --                 </totales>
    --                 <enlaces>
    --                     <dte_xml>https://temporal.facto.cl/2019/08/999993022aa9e0733174e87086ade7d4.xml</dte_xml>
    --                     <dte_pdf>https://temporal.facto.cl/2019/08/99999f52cf80c8ffc1fc22338dae022.pdf</dte_pdf>
    --                     <dte_timbre>https://temporal.facto.cl/2019/08/999999856c08b3d24a83bf29add5364a.png</dte_timbre>
    --                 </enlaces>
    --             </return>
    --         </ns1:emitirDocumentoResponse>
    --     </SOAP-ENV:Body>
    -- </SOAP-ENV:Envelope>

    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @xmlResp


END
GO