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

Payeezy Place Temp Authorization Hold on Buyer’s Credit Card

See more HTTP Misc Examples

Demonstrates how to place a temporary authorization hold for the desired amount on the buyer’s credit card. You can Capture the authorized amount on completion of service or Void/Refund the transaction as required.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkCrypt2W.h>
#include <CkPrngW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.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": "credit_card",
    //   "amount": "1299",
    //   "currency_code": "USD",
    //   "credit_card": {
    //     "type": "visa",
    //     "cardholder_name": "John Smith",
    //     "card_number": "4788250000028291",
    //     "exp_date": "1020",
    //     "cvv": "123"
    //   }
    // }

    CkJsonObjectW json;
    json.UpdateString(L"merchant_ref",L"Astonishing-Sale");
    json.UpdateString(L"transaction_type",L"authorize");
    json.UpdateString(L"method",L"credit_card");
    json.UpdateString(L"amount",L"1299");
    json.UpdateString(L"currency_code",L"USD");
    json.UpdateString(L"credit_card.type",L"visa");
    json.UpdateString(L"credit_card.cardholder_name",L"John Smith");
    json.UpdateString(L"credit_card.card_number",L"4788250000028291");
    json.UpdateString(L"credit_card.exp_date",L"1020");
    json.UpdateString(L"credit_card.cvv",L"123");
    json.put_EmitCompact(false);
    wprintf(L"%s\n",json.emit());

    // 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());
    wprintf(L"hexHash = %s\n",hexHash);

    // 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());

    // -----------------------------------------------------------
    // Now that we have the value for the Authorization header, send the POST containing the JSON.

    CkHttpW http;

    http.put_Accept(L"*/*");
    http.put_UserAgent(L"");
    http.SetRequestHeader(L"Authorization",sbBase64Hash.getAsString());
    http.SetRequestHeader(L"apikey",apiKey);
    http.SetRequestHeader(L"nonce",nonce);
    http.SetRequestHeader(L"timestamp",timestamp);
    http.SetRequestHeader(L"token",token);

    http.put_SessionLogFilename(L"qa_output/payeezy.txt");

    const wchar_t *url = L"https://api-cert.payeezy.com/v1/transactions";
    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",url,json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"response status code = %d\n",resp.get_StatusCode());
    json.Load(resp.bodyStr());
    wprintf(L"%s\n",json.emit());

    // Sample JSON response:

    // {
    //   "correlation_id": "228.4604632998994",
    //   "transaction_status": "approved",
    //   "validation_status": "success",
    //   "transaction_type": "authorize",
    //   "transaction_id": "ET175628",
    //   "transaction_tag": "2313721985",
    //   "method": "credit_card",
    //   "amount": "1299",
    //   "currency": "USD",
    //   "cvv2": "M",
    //   "token": {
    //     "token_type": "FDToken",
    //     "token_data": {
    //       "value": "9732261336638291"
    //     }
    //   },
    //   "card": {
    //     "type": "visa",
    //     "cardholder_name": "John Smith",
    //     "card_number": "8291",
    //     "exp_date": "1020"
    //   },
    //   "bank_resp_code": "100",
    //   "bank_message": "Approved",
    //   "gateway_resp_code": "00",
    //   "gateway_message": "Transaction Normal"
    // }
    // 

    wprintf(L"%s\n",json.stringOf(L"correlation_id"));
    wprintf(L"%s\n",json.stringOf(L"transaction_status"));
    wprintf(L"%s\n",json.stringOf(L"validation_status"));
    wprintf(L"%s\n",json.stringOf(L"transaction_type"));
    wprintf(L"%s\n",json.stringOf(L"transaction_id"));
    wprintf(L"%s\n",json.stringOf(L"transaction_tag"));
    wprintf(L"%s\n",json.stringOf(L"method"));
    wprintf(L"%s\n",json.stringOf(L"amount"));
    wprintf(L"%s\n",json.stringOf(L"currency"));
    wprintf(L"%s\n",json.stringOf(L"cvv2"));
    wprintf(L"%s\n",json.stringOf(L"token.token_type"));
    wprintf(L"%s\n",json.stringOf(L"token.token_data.value"));
    wprintf(L"%s\n",json.stringOf(L"card.type"));
    wprintf(L"%s\n",json.stringOf(L"card.cardholder_name"));
    wprintf(L"%s\n",json.stringOf(L"card.card_number"));
    wprintf(L"%s\n",json.stringOf(L"card.exp_date"));
    wprintf(L"%s\n",json.stringOf(L"bank_resp_code"));
    wprintf(L"%s\n",json.stringOf(L"bank_message"));
    wprintf(L"%s\n",json.stringOf(L"gateway_resp_code"));
    wprintf(L"%s\n",json.stringOf(L"gateway_message"));
    }