AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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
$oJsonAuth = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonAuth.LoadFile("qa_data/tokens/ewayAuth.json")
If ($bSuccess = False) Then
ConsoleWrite($oJsonAuth.LastErrorText & @CRLF)
Exit
EndIf
; 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
$oJsonData = ObjCreate("Chilkat.JsonObject")
$oJsonData.UpdateString("supplyType","O")
$oJsonData.UpdateString("subSupplyType","1")
$oJsonData.UpdateString("docType","INV")
$oJsonData.UpdateString("docNo","AW1234-2")
$oJsonData.UpdateString("docDate","05/04/2018")
$oJsonData.UpdateString("fromGstin","09ABDC24212B1FK")
$oJsonData.UpdateString("fromTrdName","willy")
$oJsonData.UpdateString("fromAddr1","3RD CROSS NO 200 19 A")
$oJsonData.UpdateString("fromAddr2","GROUND FLOOR OZZY ROAD")
$oJsonData.UpdateString("fromPlace","BUSY TOWN")
$oJsonData.UpdateNumber("fromPincode","640033")
$oJsonData.UpdateNumber("actFromStateCode","05")
$oJsonData.UpdateNumber("fromStateCode","05")
$oJsonData.UpdateString("toGstin","01AAAASCC10BBBB")
$oJsonData.UpdateString("toTrdName","mthustra")
$oJsonData.UpdateString("toAddr1","Shrek Ogre")
$oJsonData.UpdateString("toAddr2","Basadronsil")
$oJsonData.UpdateString("toPlace","Grifl Blagar")
$oJsonData.UpdateNumber("toPincode","699988")
$oJsonData.UpdateNumber("actToStateCode","29")
$oJsonData.UpdateNumber("toStateCode","02")
$oJsonData.UpdateNumber("totalValue","5609889")
$oJsonData.UpdateNumber("cgstValue","0")
$oJsonData.UpdateNumber("sgstValue","0")
$oJsonData.UpdateNumber("igstValue","168296.67")
$oJsonData.UpdateNumber("cessValue","224395.56")
$oJsonData.UpdateString("transporterId","09ABDC24212B1FK")
$oJsonData.UpdateString("transporterName","")
$oJsonData.UpdateString("transDocNo","12332")
$oJsonData.UpdateNumber("transMode","1")
$oJsonData.UpdateString("transDistance","656")
$oJsonData.UpdateString("transDocDate","10/04/2018")
$oJsonData.UpdateString("vehicleNo","PBN4567")
$oJsonData.UpdateString("vehicleType","R")
$oJsonData.I = 0
$oJsonData.UpdateString("itemList[i].productName","Wheat")
$oJsonData.UpdateString("itemList[i].productDesc","Wheat")
$oJsonData.UpdateNumber("itemList[i].hsnCode","1001")
$oJsonData.UpdateNumber("itemList[i].quantity","4")
$oJsonData.UpdateString("itemList[i].qtyUnit","BOX")
$oJsonData.UpdateNumber("itemList[i].cgstRate","0")
$oJsonData.UpdateNumber("itemList[i].sgstRate","0")
$oJsonData.UpdateNumber("itemList[i].igstRate","3")
$oJsonData.UpdateNumber("itemList[i].cessRate","4")
$oJsonData.UpdateNumber("itemList[i].cessAdvol","0")
$oJsonData.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..
$oJsonRequestBody = ObjCreate("Chilkat.JsonObject")
$oJsonRequestBody.UpdateString("action","GENEWAYBILL")
; Setup the encryptor using the decryptedSek from the jsonAuth
$oCrypt = ObjCreate("Chilkat.Crypt2")
$oCrypt.CryptAlgorithm = "aes"
$oCrypt.CipherMode = "ecb"
$oCrypt.KeyLength = 256
$oCrypt.SetEncodedKey $oJsonAuth.StringOf("decryptedSek"),"base64"
$oCrypt.EncodingMode = "base64"
; Encrypt the jsonData and add it to our JSON request body
$oJsonRequestBody.UpdateString("data",$oCrypt.EncryptStringENC($oJsonData.Emit()))
$oHttp = ObjCreate("Chilkat.Http")
; Add the authtoken to the request header.
; Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
$oHttp.SetRequestHeader "authtoken",$oJsonAuth.StringOf("authToken")
$oHttp.SetRequestHeader "Gstin","09ABDC24212B1FK"
$oHttp.Accept = "application/json"
; POST the request to generate an e-way bill:
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpJson("POST","http://ewb.wepgst.com/api/EWayBill",$oJsonRequestBody,"application/json",$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("response status code =" & $iRespStatusCode & @CRLF)
ConsoleWrite("response body:" & @CRLF)
ConsoleWrite($oResp.BodyStr & @CRLF)
If ($iRespStatusCode <> 200) Then
ConsoleWrite("Failed in some unknown way." & @CRLF)
Exit
EndIf
; 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.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.Load($oResp.BodyStr)
Local $iStatus = $oJson.IntOf("status")
ConsoleWrite("status = " & $iStatus & @CRLF)
If ($iStatus <> 1) Then
; Failed. Base64 decode the error
; {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
; For an invalid password, the error is: {"errorCodes":"108"}
$oSbError = ObjCreate("Chilkat.StringBuilder")
$oJson.StringOfSb("error",$oSbError)
$oSbError.Decode("base64","utf-8")
ConsoleWrite("error: " & $oSbError.GetAsString() & @CRLF)
Exit
EndIf
$oJson.EmitCompact = False
ConsoleWrite("JSON response:" & @CRLF)
ConsoleWrite($oJson.Emit() & @CRLF)
$oBdData = ObjCreate("Chilkat.BinData")
$oBdData.AppendEncoded($oJson.StringOf("data"),"base64")
$oCrypt.DecryptBd($oBdData)
; Decrypts to
; {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"}
$oJsonBill = ObjCreate("Chilkat.JsonObject")
$oJsonBill.Load($oBdData.GetString("utf-8"))
Local $iEwayBillNo = $oJsonBill.IntOf("ewayBillNo")
ConsoleWrite("ewayBillNo = " & $iEwayBillNo & @CRLF)
Local $sEwayBillDate = $oJsonBill.StringOf("ewayBillDate")
ConsoleWrite("ewayBillDate = " & $sEwayBillDate & @CRLF)
Local $sValidUpto = $oJsonBill.StringOf("validUpto")
ConsoleWrite("validUpto = " & $sValidUpto & @CRLF)
; Sample output:
; ewayBillNo = 331001121234
; ewayBillDate = 24/05/2018 04:55:00 PM
; validUpto = 31/05/2018 11:59:00 PM