Sample code for 30+ languages & platforms
PureBasic

Aruba Fatturazione Elettronica Get Zip by Filename

See more Aruba Fatturazione Examples

Returns an invoice with all of its notifications in Zip format (e.g. IT01879020517_abcde.xml.p7m).

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkZipEntry.pb"
IncludeFile "CkZip.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Implements the following CURL command:

    ; curl -X GET https://ws.fatturazioneelettronica.aruba.it/services/invoice/in/getZipByFilename?filename=IT01879020517_jtlk1.xml.p7m \
    ;   -H "Accept: application/json" \
    ;   -H "Authorization: Bearer NLOGDVXLVaF3tzmnVPkTwpkuh7dG0i09uSCcog3u+rE="

    ; Use the following online tool to generate HTTP code from a CURL command
    ; Convert a cURL Command to HTTP Source Code

    ; Adds the "Authorization: Bearer NLOGDVXLVaF3tzmnVPkTwpkuh7dG0i09uSCcog3u+rE=" header.
    CkHttp::setCkAuthToken(http, "NLOGDVXLVaF3tzmnVPkTwpkuh7dG0i09uSCcog3u+rE=")
    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    bdZip.i = CkBinData::ckCreate()
    If bdZip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetBd(http,"https://ws.fatturazioneelettronica.aruba.it/services/invoice/in/getZipByFilename?filename=IT01879020517_jtlk1.xml.p7m",bdZip)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "response status code = " + Str(respStatusCode)

    If respStatusCode <> 200
        ; If it failed, the response body will not contain the .zip file data.
        ; It will likely contain an error message.
        Debug CkBinData::ckGetString(bdZip,"utf-8")
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        ProcedureReturn
    EndIf

    ; Open the zip and extract the .p7m
    zip.i = CkZip::ckCreate()
    If zip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckOpenBd(zip,bdZip)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ; If desired, we can unzip to the filesystem..
    numUnzipped.i = CkZip::ckUnzip(zip,"c:/mySignedInvoices")
    If numUnzipped < 0
        Debug CkZip::ckLastErrorText(zip)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ; Alternatively, we can unzip into memory..
    entry.i = CkZipEntry::ckCreate()
    If entry.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckEntryAt(zip,0,entry)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    bdP7m.i = CkBinData::ckCreate()
    If bdP7m.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZipEntry::ckUnzipToBd(entry,bdP7m)
    If success = 0
        Debug CkZipEntry::ckLastErrorText(entry)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        CkBinData::ckDispose(bdP7m)
        ProcedureReturn
    EndIf

    ; Verify the signature and extract the XML from the p7m
    ; If the signature verification is successful, the contents of bdP7m are unwrapped and what
    ; remains is the original signed document..
    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCrypt2::ckOpaqueVerifyBd(crypt,bdP7m)
    If success = 0
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdZip)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        CkBinData::ckDispose(bdP7m)
        CkCrypt2::ckDispose(crypt)
        ProcedureReturn
    EndIf

    Debug "The signature was verified."

    ; The bdp7m now contains the XML that was originally signed.
    Debug "Original XML:"
    Debug CkBinData::ckGetString(bdP7m,"utf-8")


    CkHttp::ckDispose(http)
    CkBinData::ckDispose(bdZip)
    CkZip::ckDispose(zip)
    CkZipEntry::ckDispose(entry)
    CkBinData::ckDispose(bdP7m)
    CkCrypt2::ckDispose(crypt)


    ProcedureReturn
EndProcedure