Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) Generate an E-way BillDemonstrates how to send an HTTP POST request to generate an e-way bill.
#import <CkoJsonObject.h> #import <CkoCrypt2.h> #import <CkoHttp.h> #import <CkoHttpResponse.h> #import <CkoStringBuilder.h> #import <CkoBinData.h> #import <NSString.h> // 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 CkoJsonObject *jsonAuth = [[CkoJsonObject alloc] init]; BOOL success = [jsonAuth LoadFile: @"qa_data/tokens/ewayAuth.json"]; if (success != YES) { NSLog(@"%@",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 CkoJsonObject *jsonData = [[CkoJsonObject alloc] init]; [jsonData UpdateString: @"supplyType" value: @"O"]; [jsonData UpdateString: @"subSupplyType" value: @"1"]; [jsonData UpdateString: @"docType" value: @"INV"]; [jsonData UpdateString: @"docNo" value: @"AW1234-2"]; [jsonData UpdateString: @"docDate" value: @"05/04/2018"]; [jsonData UpdateString: @"fromGstin" value: @"09ABDC24212B1FK"]; [jsonData UpdateString: @"fromTrdName" value: @"willy"]; [jsonData UpdateString: @"fromAddr1" value: @"3RD CROSS NO 200 19 A"]; [jsonData UpdateString: @"fromAddr2" value: @"GROUND FLOOR OZZY ROAD"]; [jsonData UpdateString: @"fromPlace" value: @"BUSY TOWN"]; [jsonData UpdateNumber: @"fromPincode" numericStr: @"640033"]; [jsonData UpdateNumber: @"actFromStateCode" numericStr: @"05"]; [jsonData UpdateNumber: @"fromStateCode" numericStr: @"05"]; [jsonData UpdateString: @"toGstin" value: @"01AAAASCC10BBBB"]; [jsonData UpdateString: @"toTrdName" value: @"mthustra"]; [jsonData UpdateString: @"toAddr1" value: @"Shrek Ogre"]; [jsonData UpdateString: @"toAddr2" value: @"Basadronsil"]; [jsonData UpdateString: @"toPlace" value: @"Grifl Blagar"]; [jsonData UpdateNumber: @"toPincode" numericStr: @"699988"]; [jsonData UpdateNumber: @"actToStateCode" numericStr: @"29"]; [jsonData UpdateNumber: @"toStateCode" numericStr: @"02"]; [jsonData UpdateNumber: @"totalValue" numericStr: @"5609889"]; [jsonData UpdateNumber: @"cgstValue" numericStr: @"0"]; [jsonData UpdateNumber: @"sgstValue" numericStr: @"0"]; [jsonData UpdateNumber: @"igstValue" numericStr: @"168296.67"]; [jsonData UpdateNumber: @"cessValue" numericStr: @"224395.56"]; [jsonData UpdateString: @"transporterId" value: @"09ABDC24212B1FK"]; [jsonData UpdateString: @"transporterName" value: @""]; [jsonData UpdateString: @"transDocNo" value: @"12332"]; [jsonData UpdateNumber: @"transMode" numericStr: @"1"]; [jsonData UpdateString: @"transDistance" value: @"656"]; [jsonData UpdateString: @"transDocDate" value: @"10/04/2018"]; [jsonData UpdateString: @"vehicleNo" value: @"PBN4567"]; [jsonData UpdateString: @"vehicleType" value: @"R"]; jsonData.I = [NSNumber numberWithInt:0]; [jsonData UpdateString: @"itemList[i].productName" value: @"Wheat"]; [jsonData UpdateString: @"itemList[i].productDesc" value: @"Wheat"]; [jsonData UpdateNumber: @"itemList[i].hsnCode" numericStr: @"1001"]; [jsonData UpdateNumber: @"itemList[i].quantity" numericStr: @"4"]; [jsonData UpdateString: @"itemList[i].qtyUnit" value: @"BOX"]; [jsonData UpdateNumber: @"itemList[i].cgstRate" numericStr: @"0"]; [jsonData UpdateNumber: @"itemList[i].sgstRate" numericStr: @"0"]; [jsonData UpdateNumber: @"itemList[i].igstRate" numericStr: @"3"]; [jsonData UpdateNumber: @"itemList[i].cessRate" numericStr: @"4"]; [jsonData UpdateNumber: @"itemList[i].cessAdvol" numericStr: @"0"]; [jsonData UpdateNumber: @"itemList[i].taxableAmount" numericStr: @"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.. CkoJsonObject *jsonRequestBody = [[CkoJsonObject alloc] init]; [jsonRequestBody UpdateString: @"action" value: @"GENEWAYBILL"]; // Setup the encryptor using the decryptedSek from the jsonAuth CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init]; crypt.CryptAlgorithm = @"aes"; crypt.CipherMode = @"ecb"; crypt.KeyLength = [NSNumber numberWithInt:256]; [crypt SetEncodedKey: [jsonAuth StringOf: @"decryptedSek"] encoding: @"base64"]; crypt.EncodingMode = @"base64"; // Encrypt the jsonData and add it to our JSON request body [jsonRequestBody UpdateString: @"data" value: [crypt EncryptStringENC: [jsonData Emit]]]; CkoHttp *http = [[CkoHttp alloc] init]; // Add the authtoken to the request header. // Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken") [http SetRequestHeader: @"authtoken" value: [jsonAuth StringOf: @"authToken"]]; [http SetRequestHeader: @"Gstin" value: @"09ABDC24212B1FK"]; http.Accept = @"application/json"; // POST the request to generate an e-way bill: CkoHttpResponse *resp = [http PostJson2: @"http://ewb.wepgst.com/api/EWayBill" contentType: @"application/json" jsonText: [jsonRequestBody Emit]]; if (http.LastMethodSuccess != YES) { NSLog(@"%@",http.LastErrorText); return; } int respStatusCode = [resp.StatusCode intValue]; NSLog(@"%@%d",@"response status code =",respStatusCode); NSLog(@"%@",@"response body:"); NSLog(@"%@",resp.BodyStr); if (respStatusCode != 200) { NSLog(@"%@",@"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. CkoJsonObject *json = [[CkoJsonObject alloc] init]; [json Load: resp.BodyStr]; int status = [[json IntOf: @"status"] intValue]; NSLog(@"%@%d",@"status = ",status); if (status != 1) { // Failed. Base64 decode the error // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="} // For an invalid password, the error is: {"errorCodes":"108"} CkoStringBuilder *sbError = [[CkoStringBuilder alloc] init]; [json StringOfSb: @"error" sb: sbError]; [sbError Decode: @"base64" charset: @"utf-8"]; NSLog(@"%@%@",@"error: ",[sbError GetAsString]); return; } json.EmitCompact = NO; NSLog(@"%@",@"JSON response:"); NSLog(@"%@",[json Emit]); CkoBinData *bdData = [[CkoBinData alloc] init]; [bdData AppendEncoded: [json StringOf: @"data"] encoding: @"base64"]; [crypt DecryptBd: bdData]; // Decrypts to // {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"} CkoJsonObject *jsonBill = [[CkoJsonObject alloc] init]; [jsonBill Load: [bdData GetString: @"utf-8"]]; int ewayBillNo = [[jsonBill IntOf: @"ewayBillNo"] intValue]; NSLog(@"%@%d",@"ewayBillNo = ",ewayBillNo); NSString *ewayBillDate = [jsonBill StringOf: @"ewayBillDate"]; NSLog(@"%@%@",@"ewayBillDate = ",ewayBillDate); NSString *validUpto = [jsonBill StringOf: @"validUpto"]; NSLog(@"%@%@",@"validUpto = ",validUpto); // Sample output: // ewayBillNo = 331001121234 // ewayBillDate = 24/05/2018 04:55:00 PM // validUpto = 31/05/2018 11:59:00 PM |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.