Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkGzipW.h>
#include <C_CkXmlW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkCrypt2W.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *strHtml;
    HCkGzipW gzip;
    const wchar_t *gzOrderData;
    HCkXmlW xml;
    const wchar_t *sessionValidity;
    const wchar_t *countryCode;
    const wchar_t *shopperLocale;
    const wchar_t *merchantReference;
    const wchar_t *merchantAccount;
    const wchar_t *paymentAmount;
    const wchar_t *currencyCode;
    const wchar_t *skinCode;
    const wchar_t *shopperReference;
    const wchar_t *shopperEmail;
    const wchar_t *shipBeforeDate;
    HCkStringBuilderW sbTags;
    HCkStringBuilderW sbValues;
    HCkStringBuilderW sbContent;
    int n;
    int i;
    int numReplaced;
    HCkStringBuilderW sbSigningStr;
    HCkCrypt2W crypt;
    const wchar_t *hmacKey;
    const wchar_t *merchantSig;

    success = FALSE;

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

    strHtml = L"<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>";

    gzip = CkGzipW_Create();
    gzOrderData = CkGzipW_compressStringENC(gzip,strHtml,L"utf-8",L"base64");

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"keyValuePairs");

    CkXmlW_NewChild2(xml,L"orderData",gzOrderData);

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

    // 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.
    countryCode = L"GB";
    CkXmlW_NewChild2(xml,L"countryCode",countryCode);

    // optional
    shopperLocale = L"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.
    CkXmlW_NewChild2(xml,L"shopperLocale",shopperLocale);

    // 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.
    merchantReference = L"paymentTest1234";
    CkXmlW_NewChild2(xml,L"merchantReference",merchantReference);

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

    // required.  10000 for $100.00
    paymentAmount = L"10000";
    CkXmlW_NewChild2(xml,L"paymentAmount",paymentAmount);

    // required, The three-character ISO currency code
    currencyCode = L"GBP";
    CkXmlW_NewChild2(xml,L"currencyCode",currencyCode);

    // required.
    skinCode = L"S7uWsvfB";
    CkXmlW_NewChild2(xml,L"skinCode",skinCode);

    // 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.  
    shopperReference = L"somebody@example.com";
    CkXmlW_NewChild2(xml,L"shopperReference",shopperReference);

    // optional
    shopperEmail = L"somebody@example.com";
    CkXmlW_NewChild2(xml,L"shopperEmail",shopperEmail);

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

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

    CkXmlW_SortByTag(xml,TRUE);

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

    sbTags = CkStringBuilderW_Create();
    sbValues = CkStringBuilderW_Create();

    sbContent = CkStringBuilderW_Create();
    n = CkXmlW_getNumChildren(xml);
    i = 0;
    while (i < n) {
        if (i > 0) {
            CkStringBuilderW_Append(sbTags,L":");
            CkStringBuilderW_Append(sbValues,L":");
        }

        CkXmlW_GetChild2(xml,i);
        CkStringBuilderW_Append(sbTags,CkXmlW_tag(xml));

        CkStringBuilderW_SetString(sbContent,CkXmlW_content(xml));
        numReplaced = CkStringBuilderW_Replace(sbContent,L"\\",L"\\\\");
        numReplaced = CkStringBuilderW_Replace(sbContent,L":",L"\\:");
        CkStringBuilderW_AppendSb(sbValues,sbContent);
        CkXmlW_GetParent2(xml);

        i = i + 1;
    }

    sbSigningStr = CkStringBuilderW_Create();
    CkStringBuilderW_AppendSb(sbSigningStr,sbTags);
    CkStringBuilderW_Append(sbSigningStr,L":");
    CkStringBuilderW_AppendSb(sbSigningStr,sbValues);

    crypt = CkCrypt2W_Create();
    CkCrypt2W_putHashAlgorithm(crypt,L"sha256");
    CkCrypt2W_putMacAlgorithm(crypt,L"hmac");

    hmacKey = L"934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739";
    CkCrypt2W_SetMacKeyEncoded(crypt,hmacKey,L"hex");

    CkCrypt2W_putEncodingMode(crypt,L"base64");
    merchantSig = CkCrypt2W_macStringENC(crypt,CkStringBuilderW_getAsString(sbSigningStr));

    wprintf(L"%s\n",merchantSig);


    CkGzipW_Dispose(gzip);
    CkXmlW_Dispose(xml);
    CkStringBuilderW_Dispose(sbTags);
    CkStringBuilderW_Dispose(sbValues);
    CkStringBuilderW_Dispose(sbContent);
    CkStringBuilderW_Dispose(sbSigningStr);
    CkCrypt2W_Dispose(crypt);

    }