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

Swift

func chilkatTest() {
    var success: Bool = false

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

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

    //  Load out PFX file 
    let cert = CkoCert()!
    success = cert.loadPfxFile(path: "qa_data/pfx/cert_test123.pfx", password: "test123")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    let crypt = CkoCrypt2()!
    success = crypt.setSigningCert(cert: cert)
    if success == false {
        print("\(crypt.lastErrorText!)")
        return
    }

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

    //  Use no authenticated attributes
    crypt.signingAttributes = "{}"

    json.emitCompact = true
    var sigBase64: String? = crypt.opaqueSignStringENC(str: json.emit())
    if crypt.lastMethodSuccess == false {
        print("\(crypt.lastErrorText!)")
        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).
    var url: String? = "https://cobranca.bradesconetempresa.b.br/ibpjregistrotitulows/registrotitulohomologacao"

    let req = CkoHttpRequest()!
    req.httpVerb = "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.contentType = "text/xml"
    req.loadBody(fromString: sigBase64, charset: "utf-8")

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

    let http = CkoHttp()!
    let resp = CkoHttpResponse()!
    success = http.httpSReq(domain: "cobranca.bradesconetempresa.b.br", port: 443, ssl: true, request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response status code: \(resp.statusCode.intValue)")
    print("Response body:")
    print("\(resp.bodyStr!)")

}