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

Unicode C 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

 

 

 

(Unicode C) 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++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkJsonObjectW.h>
#include <C_CkCrypt2W.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    HCkJsonObjectW jsonAuth;
    BOOL success;
    HCkJsonObjectW jsonData;
    HCkJsonObjectW jsonRequestBody;
    HCkCrypt2W crypt;
    HCkHttpW http;
    HCkHttpResponseW resp;
    int respStatusCode;
    HCkJsonObjectW json;
    int status;
    HCkStringBuilderW sbError;
    HCkBinDataW bdData;
    HCkJsonObjectW jsonBill;
    int ewayBillNo;
    const wchar_t *ewayBillDate;
    const wchar_t *validUpto;

    //  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 = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(jsonAuth,L"qa_data/tokens/ewayAuth.json");
    if (success != TRUE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(jsonAuth));
        CkJsonObjectW_Dispose(jsonAuth);
        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
    jsonData = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonData,L"supplyType",L"O");
    CkJsonObjectW_UpdateString(jsonData,L"subSupplyType",L"1");
    CkJsonObjectW_UpdateString(jsonData,L"docType",L"INV");
    CkJsonObjectW_UpdateString(jsonData,L"docNo",L"AW1234-2");
    CkJsonObjectW_UpdateString(jsonData,L"docDate",L"05/04/2018");
    CkJsonObjectW_UpdateString(jsonData,L"fromGstin",L"09ABDC24212B1FK");
    CkJsonObjectW_UpdateString(jsonData,L"fromTrdName",L"willy");
    CkJsonObjectW_UpdateString(jsonData,L"fromAddr1",L"3RD CROSS NO 200  19  A");
    CkJsonObjectW_UpdateString(jsonData,L"fromAddr2",L"GROUND FLOOR OZZY ROAD");
    CkJsonObjectW_UpdateString(jsonData,L"fromPlace",L"BUSY TOWN");
    CkJsonObjectW_UpdateNumber(jsonData,L"fromPincode",L"640033");
    CkJsonObjectW_UpdateNumber(jsonData,L"actFromStateCode",L"05");
    CkJsonObjectW_UpdateNumber(jsonData,L"fromStateCode",L"05");
    CkJsonObjectW_UpdateString(jsonData,L"toGstin",L"01AAAASCC10BBBB");
    CkJsonObjectW_UpdateString(jsonData,L"toTrdName",L"mthustra");
    CkJsonObjectW_UpdateString(jsonData,L"toAddr1",L"Shrek Ogre");
    CkJsonObjectW_UpdateString(jsonData,L"toAddr2",L"Basadronsil");
    CkJsonObjectW_UpdateString(jsonData,L"toPlace",L"Grifl Blagar");
    CkJsonObjectW_UpdateNumber(jsonData,L"toPincode",L"699988");
    CkJsonObjectW_UpdateNumber(jsonData,L"actToStateCode",L"29");
    CkJsonObjectW_UpdateNumber(jsonData,L"toStateCode",L"02");
    CkJsonObjectW_UpdateNumber(jsonData,L"totalValue",L"5609889");
    CkJsonObjectW_UpdateNumber(jsonData,L"cgstValue",L"0");
    CkJsonObjectW_UpdateNumber(jsonData,L"sgstValue",L"0");
    CkJsonObjectW_UpdateNumber(jsonData,L"igstValue",L"168296.67");
    CkJsonObjectW_UpdateNumber(jsonData,L"cessValue",L"224395.56");
    CkJsonObjectW_UpdateString(jsonData,L"transporterId",L"09ABDC24212B1FK");
    CkJsonObjectW_UpdateString(jsonData,L"transporterName",L"");
    CkJsonObjectW_UpdateString(jsonData,L"transDocNo",L"12332");
    CkJsonObjectW_UpdateNumber(jsonData,L"transMode",L"1");
    CkJsonObjectW_UpdateString(jsonData,L"transDistance",L"656");
    CkJsonObjectW_UpdateString(jsonData,L"transDocDate",L"10/04/2018");
    CkJsonObjectW_UpdateString(jsonData,L"vehicleNo",L"PBN4567");
    CkJsonObjectW_UpdateString(jsonData,L"vehicleType",L"R");
    CkJsonObjectW_putI(jsonData,0);
    CkJsonObjectW_UpdateString(jsonData,L"itemList[i].productName",L"Wheat");
    CkJsonObjectW_UpdateString(jsonData,L"itemList[i].productDesc",L"Wheat");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].hsnCode",L"1001");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].quantity",L"4");
    CkJsonObjectW_UpdateString(jsonData,L"itemList[i].qtyUnit",L"BOX");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].cgstRate",L"0");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].sgstRate",L"0");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].igstRate",L"3");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].cessRate",L"4");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].cessAdvol",L"0");
    CkJsonObjectW_UpdateNumber(jsonData,L"itemList[i].taxableAmount",L"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 = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonRequestBody,L"action",L"GENEWAYBILL");

    //  Setup the encryptor using the decryptedSek from the jsonAuth
    crypt = CkCrypt2W_Create();
    CkCrypt2W_putCryptAlgorithm(crypt,L"aes");
    CkCrypt2W_putCipherMode(crypt,L"ecb");
    CkCrypt2W_putKeyLength(crypt,256);
    CkCrypt2W_SetEncodedKey(crypt,CkJsonObjectW_stringOf(jsonAuth,L"decryptedSek"),L"base64");
    CkCrypt2W_putEncodingMode(crypt,L"base64");

    //  Encrypt the jsonData and add it to our JSON request body
    CkJsonObjectW_UpdateString(jsonRequestBody,L"data",CkCrypt2W_encryptStringENC(crypt,CkJsonObjectW_emit(jsonData)));

    http = CkHttpW_Create();

    //  Add the authtoken to the request header.
    //  Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
    CkHttpW_SetRequestHeader(http,L"authtoken",CkJsonObjectW_stringOf(jsonAuth,L"authToken"));
    CkHttpW_SetRequestHeader(http,L"Gstin",L"09ABDC24212B1FK");
    CkHttpW_putAccept(http,L"application/json");

    //  POST the request to generate an e-way bill:
    resp = CkHttpW_PostJson2(http,L"http://ewb.wepgst.com/api/EWayBill",L"application/json",CkJsonObjectW_emit(jsonRequestBody));
    if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkJsonObjectW_Dispose(jsonAuth);
        CkJsonObjectW_Dispose(jsonData);
        CkJsonObjectW_Dispose(jsonRequestBody);
        CkCrypt2W_Dispose(crypt);
        CkHttpW_Dispose(http);
        return;
    }

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"response status code =%d\n",respStatusCode);
    wprintf(L"response body:\n");
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));

    if (respStatusCode != 200) {
        CkHttpResponseW_Dispose(resp);
        wprintf(L"Failed in some unknown way.\n");
        CkJsonObjectW_Dispose(jsonAuth);
        CkJsonObjectW_Dispose(jsonData);
        CkJsonObjectW_Dispose(jsonRequestBody);
        CkCrypt2W_Dispose(crypt);
        CkHttpW_Dispose(http);
        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.
    json = CkJsonObjectW_Create();
    CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));
    CkHttpResponseW_Dispose(resp);

    status = CkJsonObjectW_IntOf(json,L"status");
    wprintf(L"status = %d\n",status);

    if (status != 1) {
        //  Failed.  Base64 decode the error
        // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        //  For an invalid password, the error is: {"errorCodes":"108"}
        sbError = CkStringBuilderW_Create();
        CkJsonObjectW_StringOfSb(json,L"error",sbError);
        CkStringBuilderW_Decode(sbError,L"base64",L"utf-8");
        wprintf(L"error: %s\n",CkStringBuilderW_getAsString(sbError));
        CkJsonObjectW_Dispose(jsonAuth);
        CkJsonObjectW_Dispose(jsonData);
        CkJsonObjectW_Dispose(jsonRequestBody);
        CkCrypt2W_Dispose(crypt);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbError);
        return;
    }

    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"JSON response:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    bdData = CkBinDataW_Create();
    CkBinDataW_AppendEncoded(bdData,CkJsonObjectW_stringOf(json,L"data"),L"base64");
    CkCrypt2W_DecryptBd(crypt,bdData);

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

    jsonBill = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonBill,CkBinDataW_getString(bdData,L"utf-8"));

    ewayBillNo = CkJsonObjectW_IntOf(jsonBill,L"ewayBillNo");
    wprintf(L"ewayBillNo = %d\n",ewayBillNo);

    ewayBillDate = CkJsonObjectW_stringOf(jsonBill,L"ewayBillDate");
    wprintf(L"ewayBillDate = %s\n",ewayBillDate);

    validUpto = CkJsonObjectW_stringOf(jsonBill,L"validUpto");
    wprintf(L"validUpto = %s\n",validUpto);

    //  Sample output:

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


    CkJsonObjectW_Dispose(jsonAuth);
    CkJsonObjectW_Dispose(jsonData);
    CkJsonObjectW_Dispose(jsonRequestBody);
    CkCrypt2W_Dispose(crypt);
    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbError);
    CkBinDataW_Dispose(bdData);
    CkJsonObjectW_Dispose(jsonBill);

    }

 

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