Does anyone have an example on how to create a HMAC code using C#?
public static string CreateHMAC(string apiKey, string apiSecret, string token, string payload)
{
string randomNumber = "1";
string timestamp = DateTime.Now.Millisecond.ToString();
string temp = apiKey + randomNumber + timestamp + token + payload;
return getHashSha256(temp);
}
public static string getHashSha256(string text)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
SHA256Managed hashstring = new SHA256Managed();
byte[] hash = hashstring.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += String.Format("{0:x2}", x);
}
return hashString;
}
Hi Sean,
Unfortunately, we do not have an example of generating HMAC using C#. Please refer to Docs & Sandbox header section for any of our API that explains the logic and provides example code to generate HMAC in PHP, Objective-C & Python.
In your code, make sure that-
1. You are using an epoch timestamp in milliseconds
2. You Compute HMAC SHA256 hash on the data param using the API secret below.
3. As a final step, calculate the BASE64 of the hash.
Regards,
Payeezy Team
Hi Sean,
You don't have to make the Hmac key from the code. It is very simple in two three steps to get the Hmac key.
First login to your Demo Payeezy account then open the terminals Shown on your Right side of window.
Then Click one terminal Then it will open a window, then select API Access window..
Then copy Copy Key Id and paste it on API_Key section in your code
Then below is the Hidden HMAC key hidden as Password, then click "Generate New Key".
It will gives you HMAC key. Copy it, Paste it in your code and Update the form.
Hi Mobeen,
What you mentioned relates to Payeezy Gateway but Sean is looking for is a way to integrate with the REST API.
Regards,
Payeezy Team.
Hi folks,
I have an example of this (and a full integration) available here:
https://github.com/clifton-io/Clifton.Payment
Specifically, the HMAC part can be found here:
https://github.com/clifton-io/Clifton.Payment/blob/a51ba4bd3de8b162a8a29471a93386dbea28900a/Clifton.Payment/Gateway/Payeezy/PayeezyGateway.cs#L42
Thanks
Brian
The code as it stands in the gitbug project didn't work for me. Here is a modified method that has been tested and works:
private string GenerateHMAC(string apiKey, string apiSecret, string token, int nonce, string currentTimestamp, string payload)
{
string message = apiKey + nonce.ToString() + currentTimestamp + token + payload;
HMAC hmacSha256 = new HMACSHA256(Encoding.UTF8.GetBytes(apiSecret));
byte[] hmacData = hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(message));
string hex = BitConverter.ToString(hmacData);
hex = hex.Replace("-", "").ToLower();
byte[] hexArray = Encoding.UTF8.GetBytes(hex);
return Convert.ToBase64String(hexArray);
}
Hi blakeshadle5159,
Thanks for trying the code. I updated it, per your comment, and verified all the tests still pass:
https://github.com/clifton-io/Clifton.Payment/commit/d15b2fb0b05e775a78c038b54531d0aa5e075449
If the code is useful for you, please consider forking it and submitting pull requests if you discover any bugs or would like to add a feature
Brian,
Thank you for contributing.
Regards,
Payeezy team
Great Library. So easy to use. Thanks for making it.
I dont know if this is the library or payeezy stuff but I am getting a 401 access denied (Unathorized) error when I try to test out in my sandbox api a creditcard payment. I checked the keys and tokens and they are correct so not sure what I'm doing wrong.