Sample code for 30+ languages & platforms
Delphi ActiveX

Generate an E-way Bill

See more HTTP Misc Examples

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

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
jsonAuth: TChilkatJsonObject;
jsonData: TChilkatJsonObject;
jsonRequestBody: TChilkatJsonObject;
crypt: TChilkatCrypt2;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
respStatusCode: Integer;
json: TChilkatJsonObject;
status: Integer;
sbError: TChilkatStringBuilder;
bdData: TChilkatBinData;
jsonBill: TChilkatJsonObject;
ewayBillNo: Integer;
ewayBillDate: WideString;
validUpto: WideString;

begin
success := 0;

// 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 := TChilkatJsonObject.Create(Self);
success := jsonAuth.LoadFile('qa_data/tokens/ewayAuth.json');
if (success = 0) then
  begin
    Memo1.Lines.Add(jsonAuth.LastErrorText);
    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 := TChilkatJsonObject.Create(Self);
jsonData.UpdateString('supplyType','O');
jsonData.UpdateString('subSupplyType','1');
jsonData.UpdateString('docType','INV');
jsonData.UpdateString('docNo','AW1234-2');
jsonData.UpdateString('docDate','05/04/2018');
jsonData.UpdateString('fromGstin','09ABDC24212B1FK');
jsonData.UpdateString('fromTrdName','willy');
jsonData.UpdateString('fromAddr1','3RD CROSS NO 200  19  A');
jsonData.UpdateString('fromAddr2','GROUND FLOOR OZZY ROAD');
jsonData.UpdateString('fromPlace','BUSY TOWN');
jsonData.UpdateNumber('fromPincode','640033');
jsonData.UpdateNumber('actFromStateCode','05');
jsonData.UpdateNumber('fromStateCode','05');
jsonData.UpdateString('toGstin','01AAAASCC10BBBB');
jsonData.UpdateString('toTrdName','mthustra');
jsonData.UpdateString('toAddr1','Shrek Ogre');
jsonData.UpdateString('toAddr2','Basadronsil');
jsonData.UpdateString('toPlace','Grifl Blagar');
jsonData.UpdateNumber('toPincode','699988');
jsonData.UpdateNumber('actToStateCode','29');
jsonData.UpdateNumber('toStateCode','02');
jsonData.UpdateNumber('totalValue','5609889');
jsonData.UpdateNumber('cgstValue','0');
jsonData.UpdateNumber('sgstValue','0');
jsonData.UpdateNumber('igstValue','168296.67');
jsonData.UpdateNumber('cessValue','224395.56');
jsonData.UpdateString('transporterId','09ABDC24212B1FK');
jsonData.UpdateString('transporterName','');
jsonData.UpdateString('transDocNo','12332');
jsonData.UpdateNumber('transMode','1');
jsonData.UpdateString('transDistance','656');
jsonData.UpdateString('transDocDate','10/04/2018');
jsonData.UpdateString('vehicleNo','PBN4567');
jsonData.UpdateString('vehicleType','R');
jsonData.I := 0;
jsonData.UpdateString('itemList[i].productName','Wheat');
jsonData.UpdateString('itemList[i].productDesc','Wheat');
jsonData.UpdateNumber('itemList[i].hsnCode','1001');
jsonData.UpdateNumber('itemList[i].quantity','4');
jsonData.UpdateString('itemList[i].qtyUnit','BOX');
jsonData.UpdateNumber('itemList[i].cgstRate','0');
jsonData.UpdateNumber('itemList[i].sgstRate','0');
jsonData.UpdateNumber('itemList[i].igstRate','3');
jsonData.UpdateNumber('itemList[i].cessRate','4');
jsonData.UpdateNumber('itemList[i].cessAdvol','0');
jsonData.UpdateNumber('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 := TChilkatJsonObject.Create(Self);
jsonRequestBody.UpdateString('action','GENEWAYBILL');

// Setup the encryptor using the decryptedSek from the jsonAuth
crypt := TChilkatCrypt2.Create(Self);
crypt.CryptAlgorithm := 'aes';
crypt.CipherMode := 'ecb';
crypt.KeyLength := 256;
crypt.SetEncodedKey(jsonAuth.StringOf('decryptedSek'),'base64');
crypt.EncodingMode := 'base64';

// Encrypt the jsonData and add it to our JSON request body
jsonRequestBody.UpdateString('data',crypt.EncryptStringENC(jsonData.Emit()));

http := TChilkatHttp.Create(Self);

// Add the authtoken to the request header.
// Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
http.SetRequestHeader('authtoken',jsonAuth.StringOf('authToken'));
http.SetRequestHeader('Gstin','09ABDC24212B1FK');
http.Accept := 'application/json';

// POST the request to generate an e-way bill:
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpJson('POST','http://ewb.wepgst.com/api/EWayBill',jsonRequestBody.ControlInterface,'application/json',resp.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

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

if (respStatusCode <> 200) then
  begin
    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 := TChilkatJsonObject.Create(Self);
json.Load(resp.BodyStr);

status := json.IntOf('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 := TChilkatStringBuilder.Create(Self);
    json.StringOfSb('error',sbError.ControlInterface);
    sbError.Decode('base64','utf-8');
    Memo1.Lines.Add('error: ' + sbError.GetAsString());
    Exit;
  end;

json.EmitCompact := 0;
Memo1.Lines.Add('JSON response:');
Memo1.Lines.Add(json.Emit());

bdData := TChilkatBinData.Create(Self);
bdData.AppendEncoded(json.StringOf('data'),'base64');
crypt.DecryptBd(bdData.ControlInterface);

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

jsonBill := TChilkatJsonObject.Create(Self);
jsonBill.Load(bdData.GetString('utf-8'));

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

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

validUpto := jsonBill.StringOf('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
end;