Creating a hash in Realex payments

1.5k Views Asked by At

I need to update the Expiry Date and update the Cardholder Name on an existing card in Realex payments.

The hash value syntax should be in the following format:

Timestamp.merchantID.payerref.ref.expirydate.cardnumber

And here is an example of how it should look

20030516175919.yourmerchantid.mypayer.card01.1015.

When I run the following method I get the error:

"sha1hash incorrect - check your code and the Developers Documentation"

private string ReturnHash(string timeStamp, string merchantId, string payerRef, string reference, string expDate, string cardNum )
{
    SHA1 hash = new SHA1Managed();
    StringBuilder builder = new StringBuilder();

    builder.Append(timeStamp).Append(".");
    builder.Append(merchantId).Append(".");
    builder.Append(payerRef).Append(".");
    builder.Append(reference).Append(".");
    builder.Append(expDate).Append(".");
    builder.Append(cardNum );

    string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
    resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));

    return resultingHash;
}

What am I doing wrong?

2

There are 2 best solutions below

0
On

Thank you for your message.

Could you try before running this line of code:

string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));

To make "resultingHash" all lowercase?

Also before running:

resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));

make "resultingHash" to lowercase as well.

Thanks, Borja

0
On
 var timeStamp = RealexDateFormatter.DateFormatForRealex();

 var orderid = model.ORDER_ID;
 var secret = ConfigurationManager.AppSettings["Appsecretkey"];
 var merchantId = ConfigurationManager.AppSettings["AppMerchantId"];

 var temp1 = FormsAuthentication.HashPasswordForStoringInConfigFile(
 timeStamp + "." +
 merchantId + "." +
 orderid + "." +
 model.AMOUNT + "." + "EUR", "sha1");
 temp1 = temp1.ToLower();
 var temp2 = temp1 + "." + secret;
 var sha1hash = FormsAuthentication.HashPasswordForStoringInConfigFile(temp2, "sha1");
 sha1hash = sha1hash.ToLower();`enter code here`
 var url = "https://hpp.sandbox.realexpayments.com/pay?MERCHANT_ID="
                + ConfigurationManager.AppSettings["AppMerchantId"] +
                "&ORDER_ID=" + orderid + "&CURRENCY=EUR" + "&AMOUNT=" + model.AMOUNT + "&TIMESTAMP=" + timeStamp + "&SHA1HASH=" + sha1hash + "&MERCHANT_RESPONSE_URL=http://deposit.projectstatus.in/Payment/Response";