Sample code for 30+ languages & platforms
Zig

Adyen HMAC Signature Calculation for Hosted Payment Pages

See more Adyen Examples

Demonstrates how to do the HMAC Signature Calculation for a hosted payment page (HPP) in Adyen.

For a C# ASP.NET Razor Pages example showing the HTML Form with HMAC signature code, see Adyen HMAC Signature Calculation in C#

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 str_html = "<table class=\"od\"><tr><th>Description</th><th>Quantity</th><th>Amount</th></tr><tr><td>1 Digital Camera</td><td class=\"r\">1</td><td class=\"r\">100 GBP</td></tr><tr><td class=\"b\">Total</td><td class=\"r\"></td><td class=\"b r\">100.00 GBP</td></tr></table>";

    const gzip = try chilkat.Gzip.init();
    defer gzip.deinit();
    const gz_order_data = try gzip.compressStringENC(alloc, str_html, "utf-8", "base64");

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("keyValuePairs");

    xml.newChild2("orderData", gz_order_data);

    // required, The payment deadline; the payment needs to occur within the specified time value.
    const session_validity = "2019-08-11T10:30:00Z";
    xml.newChild2("sessionValidity", session_validity);

    // optional.  Normally we'll let Adyen automatically know the country based on the IP address.
    // By default, the payment methods offered to a shopper are filtered based on the country the shopper's IP address is mapped to.
    // In this way, shoppers are not offered payment methods that are not available in the country they are carrying out the transaction from.
    // This IP-to-country mapping is not 100% accurate, so if you have already established the country of the shopper, you can set it explicitly
    // in the countryCode parameter.
    const country_code = "GB";
    xml.newChild2("countryCode", country_code);

    // optional
    const shopper_locale = "en_GB";
    // If not specified, the locale preference is set to en_GB   by default.
    // When it is not necessary to include the country-specific part, use only the language code.
    // For example: it instead of it_IT to set the locale preferences to Italian.
    xml.newChild2("shopperLocale", shopper_locale);

    // required, A reference to uniquely identify the payment. This reference is used in all communication with you about the payment status.
    // We recommend using a unique value per payment; however, it is not a requirement. If you need to provide multiple references for a transaction,
    // you can enter them in this field. Separate each reference value with a hyphen character ("-"). This field has a length restriction:
    // you can enter max. 80 characters.
    const merchant_reference = "paymentTest1234";
    xml.newChild2("merchantReference", merchant_reference);

    // required, The merchant account identifier you want to process the (transaction) request with.
    const merchant_account = "ChilkatSoftwareIncCOM";
    xml.newChild2("merchantAccount", merchant_account);

    // required.  10000 for $100.00
    const payment_amount = "10000";
    xml.newChild2("paymentAmount", payment_amount);

    // required, The three-character ISO currency code
    const currency_code = "GBP";
    xml.newChild2("currencyCode", currency_code);

    // required.
    const skin_code = "S7uWsvfB";
    xml.newChild2("skinCode", skin_code);

    // optional, A unique identifier for the shopper, for example, a customer ID.
    // We recommend providing this information, as it is used in velocity fraud checks. It is also the key in recurring payments.
    // This field is mandatory in recurring payments.
    const shopper_reference = "somebody@example.com";
    xml.newChild2("shopperReference", shopper_reference);

    // optional
    const shopper_email = "somebody@example.com";
    xml.newChild2("shopperEmail", shopper_email);

    // optional, An integer value that adds up to the normal fraud score.
    // The value can be either a positive or negative integer.
    xml.newChild2("offset", "0");

    // Apparently this is a required field.
    const ship_before_date = "2019-06-04";
    xml.newChild2("shipBeforeDate", ship_before_date);

    xml.sortByTag(true);

    // Encode...
    //  "\" (backslash) as "\\"
    //  ":" (colon) as "\:"

    const sb_tags = try chilkat.StringBuilder.init();
    defer sb_tags.deinit();
    const sb_values = try chilkat.StringBuilder.init();
    defer sb_values.deinit();

    const sb_content = try chilkat.StringBuilder.init();
    defer sb_content.deinit();
    const n = xml.getNumChildren();
    var i: i32 = 0;
    while (i < n) {
        if (i > 0) {
            sb_tags.append(":") catch {};
            sb_values.append(":") catch {};
        }

        xml.getChild2(i) catch {};
        sb_tags.append(try xml.getTag(alloc)) catch {};

        sb_content.setString(try xml.getContent(alloc)) catch {};
        var num_replaced: i32 = sb_content.replace("\\", "\\\\");
        num_replaced = sb_content.replace(":", "\\:");
        sb_values.appendSb(sb_content) catch {};
        xml.getParent2() catch {};

        i = i + 1;
    }

    const sb_signing_str = try chilkat.StringBuilder.init();
    defer sb_signing_str.deinit();
    sb_signing_str.appendSb(sb_tags) catch {};
    sb_signing_str.append(":") catch {};
    sb_signing_str.appendSb(sb_values) catch {};

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setHashAlgorithm("sha256");
    crypt.setMacAlgorithm("hmac");

    const hmac_key = "934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739";
    crypt.setMacKeyEncoded(hmac_key, "hex") catch {};

    crypt.setEncodingMode("base64");
    const merchant_sig = try crypt.macStringENC(alloc, try sb_signing_str.getAsString(alloc));

    std.debug.print("{s}\n", .{merchant_sig});
}