React Native
React Native
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 React Native Downloads
import { BinData, Crypt2, Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// 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
const jsonAuth = new JsonObject();
try {
jsonAuth.loadFile('qa_data/tokens/ewayAuth.json');
} catch {
console.log(jsonAuth.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
const jsonData = new JsonObject();
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..
const jsonRequestBody = new JsonObject();
jsonRequestBody.updateString('action', 'GENEWAYBILL');
// Setup the encryptor using the decryptedSek from the jsonAuth
const crypt = new Crypt2();
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()));
const http = new Http();
// 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:
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', 'http://ewb.wepgst.com/api/EWayBill', jsonRequestBody, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
const respStatusCode = resp.statusCode;
console.log(`response status code =${respStatusCode}`);
console.log('response body:');
console.log(resp.bodyStr);
if (respStatusCode !== 200) {
console.log('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.
const json = new JsonObject();
json.load(resp.bodyStr);
const status = json.intOf('status');
console.log(`status = ${status}`);
if (status !== 1) {
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
const sbError = new StringBuilder();
json.stringOfSb('error', sbError);
sbError.decode('base64', 'utf-8');
console.log(`error: ${sbError.getAsString()}`);
return;
}
json.emitCompact = false;
console.log('JSON response:');
console.log(json.emit());
const bdData = new BinData();
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"}
const jsonBill = new JsonObject();
jsonBill.load(bdData.getString('utf-8'));
const ewayBillNo = jsonBill.intOf('ewayBillNo');
console.log(`ewayBillNo = ${ewayBillNo}`);
const ewayBillDate = jsonBill.stringOf('ewayBillDate');
console.log(`ewayBillDate = ${ewayBillDate}`);
const validUpto = jsonBill.stringOf('validUpto');
console.log(`validUpto = ${validUpto}`);
// Sample output:
// ewayBillNo = 331001121234
// ewayBillDate = 24/05/2018 04:55:00 PM
// validUpto = 31/05/2018 11:59:00 PM
}