Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkGzip.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    strHtml.s = "<table class=" + Chr(34) + "od" + Chr(34) + "><tr><th>Description</th><th>Quantity</th><th>Amount</th></tr><tr><td>1 Digital Camera</td><td class=" + Chr(34) + "r" + Chr(34) + ">1</td><td class=" + Chr(34) + "r" + Chr(34) + ">100 GBP</td></tr><tr><td class=" + Chr(34) + "b" + Chr(34) + ">Total</td><td class=" + Chr(34) + "r" + Chr(34) + "></td><td class=" + Chr(34) + "b r" + Chr(34) + ">100.00 GBP</td></tr></table>"

    gzip.i = CkGzip::ckCreate()
    If gzip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    gzOrderData.s = CkGzip::ckCompressStringENC(gzip,strHtml,"utf-8","base64")

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "keyValuePairs")

    CkXml::ckNewChild2(xml,"orderData",gzOrderData)

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

    ; optional
    shopperLocale.s = "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.
    CkXml::ckNewChild2(xml,"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.s = "paymentTest1234"
    CkXml::ckNewChild2(xml,"merchantReference",merchantReference)

    ; required, The merchant account identifier you want to process the (transaction) request with.
    merchantAccount.s = "ChilkatSoftwareIncCOM"
    CkXml::ckNewChild2(xml,"merchantAccount",merchantAccount)

    ; required.  10000 for $100.00
    paymentAmount.s = "10000"
    CkXml::ckNewChild2(xml,"paymentAmount",paymentAmount)

    ; required, The three-character ISO currency code
    currencyCode.s = "GBP"
    CkXml::ckNewChild2(xml,"currencyCode",currencyCode)

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

    ; optional
    shopperEmail.s = "somebody@example.com"
    CkXml::ckNewChild2(xml,"shopperEmail",shopperEmail)

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

    ; Apparently this is a required field.
    shipBeforeDate.s = "2019-06-04"
    CkXml::ckNewChild2(xml,"shipBeforeDate",shipBeforeDate)

    CkXml::ckSortByTag(xml,1)

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

    sbTags.i = CkStringBuilder::ckCreate()
    If sbTags.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbValues.i = CkStringBuilder::ckCreate()
    If sbValues.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbContent.i = CkStringBuilder::ckCreate()
    If sbContent.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    n.i = CkXml::ckNumChildren(xml)
    i.i = 0
    While i < n
        If i > 0
            CkStringBuilder::ckAppend(sbTags,":")
            CkStringBuilder::ckAppend(sbValues,":")
        EndIf

        CkXml::ckGetChild2(xml,i)
        CkStringBuilder::ckAppend(sbTags,CkXml::ckTag(xml))

        CkStringBuilder::ckSetString(sbContent,CkXml::ckContent(xml))
        numReplaced.i = CkStringBuilder::ckReplace(sbContent,"\","\\")
        numReplaced = CkStringBuilder::ckReplace(sbContent,":","\:")
        CkStringBuilder::ckAppendSb(sbValues,sbContent)
        CkXml::ckGetParent2(xml)

        i = i + 1
    Wend

    sbSigningStr.i = CkStringBuilder::ckCreate()
    If sbSigningStr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppendSb(sbSigningStr,sbTags)
    CkStringBuilder::ckAppend(sbSigningStr,":")
    CkStringBuilder::ckAppendSb(sbSigningStr,sbValues)

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
    CkCrypt2::setCkMacAlgorithm(crypt, "hmac")

    hmacKey.s = "934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739"
    CkCrypt2::ckSetMacKeyEncoded(crypt,hmacKey,"hex")

    CkCrypt2::setCkEncodingMode(crypt, "base64")
    merchantSig.s = CkCrypt2::ckMacStringENC(crypt,CkStringBuilder::ckGetAsString(sbSigningStr))

    Debug merchantSig


    CkGzip::ckDispose(gzip)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbTags)
    CkStringBuilder::ckDispose(sbValues)
    CkStringBuilder::ckDispose(sbContent)
    CkStringBuilder::ckDispose(sbSigningStr)
    CkCrypt2::ckDispose(crypt)


    ProcedureReturn
EndProcedure