Payment HMAC C# .Net 4.0 example code

It is assumed that all input texts are trimmed for leading and trailing white space.
Note: Amount always contains 2 decimals, and the decimal point is always a period (not a comma!). Remember that that the alias is a calculated value:

Payload calculation:

var inputValue = String.Format("{0}#{1}#{2}#{3}#{4}#",
                amount.ToString("0.00", CultureInfo.InvariantCulture),
var alias = MerchantId + LocationId;
//The merchant key is calculated like this:
public byte[] GetMerchantKey(string alias)
    var merchantIdSub = alias.Substring(3, 6);
    var iso = Encoding.GetEncoding("ISO-8859-1");
    var utf8 = Encoding.UTF8;
    var utfBytes = utf8.GetBytes(merchantIdSub);
    var isoBytes = Encoding.Convert(utf8, iso, utfBytes);
    var sha256Cng = new SHA256Cng();
    var merchantKey = sha256Cng.ComputeHash(isoBytes);
    return merchantKey;
//The hmac body is calculated like this:
public string CalculateHmac(string payload, byte[] merchantKey)
    var inputBytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(payload);
    //Compute hmac
    using (var hmacsha256 = new HMACSHA256(merchantKey))
        var hashmessage = hmacsha256.ComputeHash(inputBytes);
        return Convert.ToBase64String(hashmessage);