Sample code for 30+ languages & platforms
Unicode C++

Payeezy HMAC Computation

See more HTTP Misc Examples

Demonstrates how to calculate the HMAC for a Payeezy REST request.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkCrypt2W.h>
#include <CkPrngW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkCrypt2W crypt;
    CkPrngW prng;

    // An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
    const wchar_t *apiKey = L"my_api_key";
    // An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
    const wchar_t *apiSecret = L"my_api_secret";
    // A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
    const wchar_t *token = L"my_merchant_token";

    // The nonce is a random number (bytes), something like "6057786719490086000"
    const wchar_t *nonce = prng.genRandom(8,L"decimal");
    wprintf(L"nonce = %s\n",nonce);

    CkDateTimeW dtNow;
    dtNow.SetFromCurrentSystemTime();
    CkStringBuilderW sbTimestamp;
    // Get the epoch timestamp in seconds
    sbTimestamp.Append(dtNow.getAsUnixTimeStr(false));
    // Change it to milliseconds
    sbTimestamp.Append(L"000");
    // The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
    const wchar_t *timestamp = sbTimestamp.getAsString();
    wprintf(L"timestamp = %s\n",timestamp);

    // Generate the following JSON request body:
    // 	{
    // 	  "merchant_ref": "Astonishing-Sale",
    // 	  "transaction_type": "authorize",
    // 	  "method": "token",
    // 	  "amount": "200",
    // 	  "currency_code": "USD",
    // 	  "token": {
    // 	    "token_type": "FDToken",
    // 	    "token_data": {
    // 	      "type": "visa",
    // 	      "value": "2537446225198291",
    // 	      "cardholder_name": "JohnSmith",
    // 	      "exp_date": "1030",
    // 	      "special_payment": "B"
    // 	    }
    // 	  }
    // 	}

    CkJsonObjectW json;
    json.UpdateString(L"merchant_ref",L"Astonishing-Sale");
    json.UpdateString(L"transaction_type",L"authorize");
    json.UpdateString(L"method",L"token");
    json.UpdateString(L"amount",L"200");
    json.UpdateString(L"currency_code",L"USD");
    json.UpdateString(L"token.token_type",L"FDToken");
    json.UpdateString(L"token.token_data.type",L"visa");
    json.UpdateString(L"token.token_data.value",L"2537446225198291");
    json.UpdateString(L"token.token_data.cardholder_name",L"JohnSmith");
    json.UpdateString(L"token.token_data.exp_date",L"1030");
    json.UpdateString(L"token.token_data.special_payment",L"B");

    // string hashData = apiKey + nonce + timestamp + token + jsonString;
    CkStringBuilderW sbHmacData;
    sbHmacData.Append(apiKey);
    sbHmacData.Append(nonce);
    sbHmacData.Append(timestamp);
    sbHmacData.Append(token);
    sbHmacData.Append(json.emit());

    // HMAC the data to produce a hex string.
    crypt.put_EncodingMode(L"hexlower");
    crypt.put_MacAlgorithm(L"hmac");
    crypt.SetMacKeyString(apiSecret);
    crypt.put_HashAlgorithm(L"sha256");
    crypt.put_Charset(L"utf-8");
    const wchar_t *hexHash = crypt.macStringENC(sbHmacData.getAsString());

    // Now base64 encode the hex string:
    CkStringBuilderW sbBase64Hash;
    sbBase64Hash.Append(hexHash);
    sbBase64Hash.Encode(L"base64",L"utf-8");

    wprintf(L"This is the Authorization header to be sent with the payeezy request:\n");
    wprintf(L"Authorization: %s\n",sbBase64Hash.getAsString());
    }