Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Gzip,
  Chilkat.StringBuilder,
  Chilkat.Xml,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  strHtml: string;
  gzip: TGzip;
  gzOrderData: string;
  xml: TXml;
  sessionValidity: string;
  countryCode: string;
  shopperLocale: string;
  merchantReference: string;
  merchantAccount: string;
  paymentAmount: string;
  currencyCode: string;
  skinCode: string;
  shopperReference: string;
  shopperEmail: string;
  shipBeforeDate: string;
  sbTags: TStringBuilder;
  sbValues: TStringBuilder;
  sbContent: TStringBuilder;
  n: Integer;
  i: Integer;
  numReplaced: Integer;
  sbSigningStr: TStringBuilder;
  crypt: TCrypt2;
  hmacKey: string;
  merchantSig: string;

begin
  success := False;

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

  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>';

  gzip := TGzip.Create;
  gzOrderData := gzip.CompressStringENC(strHtml,'utf-8','base64');

  xml := TXml.Create;
  xml.Tag := 'keyValuePairs';

  xml.NewChild2('orderData',gzOrderData);

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

  //  optional
  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.NewChild2('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 := 'paymentTest1234';
  xml.NewChild2('merchantReference',merchantReference);

  //  required, The merchant account identifier you want to process the (transaction) request with.
  merchantAccount := 'ChilkatSoftwareIncCOM';
  xml.NewChild2('merchantAccount',merchantAccount);

  //  required.  10000 for $100.00
  paymentAmount := '10000';
  xml.NewChild2('paymentAmount',paymentAmount);

  //  required, The three-character ISO currency code
  currencyCode := 'GBP';
  xml.NewChild2('currencyCode',currencyCode);

  //  required.
  skinCode := 'S7uWsvfB';
  xml.NewChild2('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 := 'somebody@example.com';
  xml.NewChild2('shopperReference',shopperReference);

  //  optional
  shopperEmail := 'somebody@example.com';
  xml.NewChild2('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.NewChild2('offset','0');

  //  Apparently this is a required field.
  shipBeforeDate := '2019-06-04';
  xml.NewChild2('shipBeforeDate',shipBeforeDate);

  xml.SortByTag(True);

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

  sbTags := TStringBuilder.Create;
  sbValues := TStringBuilder.Create;

  sbContent := TStringBuilder.Create;
  n := xml.NumChildren;
  i := 0;
  while i < n do
    begin
      if (i > 0) then
        begin
          sbTags.Append(':');
          sbValues.Append(':');
        end;
      xml.GetChild2(i);
      sbTags.Append(xml.Tag);

      sbContent.SetString(xml.Content);
      numReplaced := sbContent.Replace('\",'\\\");
      numReplaced := sbContent.Replace(':','\\:');
      sbValues.AppendSb(sbContent);
      xml.GetParent2();

      i := i + 1;
    end;

  sbSigningStr := TStringBuilder.Create;
  sbSigningStr.AppendSb(sbTags);
  sbSigningStr.Append(':');
  sbSigningStr.AppendSb(sbValues);

  crypt := TCrypt2.Create;
  crypt.HashAlgorithm := 'sha256';
  crypt.MacAlgorithm := 'hmac';

  hmacKey := '934D1E806DDD99595EB430076FD7F8E4D12D0A3F51243A4C0C3897703118E739';
  crypt.SetMacKeyEncoded(hmacKey,'hex');

  crypt.EncodingMode := 'base64';
  merchantSig := crypt.MacStringENC(sbSigningStr.GetAsString());

  WriteLn(merchantSig);


  gzip.Free;
  xml.Free;
  sbTags.Free;
  sbValues.Free;
  sbContent.Free;
  sbSigningStr.Free;
  crypt.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.