SQL Server
SQL Server
MyInvois Malaysia Submit Document
See more Malaysia MyInvois Examples
Demonstrates how to submit a document to MyInvois.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 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
-- The following JSON is sent in the request body.
-- {
-- "documents": [
-- {
-- "format": "XML",
-- "documentHash": "<SHA256_hash_of_document>",
-- "codeNumber": "INV12345",
-- "document": "<Document_encoded_in_Base64>"
-- }
-- ]
-- }
-- Load the document to be sent.
DECLARE @sbXmlDoc int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXmlDoc OUT
EXEC sp_OAMethod @sbXmlDoc, 'LoadFile', @success OUT, 'c:/invoices/invoice1.xml', 'utf-8'
-- The MyInvois online documentation neglects to tell us the encoding of the documentHash.
-- Should it be base64? Or hex? We can only guess. MyInvois provides no samples,
-- and omits these crucial details which are needed for actual implementation.
--
-- *** Note: It was confirmed that "hex" is the encoding that is needed.
DECLARE @sha256 nvarchar(4000)
EXEC sp_OAMethod @sbXmlDoc, 'GetHash', @sha256 OUT, 'sha256', 'hex', 'utf-8'
DECLARE @sbEncoded int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbEncoded OUT
EXEC sp_OAMethod @sbEncoded, 'AppendSb', @success OUT, @sbXmlDoc
EXEC sp_OAMethod @sbEncoded, 'Encode', @success OUT, 'base64', 'utf-8'
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].format', 'XML'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].documentHash', @sha256
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'documents[0].codeNumber', 'INV12345'
EXEC sp_OAMethod @json, 'UpdateSb', @success OUT, 'documents[0].document', @sbEncoded
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://preprod-api.myinvois.hasil.gov.my/api/v1.0/documentsubmissions', @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbXmlDoc
EXEC @hr = sp_OADestroy @sbEncoded
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'response status code = ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbXmlDoc
EXEC @hr = sp_OADestroy @sbEncoded
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
END
GO