Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Delphi DLL Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Delphi DLL) Generate an E-way Bill

Demonstrates how to send an HTTP POST request to generate an e-way bill.

Chilkat for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX DLL.

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpResponse, StringBuilder, JsonObject, Crypt2, BinData;

...

procedure TForm1.Button1Click(Sender: TObject);
var
jsonAuth: HCkJsonObject;
success: Boolean;
jsonData: HCkJsonObject;
jsonRequestBody: HCkJsonObject;
crypt: HCkCrypt2;
http: HCkHttp;
resp: HCkHttpResponse;
respStatusCode: Integer;
json: HCkJsonObject;
status: Integer;
sbError: HCkStringBuilder;
bdData: HCkBinData;
jsonBill: HCkJsonObject;
ewayBillNo: Integer;
ewayBillDate: PWideChar;
validUpto: PWideChar;

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

//  This example uses the previously obtained access token that was retrieved
//  in this example:  Get EWAY Auth Token using Gstin, username, password, and app_key

jsonAuth := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonAuth,'qa_data/tokens/ewayAuth.json');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(jsonAuth));
    Exit;
  end;

//  The jsonAuth contains something like this:
//  {
//    "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
//    "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
//  }

//  Generate the JSON data for the e-way bill.
//  The following code can be generated by pasting representative JSON into this online tool:
//  Generate JSON Code
jsonData := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonData,'supplyType','O');
CkJsonObject_UpdateString(jsonData,'subSupplyType','1');
CkJsonObject_UpdateString(jsonData,'docType','INV');
CkJsonObject_UpdateString(jsonData,'docNo','AW1234-2');
CkJsonObject_UpdateString(jsonData,'docDate','05/04/2018');
CkJsonObject_UpdateString(jsonData,'fromGstin','09ABDC24212B1FK');
CkJsonObject_UpdateString(jsonData,'fromTrdName','willy');
CkJsonObject_UpdateString(jsonData,'fromAddr1','3RD CROSS NO 200  19  A');
CkJsonObject_UpdateString(jsonData,'fromAddr2','GROUND FLOOR OZZY ROAD');
CkJsonObject_UpdateString(jsonData,'fromPlace','BUSY TOWN');
CkJsonObject_UpdateNumber(jsonData,'fromPincode','640033');
CkJsonObject_UpdateNumber(jsonData,'actFromStateCode','05');
CkJsonObject_UpdateNumber(jsonData,'fromStateCode','05');
CkJsonObject_UpdateString(jsonData,'toGstin','01AAAASCC10BBBB');
CkJsonObject_UpdateString(jsonData,'toTrdName','mthustra');
CkJsonObject_UpdateString(jsonData,'toAddr1','Shrek Ogre');
CkJsonObject_UpdateString(jsonData,'toAddr2','Basadronsil');
CkJsonObject_UpdateString(jsonData,'toPlace','Grifl Blagar');
CkJsonObject_UpdateNumber(jsonData,'toPincode','699988');
CkJsonObject_UpdateNumber(jsonData,'actToStateCode','29');
CkJsonObject_UpdateNumber(jsonData,'toStateCode','02');
CkJsonObject_UpdateNumber(jsonData,'totalValue','5609889');
CkJsonObject_UpdateNumber(jsonData,'cgstValue','0');
CkJsonObject_UpdateNumber(jsonData,'sgstValue','0');
CkJsonObject_UpdateNumber(jsonData,'igstValue','168296.67');
CkJsonObject_UpdateNumber(jsonData,'cessValue','224395.56');
CkJsonObject_UpdateString(jsonData,'transporterId','09ABDC24212B1FK');
CkJsonObject_UpdateString(jsonData,'transporterName','');
CkJsonObject_UpdateString(jsonData,'transDocNo','12332');
CkJsonObject_UpdateNumber(jsonData,'transMode','1');
CkJsonObject_UpdateString(jsonData,'transDistance','656');
CkJsonObject_UpdateString(jsonData,'transDocDate','10/04/2018');
CkJsonObject_UpdateString(jsonData,'vehicleNo','PBN4567');
CkJsonObject_UpdateString(jsonData,'vehicleType','R');
CkJsonObject_putI(jsonData,0);
CkJsonObject_UpdateString(jsonData,'itemList[i].productName','Wheat');
CkJsonObject_UpdateString(jsonData,'itemList[i].productDesc','Wheat');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].hsnCode','1001');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].quantity','4');
CkJsonObject_UpdateString(jsonData,'itemList[i].qtyUnit','BOX');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].cgstRate','0');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].sgstRate','0');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].igstRate','3');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].cessRate','4');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].cessAdvol','0');
CkJsonObject_UpdateNumber(jsonData,'itemList[i].taxableAmount','5609889');

