1
我對.Net非常陌生,並且需要一些幫助,以免因此而煩惱。我正在嘗試使用sagepay管理員和報告API創建與sagepay集成的後臺系統。我在網上找到了一個示例代碼,它非常有幫助,但是當我嘗試打電話給API時,出現了上述錯誤。這裏是一個示例代碼,下面是該調用。我想知道是否有人能告訴我我要去哪裏?Sagepay Reporting API錯誤無法驗證<signature>的值
private string BuildCommandString(string command, string vendor, string user, string xmldata, string password = null, string signature = null)
{
return string.Format("<command>{0}</command><vendor>{1}</vendor><user>{2}</user>{3}{4}{5}",
command,
Vendor,
User,
xmldata ?? string.Empty,
(string.IsNullOrEmpty(password) == false ? "<password>" + password + "</password>" : string.Empty),
(string.IsNullOrEmpty(signature) == false ? "<signature>" + signature + "</signature>" : string.Empty));
}
/// <summary>
/// Perform the main call for the API and collect the response
/// </summary>
/// <param name="command">api command name</param>
/// <param name="xmldata">optional extra data for api</param>
/// <returns>new SagePayResponse or null if communication error</returns>
protected SagePayResponse ProcessAPI(string command, string xmldata)
{
// get the requiest
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Url);
httpRequest.Method = "POST";
// build data
string data = BuildCommandString(command, Vendor, User, xmldata, Password);
// apply signature
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(data));
string sig = BitConverter.ToString(hash).Replace("-", string.Empty);
// rebuild with signature
data = "XML=<vspaccess>" + BuildCommandString(command, Vendor, User, xmldata, null, sig) + "</vspaccess>";
// get the data
byte[] bytes = Encoding.UTF8.GetBytes(data);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
// get the request stream
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
// call the sagepay url and get response
SagePayResponse sagePayResponse = null;
HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
//string contentType = response.ContentType;
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
try
{
//Console.WriteLine("response ok");
sagePayResponse = new SagePayResponse(reader.ReadToEnd());
}
finally
{
reader.Close();
}
}
}
finally
{
response.Close();
}
return sagePayResponse;
}
我面對類似的問題,什麼是SagePayResponse類?我在sagepay套件中找不到它。 –
我已經使用SagePayResponse類添加了一個新答案。我希望它對你有幫助 –