Sample code for 30+ languages & platforms
Dart

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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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

  final jsonAuth = CkJsonObject();
  try {
    jsonAuth.loadFile('qa_data/tokens/ewayAuth.json');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // 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
  final jsonData = CkJsonObject();
  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..
  final jsonRequestBody = CkJsonObject();
  jsonRequestBody.updateString('action', 'GENEWAYBILL');

  // Setup the encryptor using the decryptedSek from the jsonAuth
  final crypt = CkCrypt2();
  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()));

  final http = CkHttp();

  // 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:
  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', 'http://ewb.wepgst.com/api/EWayBill', jsonRequestBody, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final respStatusCode = resp.statusCode;
  print('response status code =$respStatusCode');
  print('response body:');
  print(resp.bodyStr);

  if (respStatusCode != 200) {
    print('Failed in some unknown way.');
    return;
  }

  // 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.
  final json = CkJsonObject();
  json.load(resp.bodyStr);

  final status = json.intOf('status');
  print('status = $status');

  if (status != 1) {
    // Failed.  Base64 decode the error
    // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
    // For an invalid password, the error is: {"errorCodes":"108"}
    final sbError = CkStringBuilder();
    json.stringOfSb('error', sbError);
    sbError.decode('base64', 'utf-8');
    print('error: ${sbError.getAsString()}');
    return;
  }

  json.emitCompact = false;
  print('JSON response:');
  print(json.emit());

  final bdData = CkBinData();
  bdData.appendEncoded(json.stringOf('data'), 'base64');
  crypt.decryptBd(bdData);

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

  final jsonBill = CkJsonObject();
  jsonBill.load(bdData.getString('utf-8'));

  final ewayBillNo = jsonBill.intOf('ewayBillNo');
  print('ewayBillNo = $ewayBillNo');

  final ewayBillDate = jsonBill.stringOf('ewayBillDate');
  print('ewayBillDate = $ewayBillDate');

  final validUpto = jsonBill.stringOf('validUpto');
  print('validUpto = $validUpto');

  // Sample output:

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