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 <C_CkCrypt2W.h>
#include <C_CkPrngW.h>
#include <C_CkDateTimeW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    HCkPrngW prng;
    const wchar_t *apiKey;
    const wchar_t *apiSecret;
    const wchar_t *token;
    const wchar_t *nonce;
    HCkDateTimeW dtNow;
    HCkStringBuilderW sbTimestamp;
    const wchar_t *timestamp;
    HCkJsonObjectW json;
    HCkStringBuilderW sbHmacData;
    const wchar_t *hexHash;
    HCkStringBuilderW sbBase64Hash;
    HCkHttpW http;
    const wchar_t *url;
    HCkHttpResponseW resp;

    success = FALSE;

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

    crypt = CkCrypt2W_Create();
    prng = CkPrngW_Create();

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

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

    dtNow = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dtNow);
    sbTimestamp = CkStringBuilderW_Create();
    // Get the epoch timestamp in seconds
    CkStringBuilderW_Append(sbTimestamp,CkDateTimeW_getAsUnixTimeStr(dtNow,FALSE));
    // Change it to milliseconds
    CkStringBuilderW_Append(sbTimestamp,L"000");
    // The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
    timestamp = CkStringBuilderW_getAsString(sbTimestamp);
    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"
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"merchant_ref",L"Astonishing-Sale");
    CkJsonObjectW_UpdateString(json,L"transaction_type",L"authorize");
    CkJsonObjectW_UpdateString(json,L"method",L"credit_card");
    CkJsonObjectW_UpdateString(json,L"amount",L"1299");
    CkJsonObjectW_UpdateString(json,L"currency_code",L"USD");
    CkJsonObjectW_UpdateString(json,L"credit_card.type",L"visa");
    CkJsonObjectW_UpdateString(json,L"credit_card.cardholder_name",L"John Smith");
    CkJsonObjectW_UpdateString(json,L"credit_card.card_number",L"4788250000028291");
    CkJsonObjectW_UpdateString(json,L"credit_card.exp_date",L"1020");
    CkJsonObjectW_UpdateString(json,L"credit_card.cvv",L"123");
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // string hashData = apiKey + nonce + timestamp + token + jsonString;
    sbHmacData = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHmacData,apiKey);
    CkStringBuilderW_Append(sbHmacData,nonce);
    CkStringBuilderW_Append(sbHmacData,timestamp);
    CkStringBuilderW_Append(sbHmacData,token);
    CkStringBuilderW_Append(sbHmacData,CkJsonObjectW_emit(json));

    // HMAC the data to produce a hex string.
    CkCrypt2W_putEncodingMode(crypt,L"hexlower");
    CkCrypt2W_putMacAlgorithm(crypt,L"hmac");
    CkCrypt2W_SetMacKeyString(crypt,apiSecret);
    CkCrypt2W_putHashAlgorithm(crypt,L"sha256");
    CkCrypt2W_putCharset(crypt,L"utf-8");
    hexHash = CkCrypt2W_macStringENC(crypt,CkStringBuilderW_getAsString(sbHmacData));
    wprintf(L"hexHash = %s\n",hexHash);

    // Now base64 encode the hex string:
    sbBase64Hash = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbBase64Hash,hexHash);
    CkStringBuilderW_Encode(sbBase64Hash,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",CkStringBuilderW_getAsString(sbBase64Hash));

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

    http = CkHttpW_Create();

    CkHttpW_putAccept(http,L"*/*");
    CkHttpW_putUserAgent(http,L"");
    CkHttpW_SetRequestHeader(http,L"Authorization",CkStringBuilderW_getAsString(sbBase64Hash));
    CkHttpW_SetRequestHeader(http,L"apikey",apiKey);
    CkHttpW_SetRequestHeader(http,L"nonce",nonce);
    CkHttpW_SetRequestHeader(http,L"timestamp",timestamp);
    CkHttpW_SetRequestHeader(http,L"token",token);

    CkHttpW_putSessionLogFilename(http,L"qa_output/payeezy.txt");

    url = L"https://api-cert.payeezy.com/v1/transactions";
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",url,json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkCrypt2W_Dispose(crypt);
        CkPrngW_Dispose(prng);
        CkDateTimeW_Dispose(dtNow);
        CkStringBuilderW_Dispose(sbTimestamp);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbHmacData);
        CkStringBuilderW_Dispose(sbBase64Hash);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"response status code = %d\n",CkHttpResponseW_getStatusCode(resp));
    CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // 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",CkJsonObjectW_stringOf(json,L"correlation_id"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"transaction_status"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"validation_status"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"transaction_type"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"transaction_id"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"transaction_tag"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"method"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"amount"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"currency"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"cvv2"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"token.token_type"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"token.token_data.value"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"card.type"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"card.cardholder_name"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"card.card_number"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"card.exp_date"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"bank_resp_code"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"bank_message"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"gateway_resp_code"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"gateway_message"));


    CkCrypt2W_Dispose(crypt);
    CkPrngW_Dispose(prng);
    CkDateTimeW_Dispose(dtNow);
    CkStringBuilderW_Dispose(sbTimestamp);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbHmacData);
    CkStringBuilderW_Dispose(sbBase64Hash);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);

    }