Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

MyInvois Malaysia Submit Document

See more Malaysia MyInvois Examples

Demonstrates how to submit a document to MyInvois.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  sbXmlDoc: TStringBuilder;
  sha256: string;
  sbEncoded: TStringBuilder;
  json: TJsonObject;
  resp: THttpResponse;

begin
  success := False;

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

  http := THttp.Create;

  //  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.
  sbXmlDoc := TStringBuilder.Create;
  success := 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.
  sha256 := sbXmlDoc.GetHash('sha256','hex','utf-8');

  sbEncoded := TStringBuilder.Create;
  sbEncoded.AppendSb(sbXmlDoc);
  sbEncoded.Encode('base64','utf-8');

  json := TJsonObject.Create;
  json.UpdateString('documents[0].format','XML');
  json.UpdateString('documents[0].documentHash',sha256);
  json.UpdateString('documents[0].codeNumber','INV12345');
  json.UpdateSb('documents[0].document',sbEncoded);

  //  Adds the "Authorization: Bearer <access_token>" header.
  http.AuthToken := '<access_token>';

  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://preprod-api.myinvois.hasil.gov.my/api/v1.0/documentsubmissions',json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('response status code = ' + resp.StatusCode);
  WriteLn(resp.BodyStr);


  http.Free;
  sbXmlDoc.Free;
  sbEncoded.Free;
  json.Free;
  resp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.