Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- 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.
    EXEC sp_OASetProperty @http, 'AuthToken', 'NLOGDVXLVaF3tzmnVPkTwpkuh7dG0i09uSCcog3u+rE='
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'

    DECLARE @bdZip int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdZip OUT

    EXEC sp_OAMethod @http, 'QuickGetBd', @success OUT, 'https://ws.fatturazioneelettronica.aruba.it/services/invoice/in/getZipByFilename?filename=IT01879020517_jtlk1.xml.p7m', @bdZip
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        RETURN
      END

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'response status code = ' + @respStatusCode

    IF @respStatusCode <> 200
      BEGIN
        -- If it failed, the response body will not contain the .zip file data.
        -- It will likely contain an error message.
        EXEC sp_OAMethod @bdZip, 'GetString', @sTmp0 OUT, 'utf-8'
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        RETURN
      END

    -- Open the zip and extract the .p7m
    DECLARE @zip int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT

    EXEC sp_OAMethod @zip, 'OpenBd', @success OUT, @bdZip
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- If desired, we can unzip to the filesystem..
    DECLARE @numUnzipped int
    EXEC sp_OAMethod @zip, 'Unzip', @numUnzipped OUT, 'c:/mySignedInvoices'
    IF @numUnzipped < 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- Alternatively, we can unzip into memory..
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, 0, @entry
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    DECLARE @bdP7m int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdP7m OUT

    EXEC sp_OAMethod @entry, 'UnzipToBd', @success OUT, @bdP7m
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @entry, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @bdP7m
        RETURN
      END

    -- 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..
    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    EXEC sp_OAMethod @crypt, 'OpaqueVerifyBd', @success OUT, @bdP7m
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdZip
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @bdP7m
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END


    PRINT 'The signature was verified.'

    -- The bdp7m now contains the XML that was originally signed.

    PRINT 'Original XML:'
    EXEC sp_OAMethod @bdP7m, 'GetString', @sTmp0 OUT, 'utf-8'
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @bdZip
    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @entry
    EXEC @hr = sp_OADestroy @bdP7m
    EXEC @hr = sp_OADestroy @crypt


END
GO