Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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

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

-- Setup the encryptor using the decryptedSek from the jsonAuth
local crypt = chilkat.Crypt2.new()
crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "ecb"
crypt.key_length = 256
crypt:set_encoded_key(jsonAuth:string_of("decryptedSek"), "base64")
crypt.encoding_mode = "base64"

-- Encrypt the jsonData and add it to our JSON request body
jsonRequestBody:update_string("data", crypt:encrypt_string_enc(jsonData:emit()))

local http = chilkat.Http.new()

-- Add the authtoken to the request header.
-- Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
http:set_request_header("authtoken", jsonAuth:string_of("authToken"))
http:set_request_header("Gstin", "09ABDC24212B1FK")
http.accept = "application/json"

-- POST the request to generate an e-way bill:
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "http://ewb.wepgst.com/api/EWayBill", jsonRequestBody, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local respStatusCode = resp.status_code
print("response status code =" .. respStatusCode)
print("response body:")
print(resp.body_str)

if respStatusCode ~= 200 then
    print("Failed in some unknown way.")
    return
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.
local json = chilkat.JsonObject.new()
json:load(resp.body_str)

local status = json:int_of("status")
print("status = " .. status)

if status ~= 1 then
    -- Failed.  Base64 decode the error
    -- {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
    -- For an invalid password, the error is: {"errorCodes":"108"}
    local sbError = chilkat.StringBuilder.new()
    json:string_of_sb("error", sbError)
    sbError:decode("base64", "utf-8")
    print("error: " .. sbError:get_as_string())
    return
end

json.emit_compact = false
print("JSON response:")
print(json:emit())

local bdData = chilkat.BinData.new()
bdData:append_encoded(json:string_of("data"), "base64")
crypt:decrypt_bd(bdData)

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

local jsonBill = chilkat.JsonObject.new()
jsonBill:load(bdData:get_string("utf-8"))

local ewayBillNo = jsonBill:int_of("ewayBillNo")
print("ewayBillNo = " .. ewayBillNo)

local ewayBillDate = jsonBill:string_of("ewayBillDate")
print("ewayBillDate = " .. ewayBillDate)

local validUpto = jsonBill:string_of("validUpto")
print("validUpto = " .. validUpto)

-- Sample output:

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