Sample code for 30+ languages & platforms
Zig

SOAP e-factura.sunat.gob.pe getStatusCdr

See more HTTP Misc Examples

Make a SOAP call to https://e-factura.sunat.gob.pe/ol-it-wsconscpegem/billConsultService

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    // <soapenv:Envelope xmlns:ser="http://service.sunat.gob.pe"
    //   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    //   xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    //  <soapenv:Header>
    //   <wsse:Security>
    //    <wsse:UsernameToken>
    //     <wsse:Username>MY_USERNAME</wsse:Username>
    //     <wsse:Password>MY_PASSWORD</wsse:Password>
    //    </wsse:UsernameToken>
    //   </wsse:Security>
    //  </soapenv:Header>
    //  <soapenv:Body>
    //   <ser:getStatus>
    //    <rucComprobante>99999999999</rucComprobante>
    //    <tipoComprobante>01</tipoComprobante>
    //    <serieComprobante>F001</serieComprobante>
    //    <numeroComprobante>55555</numeroComprobante>
    //   </ser:getStatus>
    //  </soapenv:Body>
    //  </soapenv:Envelope>
    //

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soapenv:Envelope");
    xml.addAttribute("xmlns:ser", "http://service.sunat.gob.pe") catch {};
    xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.addAttribute("xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") catch {};
    xml.updateChildContent("soapenv:Header|wsse:Security|wsse:UsernameToken|wsse:Username", "MY_USERNAME");
    xml.updateChildContent("soapenv:Header|wsse:Security|wsse:UsernameToken|wsse:Password", "MY_PASSWORD");
    xml.updateChildContent("soapenv:Body|ser:getStatus|rucComprobante", "99999999999");
    xml.updateChildContent("soapenv:Body|ser:getStatus|tipoComprobante", "01");
    xml.updateChildContent("soapenv:Body|ser:getStatus|serieComprobante", "F001");
    xml.updateChildContent("soapenv:Body|ser:getStatus|numeroComprobante", "55555");

    const str_xml = try xml.getXml(alloc);

    http.setRequestHeader("SOAPAction", "urn:getStatusCdr");

    const end_point = "https://e-factura.sunat.gob.pe/ol-it-wsconscpegem/billConsultService";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("POST", end_point, str_xml, "utf-8", "text/xml", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    var status_code: i32 = resp.getStatusCode();
    if (status_code != 200) {
        std.debug.print("Non-success status code: {d}\n", .{status_code});
        std.debug.print("Response header: {s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Response body: {s}\n", .{try resp.getBodyStr(alloc)});
        std.debug.print("Unsuccessful.\n", .{});
        return;
    }

    const xml_resp = try chilkat.Xml.init();
    defer xml_resp.deinit();
    xml_resp.loadXml(try resp.getBodyStr(alloc)) catch {};

    std.debug.print("{s}\n", .{try xml_resp.getXml(alloc)});

    // A sample response:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    //     <S:Body>
    //         <ns0:getStatusResponse xmlns:ns0="http://service.sunat.gob.pe">
    //             <status>
    //                 <statusCode>0001</statusCode>
    //                 <statusMessage>El comprobante existe y est� aceptado.</statusMessage>
    //             </status>
    //         </ns0:getStatusResponse>
    //     </S:Body>
    // </S:Envelope>
    //

    // Parse the response...
    _ = try xml.getAttrValue(alloc, "xmlns:S");
    _ = try xml.chilkatPath(alloc, "S:Body|ns0:getStatusResponse|(xmlns:ns0)");
    status_code = xml.getChildIntValue("S:Body|ns0:getStatusResponse|status|statusCode");
    _ = try xml.getChildContent(alloc, "S:Body|ns0:getStatusResponse|status|statusMessage");
}