//  The body of the HTTP POST will contain JSON that looks like this:
//  	{
//  	"action":"GENEWAYBILL",
//  	"data": " iJiJGXq ... oUZp/25Y "
//  	}

//  The "data" is the encrypted jsonData using our previously agreed-upon symmetric encryption key.
//  Let's begin build the JSON request body..
jsonRequestBody := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonRequestBody,'action','GENEWAYBILL');

//  Setup the encryptor using the decryptedSek from the jsonAuth
crypt := CkCrypt2_Create();
CkCrypt2_putCryptAlgorithm(crypt,'aes');
CkCrypt2_putCipherMode(crypt,'ecb');
CkCrypt2_putKeyLength(crypt,256);
CkCrypt2_SetEncodedKey(crypt,CkJsonObject__stringOf(jsonAuth,'decryptedSek'),'base64');
CkCrypt2_putEncodingMode(crypt,'base64');

//  Encrypt the jsonData and add it to our JSON request body
CkJsonObject_UpdateString(jsonRequestBody,'data',CkCrypt2__encryptStringENC(crypt,CkJsonObject__emit(jsonData)));

http := CkHttp_Create();

//  Add the authtoken to the request header.
//  Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
CkHttp_SetRequestHeader(http,'authtoken',CkJsonObject__stringOf(jsonAuth,'authToken'));
CkHttp_SetRequestHeader(http,'Gstin','09ABDC24212B1FK');
CkHttp_putAccept(http,'application/json');

//  POST the request to generate an e-way bill:
resp := CkHttp_PostJson2(http,'http://ewb.wepgst.com/api/EWayBill','application/json',CkJsonObject__emit(jsonRequestBody));
if (CkHttp_getLastMethodSuccess(http) <> True) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('response status code =' + IntToStr(respStatusCode));
Memo1.Lines.Add('response body:');
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));

if (respStatusCode <> 200) then
  begin
    CkHttpResponse_Dispose(resp);
    Memo1.Lines.Add('Failed in some unknown way.');
    Exit;
  end;

//  When the response status code = 200, we'll have either
//  success response like this:
//   {"status":"1","data":"..."}
// 
//  or a failed response like this:
// 
//  {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}

//  Load the response body into a JSON object.
json := CkJsonObject_Create();
CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp));
CkHttpResponse_Dispose(resp);

status := CkJsonObject_IntOf(json,'status');
Memo1.Lines.Add('status = ' + IntToStr(status));

if (status <> 1) then
  begin
    //  Failed.  Base64 decode the error
    // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
    //  For an invalid password, the error is: {"errorCodes":"108"}
    sbError := CkStringBuilder_Create();
    CkJsonObject_StringOfSb(json,'error',sbError);
    CkStringBuilder_Decode(sbError,'base64','utf-8');
    Memo1.Lines.Add('error: ' + CkStringBuilder__getAsString(sbError));
    Exit;
  end;

CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add('JSON response:');
Memo1.Lines.Add(CkJsonObject__emit(json));

bdData := CkBinData_Create();
CkBinData_AppendEncoded(bdData,CkJsonObject__stringOf(json,'data'),'base64');
CkCrypt2_DecryptBd(crypt,bdData);

//  Decrypts to
//  {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"}

jsonBill := CkJsonObject_Create();
CkJsonObject_Load(jsonBill,CkBinData__getString(bdData,'utf-8'));

ewayBillNo := CkJsonObject_IntOf(jsonBill,'ewayBillNo');
Memo1.Lines.Add('ewayBillNo = ' + IntToStr(ewayBillNo));

ewayBillDate := CkJsonObject__stringOf(jsonBill,'ewayBillDate');
Memo1.Lines.Add('ewayBillDate = ' + ewayBillDate);

validUpto := CkJsonObject__stringOf(jsonBill,'validUpto');
Memo1.Lines.Add('validUpto = ' + validUpto);

//  Sample output:

//  ewayBillNo = 331001121234
//  ewayBillDate = 24/05/2018 04:55:00 PM
//  validUpto = 31/05/2018 11:59:00 PM

CkJsonObject_Dispose(jsonAuth);
CkJsonObject_Dispose(jsonData);
CkJsonObject_Dispose(jsonRequestBody);
CkCrypt2_Dispose(crypt);
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbError);
CkBinData_Dispose(bdData);
CkJsonObject_Dispose(jsonBill);

end;

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.