Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Send POST to Bradesco Platform with Billing Ticket for Registration

See more HTTP Misc Examples

Sends a POST request to the Bradesco platform containing the JSON data of the Billing Ticket for registration.

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.

    // First build the JSON containing the data to be sent.

    // {
    // "nuCPFCNPJ":"12668",
    // "filialCPFCNPJ":"1",
    // "ctrlCPFCNPJ":"59",
    // "cdTipoAcesso":"2",
    // "idProduto":"9",
    // "nuNegociacao":"262200000000005577",
    // "nuCliente":"SEU_NUM_CLIENTE",
    // "dtEmissaoTitulo":"21.09.2016",
    // "dtVencimentoTitulo":"21.09.2016",
    // "vlNominalTitulo":"888888",
    // "cdEspecieTitulo":"2",
    // "nomePagador":"NOME DO PAGADOR",
    // "logradouroPagador":"ENDERECO DO PAGADOR",
    // "nuLogradouroPagador":"1145",
    // "complementoLogradouroPagador":"APTO 34",
    // "cepPagador":"5588",
    // "complementoCepPagador":"1",
    // "bairroPagador":"BAIRRO PAGADOR",
    // "municipioPagador":"MUNICIPIO PAGADOR",
    // "ufPagador":"SP",
    // "cdIndCpfcnpjPagador":"2",
    // "nuCpfcnpjPagador":"12668000159",
    // "endEletronicoPagador":"PAGADOR@BRADESCO.COM.BR",
    // "nomeSacadorAvalista":"NOME SACADOR AVALISTA",
    // "logradouroSacadorAvalista":"ENDERECO SACADOR AVALISTA",
    // "nuLogradouroSacadorAvalista":"5555",
    // "complementoLogradouroSacadorAvalista":"BLOCO 23",
    // "cepSacadorAvalista":"6182",
    // "complementoCepSacadorAvalista":"160",
    // "bairroSacadorAvalista":"BAIRRO SACADOR AVALISTA",
    // "municipioSacadorAvalista":"MUNICIPIO SACADOR AVALISTA",
    // "ufSacadorAvalista":"SP",
    // "cdIndCpfcnpjSacadorAvalista":"2",
    // "nuCpfcnpjSacadorAvalista":"12668000159",
    // "endEletronicoSacadorAvalista":"SACADOR@BRADESCO.COM.BR",
    // }
    //

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("nuCPFCNPJ", "12668") catch {};
    json.updateString("filialCPFCNPJ", "1") catch {};
    json.updateString("ctrlCPFCNPJ", "59") catch {};
    json.updateString("cdTipoAcesso", "2") catch {};
    json.updateString("idProduto", "9") catch {};
    json.updateString("nuNegociacao", "262200000000005577") catch {};
    json.updateString("nuCliente", "SEU_NUM_CLIENTE") catch {};
    json.updateString("dtEmissaoTitulo", "21.09.2016") catch {};
    json.updateString("dtVencimentoTitulo", "21.09.2016") catch {};
    json.updateString("vlNominalTitulo", "888888") catch {};
    json.updateString("cdEspecieTitulo", "2") catch {};
    json.updateString("nomePagador", "NOME DO PAGADOR") catch {};
    json.updateString("logradouroPagador", "ENDERECO DO PAGADOR") catch {};
    json.updateString("nuLogradouroPagador", "1145") catch {};
    json.updateString("complementoLogradouroPagador", "APTO 34") catch {};
    json.updateString("cepPagador", "5588") catch {};
    json.updateString("complementoCepPagador", "1") catch {};
    json.updateString("bairroPagador", "BAIRRO PAGADOR") catch {};
    json.updateString("municipioPagador", "MUNICIPIO PAGADOR") catch {};
    json.updateString("ufPagador", "SP") catch {};
    json.updateString("cdIndCpfcnpjPagador", "2") catch {};
    json.updateString("nuCpfcnpjPagador", "12668000159") catch {};
    json.updateString("endEletronicoPagador", "PAGADOR@BRADESCO.COM.BR") catch {};
    json.updateString("nomeSacadorAvalista", "NOME SACADOR AVALISTA") catch {};
    json.updateString("logradouroSacadorAvalista", "ENDERECO SACADOR AVALISTA") catch {};
    json.updateString("nuLogradouroSacadorAvalista", "5555") catch {};
    json.updateString("complementoLogradouroSacadorAvalista", "BLOCO 23") catch {};
    json.updateString("cepSacadorAvalista", "6182") catch {};
    json.updateString("complementoCepSacadorAvalista", "160") catch {};
    json.updateString("bairroSacadorAvalista", "BAIRRO SACADOR AVALISTA") catch {};
    json.updateString("municipioSacadorAvalista", "MUNICIPIO SACADOR AVALISTA") catch {};
    json.updateString("ufSacadorAvalista", "SP") catch {};
    json.updateString("cdIndCpfcnpjSacadorAvalista", "2") catch {};
    json.updateString("nuCpfcnpjSacadorAvalista", "12668000159") catch {};
    json.updateString("endEletronicoSacadorAvalista", "SACADOR@BRADESCO.COM.BR") catch {};

    // Load out PFX file
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setSigningCert(cert) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Use SHA-256 within the signing..
    crypt.setHashAlgorithm("sha256");

    // Use no authenticated attributes
    crypt.setSigningAttributes("{}");

    json.setEmitCompact(true);
    const sig_base64 = crypt.opaqueSignStringENC(alloc, try json.emit(alloc)) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Next, we need to send a POST to the following URL where the body of the POST
    // contains the binary PKCS7 signature (which embeds the JSON).

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    // This is strange because apparently the server wants a "text/xml" Content-Type,
    // but the content in the body is not actually XML, it's binary PKCS7.
    req.setContentType("text/xml");
    req.loadBodyFromString(sig_base64, "utf-8") catch {};

    // We don't actually pass the URL, we set the path here, and the domain is passed below..
    req.setPath("/ibpjregistrotitulows/registrotitulohomologacao");

    const http = try chilkat.Http.init();
    defer http.deinit();
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("cobranca.bradesconetempresa.b.br", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response status code: {d}\n", .{resp.getStatusCode()});
    std.debug.print("Response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}