I am currently trying to Ping the Payfast API to get basic authentication rate.
I am stuck at getting the signature correct I think.
To generate the signature I am
Sorting all variables in alphabetical order as seen here and URLencoding
string signature = HttpUtility.UrlEncode($"merchant-id={merchantID}&passphrase={passphrase}×tamp={timestamp}&version={version}");
I am then generating the MD5 hash string as seen here
using (var md5Hash = MD5.Create())
{
// Byte array representation of source string
var sourceBytes = Encoding.UTF8.GetBytes(signature);
// Generate hash value(Byte Array) for input data
var hashBytes = md5Hash.ComputeHash(sourceBytes);
// Convert hash byte array to string
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
// Output the MD5 hash
Console.WriteLine(signature + " is: " + hash);
string hashlower = hash.ToLower()
}
I then add the signature to the header
request.AddHeader("merchant-id", merchantID);
request.AddHeader("version", version);
request.AddHeader("signature", hashlower);
request.AddHeader("timestamp", timestamp);
This seems correct when viewing an example in their postman collection https://documenter.getpostman.com/view/10608852/TVCmSQZu
Could anyone pick anything up that I am doing wrong in the signature generation
ull code
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss", CultureInfo.InvariantCulture);
string signature = HttpUtility.UrlEncode($"merchant-id={merchantID}&passphrase={passphrase}×tamp={timestamp}&version={version}");
using (var md5Hash = MD5.Create())
{
// Byte array representation of source string
var sourceBytes = Encoding.UTF8.GetBytes(signature);
// Generate hash value(Byte Array) for input data
var hashBytes = md5Hash.ComputeHash(sourceBytes);
// Convert hash byte array to string
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
// Output the MD5 hash
Console.WriteLine(signature + " is: " + hash);
string hashlower = hash.ToLower();
var client = new RestClient("https://api.payfast.co.za/ping");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("merchant-id", merchantID);
request.AddHeader("version", version);
request.AddHeader("signature", hashlower);
request.AddHeader("timestamp", timestamp);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}