Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Generate an E-way Bill

Demonstrates how to send an HTTP POST request to generate an e-way bill.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
Auth    Handle hoJsonAuth
    Boolean iSuccess
Data    Handle hoJsonData
RequestBody    Handle hoJsonRequestBody
    Handle hoCrypt
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Integer iRespStatusCode
    Handle hoJson
    Integer iStatus
    Variant vSbError
    Handle hoSbError
    Variant vBdData
    Handle hoBdData
Bill    Handle hoJsonBill
    Integer iEwayBillNo
    String sEwayBillDate
    String sValidUpto
    String sTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAuth
    If (Not(IsComObjectCreated(hoJsonAuth))) Begin
        Send CreateComObject of hoJsonAuth
    End
    Get ComLoadFile Of hoJsonAuth "qa_data/tokens/ewayAuth.json" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJsonAuth To sTemp1
        Showln sTemp1
        Procedure_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
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonData
    If (Not(IsComObjectCreated(hoJsonData))) Begin
        Send CreateComObject of hoJsonData
    End
    Get ComUpdateString Of hoJsonData "supplyType" "O" To iSuccess
    Get ComUpdateString Of hoJsonData "subSupplyType" "1" To iSuccess
    Get ComUpdateString Of hoJsonData "docType" "INV" To iSuccess
    Get ComUpdateString Of hoJsonData "docNo" "AW1234-2" To iSuccess
    Get ComUpdateString Of hoJsonData "docDate" "05/04/2018" To iSuccess
    Get ComUpdateString Of hoJsonData "fromGstin" "09ABDC24212B1FK" To iSuccess
    Get ComUpdateString Of hoJsonData "fromTrdName" "willy" To iSuccess
    Get ComUpdateString Of hoJsonData "fromAddr1" "3RD CROSS NO 200  19  A" To iSuccess
    Get ComUpdateString Of hoJsonData "fromAddr2" "GROUND FLOOR OZZY ROAD" To iSuccess
    Get ComUpdateString Of hoJsonData "fromPlace" "BUSY TOWN" To iSuccess
    Get ComUpdateNumber Of hoJsonData "fromPincode" "640033" To iSuccess
    Get ComUpdateNumber Of hoJsonData "actFromStateCode" "05" To iSuccess
    Get ComUpdateNumber Of hoJsonData "fromStateCode" "05" To iSuccess
    Get ComUpdateString Of hoJsonData "toGstin" "01AAAASCC10BBBB" To iSuccess
    Get ComUpdateString Of hoJsonData "toTrdName" "mthustra" To iSuccess
    Get ComUpdateString Of hoJsonData "toAddr1" "Shrek Ogre" To iSuccess
    Get ComUpdateString Of hoJsonData "toAddr2" "Basadronsil" To iSuccess
    Get ComUpdateString Of hoJsonData "toPlace" "Grifl Blagar" To iSuccess
    Get ComUpdateNumber Of hoJsonData "toPincode" "699988" To iSuccess
    Get ComUpdateNumber Of hoJsonData "actToStateCode" "29" To iSuccess
    Get ComUpdateNumber Of hoJsonData "toStateCode" "02" To iSuccess
    Get ComUpdateNumber Of hoJsonData "totalValue" "5609889" To iSuccess
    Get ComUpdateNumber Of hoJsonData "cgstValue" "0" To iSuccess
    Get ComUpdateNumber Of hoJsonData "sgstValue" "0" To iSuccess
    Get ComUpdateNumber Of hoJsonData "igstValue" "168296.67" To iSuccess
    Get ComUpdateNumber Of hoJsonData "cessValue" "224395.56" To iSuccess
    Get ComUpdateString Of hoJsonData "transporterId" "09ABDC24212B1FK" To iSuccess
    Get ComUpdateString Of hoJsonData "transporterName" "" To iSuccess
    Get ComUpdateString Of hoJsonData "transDocNo" "12332" To iSuccess
    Get ComUpdateNumber Of hoJsonData "transMode" "1" To iSuccess
    Get ComUpdateString Of hoJsonData "transDistance" "656" To iSuccess
    Get ComUpdateString Of hoJsonData "transDocDate" "10/04/2018" To iSuccess
    Get ComUpdateString Of hoJsonData "vehicleNo" "PBN4567" To iSuccess
    Get ComUpdateString Of hoJsonData "vehicleType" "R" To iSuccess
    Set ComI Of hoJsonData To 0
    Get ComUpdateString Of hoJsonData "itemList[i].productName" "Wheat" To iSuccess
    Get ComUpdateString Of hoJsonData "itemList[i].productDesc" "Wheat" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].hsnCode" "1001" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].quantity" "4" To iSuccess
    Get ComUpdateString Of hoJsonData "itemList[i].qtyUnit" "BOX" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].cgstRate" "0" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].sgstRate" "0" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].igstRate" "3" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].cessRate" "4" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].cessAdvol" "0" To iSuccess
    Get ComUpdateNumber Of hoJsonData "itemList[i].taxableAmount" "5609889" To iSuccess

    // 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..
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRequestBody
    If (Not(IsComObjectCreated(hoJsonRequestBody))) Begin
        Send CreateComObject of hoJsonRequestBody
    End
    Get ComUpdateString Of hoJsonRequestBody "action" "GENEWAYBILL" To iSuccess

    // Setup the encryptor using the decryptedSek from the jsonAuth
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComCryptAlgorithm Of hoCrypt To "aes"
    Set ComCipherMode Of hoCrypt To "ecb"
    Set ComKeyLength Of hoCrypt To 256
    Get ComStringOf Of hoJsonAuth "decryptedSek" To sTemp1
    Send ComSetEncodedKey To hoCrypt sTemp1 "base64"
    Set ComEncodingMode Of hoCrypt To "base64"

    // Encrypt the jsonData and add it to our JSON request body
    Get ComEncryptStringENC Of hoCrypt (ComEmit(hoJsonData)) To sTemp1
    Get ComUpdateString Of hoJsonRequestBody "data" sTemp1 To iSuccess

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Add the authtoken to the request header.
    // Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
    Get ComStringOf Of hoJsonAuth "authToken" To sTemp1
    Send ComSetRequestHeader To hoHttp "authtoken" sTemp1
    Send ComSetRequestHeader To hoHttp "Gstin" "09ABDC24212B1FK"
    Set ComAccept Of hoHttp To "application/json"

    // POST the request to generate an e-way bill:
    Get ComEmit Of hoJsonRequestBody To sTemp1
    Get ComPostJson2 Of hoHttp "http://ewb.wepgst.com/api/EWayBill" "application/json" sTemp1 To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "response status code =" iRespStatusCode
    Showln "response body:"
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    If (iRespStatusCode <> 200) Begin
        Send Destroy of hoResp
        Showln "Failed in some unknown way."
        Procedure_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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJson sTemp1 To iSuccess
    Send Destroy of hoResp

    Get ComIntOf Of hoJson "status" To iStatus
    Showln "status = " iStatus

    If (iStatus <> 1) Begin
        // Failed.  Base64 decode the error
        // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        // For an invalid password, the error is: {"errorCodes":"108"}
        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbError
        If (Not(IsComObjectCreated(hoSbError))) Begin
            Send CreateComObject of hoSbError
        End
        Get pvComObject of hoSbError to vSbError
        Get ComStringOfSb Of hoJson "error" vSbError To iSuccess
        Get ComDecode Of hoSbError "base64" "utf-8" To iSuccess
        Get ComGetAsString Of hoSbError To sTemp1
        Showln "error: " sTemp1
        Procedure_Return
    End

    Set ComEmitCompact Of hoJson To False
    Showln "JSON response:"
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    Get Create (RefClass(cComChilkatBinData)) To hoBdData
    If (Not(IsComObjectCreated(hoBdData))) Begin
        Send CreateComObject of hoBdData
    End
    Get ComStringOf Of hoJson "data" To sTemp1
    Get ComAppendEncoded Of hoBdData sTemp1 "base64" To iSuccess
    Get pvComObject of hoBdData to vBdData
    Get ComDecryptBd Of hoCrypt vBdData To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonBill
    If (Not(IsComObjectCreated(hoJsonBill))) Begin
        Send CreateComObject of hoJsonBill
    End
    Get ComGetString Of hoBdData "utf-8" To sTemp1
    Get ComLoad Of hoJsonBill sTemp1 To iSuccess

    Get ComIntOf Of hoJsonBill "ewayBillNo" To iEwayBillNo
    Showln "ewayBillNo = " iEwayBillNo

    Get ComStringOf Of hoJsonBill "ewayBillDate" To sEwayBillDate
    Showln "ewayBillDate = " sEwayBillDate

    Get ComStringOf Of hoJsonBill "validUpto" To sValidUpto
    Showln "validUpto = " sValidUpto

    // Sample output:

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


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.