Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

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

local strHtml = "<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>"

local gzip = chilkat.Gzip.new()
local gzOrderData = gzip:compress_string_enc(strHtml, "utf-8", "base64")

local xml = chilkat.Xml.new()
xml.tag = "keyValuePairs"

xml:new_child2("orderData", gzOrderData)

-- required, The payment deadline; the payment needs to occur within the specified time value.
local sessionValidity = "2019-08-11T10:30:00Z"
xml:new_child2("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.
local countryCode = "GB"
xml:new_child2("countryCode", countryCode)

-- optional
local shopperLocale = "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:new_child2("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.
local merchantReference = "paymentTest1234"
xml:new_child2("merchantReference", merchantReference)

-- required, The merchant account identifier you want to process the (transaction) request with.
local merchantAccount = "ChilkatSoftwareIncCOM"
xml:new_child2("merchantAccount", merchantAccount)

-- required.  10000 for $100.00
local paymentAmount = "10000"
xml:new_child2("paymentAmount", paymentAmount)

-- required, The three-character ISO currency code
local currencyCode = "GBP"
xml:new_child2("currencyCode", currencyCode)

-- required.
local skinCode = "S7uWsvfB"
xml:new_child2("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.
local shopperReference = "somebody@example.com"
xml:new_child2("shopperReference", shopperReference)

-- optional
local shopperEmail = "somebody@example.com"
xml:new_child2("shopperEmail", shopperEmail)

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

-- Apparently this is a required field.
local shipBeforeDate = "2019-06-04"
xml:new_child2("shipBeforeDate", shipBeforeDate)

xml:sort_by_tag(true)

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

local sbTags = chilkat.StringBuilder.new()
local sbValues = chilkat.StringBuilder.new()

local sbContent = chilkat.StringBuilder.new()
local n = xml.num_children
local i = 0
while i < n do
    if i > 0 then
        sbTags:append(":")
        sbValues:append(":")
    end

    xml:get_child2(i)
    sbTags:append(xml.tag)

    sbContent:set_string(xml.content)
    local numReplaced = sbContent:replace("\\", "\\\\")
    numReplaced = sbContent:replace(":", "\\:")
    sbValues:append_sb(sbContent)
    xml:get_parent2()

    i = i + 1
end

local sbSigningStr = chilkat.StringBuilder.new()
sbSigningStr:append_sb(sbTags)
sbSigningStr:append(":")
sbSigningStr:append_sb(sbValues)

local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "sha256"
crypt.mac_algorithm = "hmac"

local hmacKey = "934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739"
crypt:set_mac_key_encoded(hmacKey, "hex")

crypt.encoding_mode = "base64"
local merchantSig = crypt:mac_string_enc(sbSigningStr:get_as_string())

print(merchantSig)