Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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",
-- }
--

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

-- Load out PFX file
local cert = chilkat.Cert.new()
local success = cert:load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123")
if not success then
    print(cert.last_error_text)
    return
end

local crypt = chilkat.Crypt2.new()
success = crypt:set_signing_cert(cert)
if not success then
    print(crypt.last_error_text)
    return
end

-- Use SHA-256 within the signing..
crypt.hash_algorithm = "sha256"

-- Use no authenticated attributes
crypt.signing_attributes = "{}"

json.emit_compact = true
local sigBase64 = crypt:opaque_sign_string_enc(json:emit())
if not crypt.last_method_success then
    print(crypt.last_error_text)
    return
end

-- 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).
local url = "https://cobranca.bradesconetempresa.b.br/ibpjregistrotitulows/registrotitulohomologacao"

local req = chilkat.HttpRequest.new()
req.http_verb = "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.content_type = "text/xml"
req:load_body_from_string(sigBase64, "utf-8")

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

local http = chilkat.Http.new()
local resp = chilkat.HttpResponse.new()
success = http:http_sreq("cobranca.bradesconetempresa.b.br", 443, true, req, resp)
if not success then
    print(http.last_error_text)
    return
end

print("Response status code: " .. resp.status_code)
print("Response body:")
print(resp.body_str)