Chilkat Examples

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

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
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
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
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Generate an E-way Bill

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

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkJsonObject.h>
#include <CkCrypt2.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
#include <CkBinData.h>

void ChilkatSample(void)
    {
    CkString strOut;

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

    CkJsonObject jsonAuth;
    bool success = jsonAuth.LoadFile("qa_data/tokens/ewayAuth.json");
    if (success != true) {
        strOut.append(jsonAuth.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        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
    CkJsonObject jsonData;
    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.put_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..
    CkJsonObject jsonRequestBody;
    jsonRequestBody.UpdateString("action","GENEWAYBILL");

    //  Setup the encryptor using the decryptedSek from the jsonAuth
    CkCrypt2 crypt;
    crypt.put_CryptAlgorithm("aes");
    crypt.put_CipherMode("ecb");
    crypt.put_KeyLength(256);
    crypt.SetEncodedKey(jsonAuth.stringOf("decryptedSek"),"base64");
    crypt.put_EncodingMode("base64");

    //  Encrypt the jsonData and add it to our JSON request body
    jsonRequestBody.UpdateString("data",crypt.encryptStringENC(jsonData.emit()));

    CkHttp 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.put_Accept("application/json");

    //  POST the request to generate an e-way bill:
    CkHttpResponse *resp = http.PostJson2("http://ewb.wepgst.com/api/EWayBill","application/json",jsonRequestBody.emit());
    if (http.get_LastMethodSuccess() != true) {
        strOut.append(http.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    int respStatusCode = resp->get_StatusCode();
    strOut.append("response status code =");
    strOut.appendInt(respStatusCode);
    strOut.append("\r\n");
    strOut.append("response body:");
    strOut.append("\r\n");
    strOut.append(resp->bodyStr());
    strOut.append("\r\n");

    if (respStatusCode != 200) {
        delete resp;
        strOut.append("Failed in some unknown way.");
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        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.
    CkJsonObject json;
    json.Load(resp->bodyStr());
    delete resp;

    int status = json.IntOf("status");
    strOut.append("status = ");
    strOut.appendInt(status);
    strOut.append("\r\n");

    if (status != 1) {
        //  Failed.  Base64 decode the error
        // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        //  For an invalid password, the error is: {"errorCodes":"108"}
        CkStringBuilder sbError;
        json.StringOfSb("error",sbError);
        sbError.Decode("base64","utf-8");
        strOut.append("error: ");
        strOut.append(sbError.getAsString());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    json.put_EmitCompact(false);
    strOut.append("JSON response:");
    strOut.append("\r\n");
    strOut.append(json.emit());
    strOut.append("\r\n");

    CkBinData bdData;
    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"}

    CkJsonObject jsonBill;
    jsonBill.Load(bdData.getString("utf-8"));

    int ewayBillNo = jsonBill.IntOf("ewayBillNo");
    strOut.append("ewayBillNo = ");
    strOut.appendInt(ewayBillNo);
    strOut.append("\r\n");

    const char *ewayBillDate = jsonBill.stringOf("ewayBillDate");
    strOut.append("ewayBillDate = ");
    strOut.append(ewayBillDate);
    strOut.append("\r\n");

    const char *validUpto = jsonBill.stringOf("validUpto");
    strOut.append("validUpto = ");
    strOut.append(validUpto);
    strOut.append("\r\n");

    //  Sample output:

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


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

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