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

olmpazwi  于 2023-02-20  发布在  .NET
关注(0)|答案(2)|浏览(131)

我是非常非常新的。Net和需要一些帮助,所以裸请。我试图创建一个后台办公系统与整合与sagepay使用sagepay管理和报告的API。我发现了一个样本代码在线这是非常有益的,但我得到上述错误时,我试图使一个调用的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;
    }
wa7juj8i

wa7juj8i1#

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

2izufjch2#

如果其他人正在寻找有关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);

相关问题