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
(Swift 3,4,5...) Generate an E-way BillDemonstrates how to send an HTTP POST request to generate an e-way bill.
func chilkatTest() { // 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 let jsonAuth = CkoJsonObject()! var success: Bool = jsonAuth.loadFile("qa_data/tokens/ewayAuth.json") if success != true { print("\(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 let jsonData = CkoJsonObject()! jsonData.update("supplyType", value: "O") jsonData.update("subSupplyType", value: "1") jsonData.update("docType", value: "INV") jsonData.update("docNo", value: "AW1234-2") jsonData.update("docDate", value: "05/04/2018") jsonData.update("fromGstin", value: "09ABDC24212B1FK") jsonData.update("fromTrdName", value: "willy") jsonData.update("fromAddr1", value: "3RD CROSS NO 200 19 A") jsonData.update("fromAddr2", value: "GROUND FLOOR OZZY ROAD") jsonData.update("fromPlace", value: "BUSY TOWN") jsonData.updateNumber("fromPincode", numericStr: "640033") jsonData.updateNumber("actFromStateCode", numericStr: "05") jsonData.updateNumber("fromStateCode", numericStr: "05") jsonData.update("toGstin", value: "01AAAASCC10BBBB") jsonData.update("toTrdName", value: "mthustra") jsonData.update("toAddr1", value: "Shrek Ogre") jsonData.update("toAddr2", value: "Basadronsil") jsonData.update("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.update("transporterId", value: "09ABDC24212B1FK") jsonData.update("transporterName", value: "") jsonData.update("transDocNo", value: "12332") jsonData.updateNumber("transMode", numericStr: "1") jsonData.update("transDistance", value: "656") jsonData.update("transDocDate", value: "10/04/2018") jsonData.update("vehicleNo", value: "PBN4567") jsonData.update("vehicleType", value: "R") jsonData.i = 0 jsonData.update("itemList[i].productName", value: "Wheat") jsonData.update("itemList[i].productDesc", value: "Wheat") jsonData.updateNumber("itemList[i].hsnCode", numericStr: "1001") jsonData.updateNumber("itemList[i].quantity", numericStr: "4") jsonData.update("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.. let jsonRequestBody = CkoJsonObject()! jsonRequestBody.update("action", value: "GENEWAYBILL") // Setup the encryptor using the decryptedSek from the jsonAuth let crypt = CkoCrypt2()! crypt.cryptAlgorithm = "aes" crypt.cipherMode = "ecb" crypt.keyLength = 256 crypt.setEncodedKey(jsonAuth.string(of: "decryptedSek"), encoding: "base64") crypt.encodingMode = "base64" // Encrypt the jsonData and add it to our JSON request body jsonRequestBody.update("data", value: crypt.encryptStringENC(jsonData.emit())) let http = CkoHttp()! // Add the authtoken to the request header. // Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken") http.setRequestHeader("authtoken", value: jsonAuth.string(of: "authToken")) http.setRequestHeader("Gstin", value: "09ABDC24212B1FK") http.accept = "application/json" // POST the request to generate an e-way bill: var resp: CkoHttpResponse? = http.postJson2("http://ewb.wepgst.com/api/EWayBill", contentType: "application/json", jsonText: jsonRequestBody.emit()) if http.lastMethodSuccess != true { print("\(http.lastErrorText!)") return } var respStatusCode: Int = resp!.statusCode.intValue print("response status code =\(respStatusCode)") print("response body:") print("\(resp!.bodyStr!)") if respStatusCode != 200 { resp = nil 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. let json = CkoJsonObject()! json.load(resp!.bodyStr) resp = nil var status: Int = json.int(of: "status").intValue print("status = \(status)") if status != 1 { // Failed. Base64 decode the error // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="} // For an invalid password, the error is: {"errorCodes":"108"} let sbError = CkoStringBuilder()! json.string(ofSb: "error", sb: sbError) sbError.decode("base64", charset: "utf-8") print("error: \(sbError.getAsString()!)") return } json.emitCompact = false print("JSON response:") print("\(json.emit()!)") let bdData = CkoBinData()! bdData.appendEncoded(json.string(of: "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"} let jsonBill = CkoJsonObject()! jsonBill.load(bdData.getString("utf-8")) var ewayBillNo: Int = jsonBill.int(of: "ewayBillNo").intValue print("ewayBillNo = \(ewayBillNo)") var ewayBillDate: String? = jsonBill.string(of: "ewayBillDate") print("ewayBillDate = \(ewayBillDate!)") var validUpto: String? = 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 } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.