Sample code for 30+ languages & platforms
Zig

Payeezy HMAC Computation

See more HTTP Misc Examples

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

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    const prng = try chilkat.Prng.init();
    defer prng.deinit();

    // An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
    const api_key = "my_api_key";
    // An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
    const api_secret = "my_api_secret";
    // A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
    const token = "my_merchant_token";

    // The nonce is a random number (bytes), something like "6057786719490086000"
    const nonce = try prng.genRandom(alloc, 8, "decimal");
    std.debug.print("nonce = {s}\n", .{nonce});

    const dt_now = try chilkat.DateTime.init();
    defer dt_now.deinit();
    dt_now.setFromCurrentSystemTime() catch {};
    const sb_timestamp = try chilkat.StringBuilder.init();
    defer sb_timestamp.deinit();
    // Get the epoch timestamp in seconds
    sb_timestamp.append(try dt_now.getAsUnixTimeStr(alloc, false)) catch {};
    // Change it to milliseconds
    sb_timestamp.append("000") catch {};
    // The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
    const timestamp = try sb_timestamp.getAsString(alloc);
    std.debug.print("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"
    //         }
    //       }
    //     }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("merchant_ref", "Astonishing-Sale") catch {};
    json.updateString("transaction_type", "authorize") catch {};
    json.updateString("method", "token") catch {};
    json.updateString("amount", "200") catch {};
    json.updateString("currency_code", "USD") catch {};
    json.updateString("token.token_type", "FDToken") catch {};
    json.updateString("token.token_data.type", "visa") catch {};
    json.updateString("token.token_data.value", "2537446225198291") catch {};
    json.updateString("token.token_data.cardholder_name", "JohnSmith") catch {};
    json.updateString("token.token_data.exp_date", "1030") catch {};
    json.updateString("token.token_data.special_payment", "B") catch {};

    // string hashData = apiKey + nonce + timestamp + token + jsonString;
    const sb_hmac_data = try chilkat.StringBuilder.init();
    defer sb_hmac_data.deinit();
    sb_hmac_data.append(api_key) catch {};
    sb_hmac_data.append(nonce) catch {};
    sb_hmac_data.append(timestamp) catch {};
    sb_hmac_data.append(token) catch {};
    sb_hmac_data.append(try json.emit(alloc)) catch {};

    // HMAC the data to produce a hex string.
    crypt.setEncodingMode("hexlower");
    crypt.setMacAlgorithm("hmac");
    crypt.setMacKeyString(api_secret) catch {};
    crypt.setHashAlgorithm("sha256");
    crypt.setCharset("utf-8");
    const hex_hash = try crypt.macStringENC(alloc, try sb_hmac_data.getAsString(alloc));

    // Now base64 encode the hex string:
    const sb_base64_hash = try chilkat.StringBuilder.init();
    defer sb_base64_hash.deinit();
    sb_base64_hash.append(hex_hash) catch {};
    sb_base64_hash.encode("base64", "utf-8") catch {};

    std.debug.print("This is the Authorization header to be sent with the payeezy request:\n", .{});
    std.debug.print("Authorization: {s}\n", .{try sb_base64_hash.getAsString(alloc)});
}