Sagepay报告API错误无法验证<signature>值

时间:2016-09-25 06:35:27

标签: c# .net api sagepay

我对.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;
    }

1 个答案:

答案 0 :(得分:1)

@ 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();
                }
            }
        }