Sagepay报告API错误无法验证签名价值

本文关键字:验证 报告 API 错误 Sagepay | 更新日期: 2023-09-27 18:08:45

我是。net的新手,需要一些帮助。我试图创建一个后台系统与使用sagepay管理和报告API sagepay集成。我在网上找到了一个非常有用的示例代码,但是当我试图调用API时,我得到了上述错误。下面是执行该调用的示例代码。我想知道有没有人能告诉我哪里出错了?

       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;
    }

Sagepay报告API错误无法验证签名价值

@ Mandar Jogalekar我希望这有帮助

protected class SagePayResponse
    {
        /// <summary>
        /// Raw xml response object
        /// </summary>
        private XmlDocument m_responseXml;
        /// <summary>
        /// Create a new response object from the xml string
        /// </summary>
        /// <param name="responseXml"></param>
        public SagePayResponse(string responseXml)
        {
            // create our xml doc
            m_responseXml = new XmlDocument();
            m_responseXml.LoadXml(responseXml);
            // find an error node
            XmlNode node = m_responseXml.SelectSingleNode("//vspaccess/errorcode");
            int errorCode = 0;
            if (node != null && int.TryParse(node.InnerText, out errorCode) == true && errorCode != 0)
            {
                // there was an error and we have a non-zero error code
                ErrorCode = errorCode;
            }
            // pick out any error description
            node = m_responseXml.SelectSingleNode("//vspaccess/error");
            if (node != null && node.InnerText.Length != 0)
            {
                ErrorText = node.InnerText;
            }
            // pick out the timestamp
            node = m_responseXml.SelectSingleNode("//vspaccess/timestamp");
            if (node != null && node.InnerText.Length != 0)
            {
                DateTime dt;
                if (DateTime.TryParseExact(node.InnerText, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt) == true)
                {
                    Timestamp = DateTime.SpecifyKind(dt, DateTimeKind.Utc).ToLocalTime();
                }
            }
        }

如果其他人正在寻找SagePay报告&管理API,你可以在这里找到NuGet包:

https://www.nuget.org/packages/Joe.Opayo.Admin.Api.Client/1.1.0

客户端允许这样的代码:

var client = new OpayoAdminApiClient();
var isTest = true;
var request = new OpayoApiRequest(ApiCommandType.GetTransactionDetail, "password", isTest, "user-name", "vendor-name");
var commandSpecificXml = "<vendortxcode>01Jan2010Transaction12345</vendortxcode>"; 
return await client.ProcessApiCommandAsync<TransactionDetail>(request, commandSpecificXml);