Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PureBasic Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PureBasic) Generate an E-way Bill

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

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkBinData.pb"

Procedure 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

    jsonAuth.i = CkJsonObject::ckCreate()
    If jsonAuth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i = CkJsonObject::ckLoadFile(jsonAuth,"qa_data/tokens/ewayAuth.json")
    If success <> 1
        Debug CkJsonObject::ckLastErrorText(jsonAuth)
        CkJsonObject::ckDispose(jsonAuth)
        ProcedureReturn
    EndIf

    ;  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
    jsonData.i = CkJsonObject::ckCreate()
    If jsonData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonData,"supplyType","O")
    CkJsonObject::ckUpdateString(jsonData,"subSupplyType","1")
    CkJsonObject::ckUpdateString(jsonData,"docType","INV")
    CkJsonObject::ckUpdateString(jsonData,"docNo","AW1234-2")
    CkJsonObject::ckUpdateString(jsonData,"docDate","05/04/2018")
    CkJsonObject::ckUpdateString(jsonData,"fromGstin","09ABDC24212B1FK")
    CkJsonObject::ckUpdateString(jsonData,"fromTrdName","willy")
    CkJsonObject::ckUpdateString(jsonData,"fromAddr1","3RD CROSS NO 200  19  A")
    CkJsonObject::ckUpdateString(jsonData,"fromAddr2","GROUND FLOOR OZZY ROAD")
    CkJsonObject::ckUpdateString(jsonData,"fromPlace","BUSY TOWN")
    CkJsonObject::ckUpdateNumber(jsonData,"fromPincode","640033")
    CkJsonObject::ckUpdateNumber(jsonData,"actFromStateCode","05")
    CkJsonObject::ckUpdateNumber(jsonData,"fromStateCode","05")
    CkJsonObject::ckUpdateString(jsonData,"toGstin","01AAAASCC10BBBB")
    CkJsonObject::ckUpdateString(jsonData,"toTrdName","mthustra")
    CkJsonObject::ckUpdateString(jsonData,"toAddr1","Shrek Ogre")
    CkJsonObject::ckUpdateString(jsonData,"toAddr2","Basadronsil")
    CkJsonObject::ckUpdateString(jsonData,"toPlace","Grifl Blagar")
    CkJsonObject::ckUpdateNumber(jsonData,"toPincode","699988")
    CkJsonObject::ckUpdateNumber(jsonData,"actToStateCode","29")
    CkJsonObject::ckUpdateNumber(jsonData,"toStateCode","02")
    CkJsonObject::ckUpdateNumber(jsonData,"totalValue","5609889")
    CkJsonObject::ckUpdateNumber(jsonData,"cgstValue","0")
    CkJsonObject::ckUpdateNumber(jsonData,"sgstValue","0")
    CkJsonObject::ckUpdateNumber(jsonData,"igstValue","168296.67")
    CkJsonObject::ckUpdateNumber(jsonData,"cessValue","224395.56")
    CkJsonObject::ckUpdateString(jsonData,"transporterId","09ABDC24212B1FK")
    CkJsonObject::ckUpdateString(jsonData,"transporterName","")
    CkJsonObject::ckUpdateString(jsonData,"transDocNo","12332")
    CkJsonObject::ckUpdateNumber(jsonData,"transMode","1")
    CkJsonObject::ckUpdateString(jsonData,"transDistance","656")
    CkJsonObject::ckUpdateString(jsonData,"transDocDate","10/04/2018")
    CkJsonObject::ckUpdateString(jsonData,"vehicleNo","PBN4567")
    CkJsonObject::ckUpdateString(jsonData,"vehicleType","R")
    CkJsonObject::setCkI(jsonData, 0)
    CkJsonObject::ckUpdateString(jsonData,"itemList[i].productName","Wheat")
    CkJsonObject::ckUpdateString(jsonData,"itemList[i].productDesc","Wheat")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].hsnCode","1001")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].quantity","4")
    CkJsonObject::ckUpdateString(jsonData,"itemList[i].qtyUnit","BOX")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].cgstRate","0")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].sgstRate","0")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].igstRate","3")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].cessRate","4")
    CkJsonObject::ckUpdateNumber(jsonData,"itemList[i].cessAdvol","0")
    CkJsonObject::ckUpdateNumber(jsonData,"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..
    jsonRequestBody.i = CkJsonObject::ckCreate()
    If jsonRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonRequestBody,"action","GENEWAYBILL")

    ;  Setup the encryptor using the decryptedSek from the jsonAuth
    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkCryptAlgorithm(crypt, "aes")
    CkCrypt2::setCkCipherMode(crypt, "ecb")
    CkCrypt2::setCkKeyLength(crypt, 256)
    CkCrypt2::ckSetEncodedKey(crypt,CkJsonObject::ckStringOf(jsonAuth,"decryptedSek"),"base64")
    CkCrypt2::setCkEncodingMode(crypt, "base64")

    ;  Encrypt the jsonData and add it to our JSON request body
    CkJsonObject::ckUpdateString(jsonRequestBody,"data",CkCrypt2::ckEncryptStringENC(crypt,CkJsonObject::ckEmit(jsonData)))

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Add the authtoken to the request header.
    ;  Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
    CkHttp::ckSetRequestHeader(http,"authtoken",CkJsonObject::ckStringOf(jsonAuth,"authToken"))
    CkHttp::ckSetRequestHeader(http,"Gstin","09ABDC24212B1FK")
    CkHttp::setCkAccept(http, "application/json")

    ;  POST the request to generate an e-way bill:
    resp.i = CkHttp::ckPostJson2(http,"http://ewb.wepgst.com/api/EWayBill","application/json",CkJsonObject::ckEmit(jsonRequestBody))
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkJsonObject::ckDispose(jsonAuth)
        CkJsonObject::ckDispose(jsonData)
        CkJsonObject::ckDispose(jsonRequestBody)
        CkCrypt2::ckDispose(crypt)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "response status code =" + Str(respStatusCode)
    Debug "response body:"
    Debug CkHttpResponse::ckBodyStr(resp)

    If respStatusCode <> 200
        CkHttpResponse::ckDispose(resp)

        Debug "Failed in some unknown way."
        CkJsonObject::ckDispose(jsonAuth)
        CkJsonObject::ckDispose(jsonData)
        CkJsonObject::ckDispose(jsonRequestBody)
        CkCrypt2::ckDispose(crypt)
        CkHttp::ckDispose(http)
        ProcedureReturn
    EndIf

    ;  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.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    CkHttpResponse::ckDispose(resp)

    status.i = CkJsonObject::ckIntOf(json,"status")
    Debug "status = " + Str(status)

    If status <> 1
        ;  Failed.  Base64 decode the error
        ; {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        ;  For an invalid password, the error is: {"errorCodes":"108"}
        sbError.i = CkStringBuilder::ckCreate()
        If sbError.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::ckStringOfSb(json,"error",sbError)
        CkStringBuilder::ckDecode(sbError,"base64","utf-8")
        Debug "error: " + CkStringBuilder::ckGetAsString(sbError)
        CkJsonObject::ckDispose(jsonAuth)
        CkJsonObject::ckDispose(jsonData)
        CkJsonObject::ckDispose(jsonRequestBody)
        CkCrypt2::ckDispose(crypt)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbError)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug "JSON response:"
    Debug CkJsonObject::ckEmit(json)

    bdData.i = CkBinData::ckCreate()
    If bdData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkBinData::ckAppendEncoded(bdData,CkJsonObject::ckStringOf(json,"data"),"base64")
    CkCrypt2::ckDecryptBd(crypt,bdData)

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

    jsonBill.i = CkJsonObject::ckCreate()
    If jsonBill.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonBill,CkBinData::ckGetString(bdData,"utf-8"))

    ewayBillNo.i = CkJsonObject::ckIntOf(jsonBill,"ewayBillNo")
    Debug "ewayBillNo = " + Str(ewayBillNo)

    ewayBillDate.s = CkJsonObject::ckStringOf(jsonBill,"ewayBillDate")
    Debug "ewayBillDate = " + ewayBillDate

    validUpto.s = CkJsonObject::ckStringOf(jsonBill,"validUpto")
    Debug "validUpto = " + validUpto

    ;  Sample output:

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


    CkJsonObject::ckDispose(jsonAuth)
    CkJsonObject::ckDispose(jsonData)
    CkJsonObject::ckDispose(jsonRequestBody)
    CkCrypt2::ckDispose(crypt)
    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbError)
    CkBinData::ckDispose(bdData)
    CkJsonObject::ckDispose(jsonBill)


    ProcedureReturn
EndProcedure

 

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