Sample code for 30+ languages & platforms
PowerBuilder

MyInvois Malaysia Submit Document

See more Malaysia MyInvois Examples

Demonstrates how to submit a document to MyInvois.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_SbXmlDoc
string ls_Sha256
oleobject loo_SbEncoded
oleobject loo_Json
oleobject loo_Resp

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// 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.
loo_SbXmlDoc = create oleobject
li_rc = loo_SbXmlDoc.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_SbXmlDoc.LoadFile("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.
ls_Sha256 = loo_SbXmlDoc.GetHash("sha256","hex","utf-8")

loo_SbEncoded = create oleobject
li_rc = loo_SbEncoded.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbEncoded.AppendSb(loo_SbXmlDoc)
loo_SbEncoded.Encode("base64","utf-8")

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("documents[0].format","XML")
loo_Json.UpdateString("documents[0].documentHash",ls_Sha256)
loo_Json.UpdateString("documents[0].codeNumber","INV12345")
loo_Json.UpdateSb("documents[0].document",loo_SbEncoded)

// Adds the "Authorization: Bearer <access_token>" header.
loo_Http.AuthToken = "<access_token>"

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpJson("POST","https://preprod-api.myinvois.hasil.gov.my/api/v1.0/documentsubmissions",loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbXmlDoc
    destroy loo_SbEncoded
    destroy loo_Json
    destroy loo_Resp
    return
end if

Write-Debug "response status code = " + string(loo_Resp.StatusCode)
Write-Debug loo_Resp.BodyStr


destroy loo_Http
destroy loo_SbXmlDoc
destroy loo_SbEncoded
destroy loo_Json
destroy loo_Resp