Exemple #1
0
        private JdPayDictionary GetEncryptDicContent <T>(IJdPayRequest <T> request, IDictionary <string, string> dic) where T : JdPayResponse
        {
            var key     = Convert.FromBase64String(Options.DesKey);
            var signDic = new JdPayDictionary(dic)
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
            };

            var sign = JdPaySignature.RSASign(signDic, RSAPrivateParameters);

            var encyptDic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { SIGN, sign }
            };

            foreach (var item in dic)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    encyptDic.Add(item.Key, DES3.EncryptECB(key, item.Value));
                }
            }
            return(encyptDic);
        }
Exemple #2
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JdPayNotifyResponse
        {
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                var key     = Convert.FromBase64String(Options.DesKey);
                var signDic = new JdPayDictionary();
                foreach (var item in request.Form)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                    {
                        if (item.Key == "sign")
                        {
                            signDic.Add(item.Key, item.Value);
                        }
                        else
                        {
                            signDic.Add(item.Key, DES3.DecryptECB(key, item.Value));
                        }
                    }
                }

                var parser = new JdPayDictionaryParser <T>();
                var rsp    = parser.Parse(signDic);

                CheckNotifySign(signDic, RSAPrivateParameters);
                return(rsp);
            }
            else
            {
                var body = await new StreamReader(request.Body).ReadToEndAsync();
                var rsp  = DecryptResponseXml <T>(JdPayUtil.FotmatXmlString(body));
                return(rsp);
            }
        }
Exemple #3
0
        public async Task <T> ExecuteAsync <T>(IJdPayRequest <T> request) where T : JdPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JdPayDictionary(request.GetParameters());
            var xml             = BuildEncryptXml(request, sortedTxtParams);
            var body            = await Client.DoPostAsync(request.GetRequestUrl(), xml);

            return(DecryptResponseXml <T>(JdPayUtil.FotmatXmlString(body)));
        }
Exemple #4
0
        public Task <T> PageExecuteAsync <T>(IJdPayRequest <T> request, string reqMethod) where T : JdPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JdPayDictionary(request.GetParameters());
            var encyptParams    = GetEncryptDicContent(request, sortedTxtParams);
            var rsp             = Activator.CreateInstance <T>();

            rsp.Body = GetHtmlRequestContent(request.GetRequestUrl(), encyptParams, reqMethod);
            return(Task.FromResult(rsp));
        }
Exemple #5
0
        private string GetHtmlRequestContent(string url, JdPayDictionary dicPara, string strMethod)
        {
            var sbHtml = new StringBuilder();

            sbHtml.Append("<form id='submit' name='submit' action='" + url + "' method='" + strMethod + "' style='display:none;'>");
            foreach (var temp in dicPara)
            {
                sbHtml.Append("<input  name='" + temp.Key + "' value='" + temp.Value + "'/>");
            }
            sbHtml.Append("<input type='submit' style='display:none;'></form>");
            sbHtml.Append("<script>document.forms['submit'].submit();</script>");
            return(sbHtml.ToString());
        }
Exemple #6
0
        public async Task <T> ExecuteAsync <T>(IJdPayRequest <T> request) where T : JdPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JdPayDictionary(request.GetParameters());

            var reqContent = GetEncryptXmlContent(request, sortedTxtParams);

            var rspContent = await Client.DoPostAsync(request.GetRequestUrl(), reqContent);

            if (string.IsNullOrEmpty(rspContent))
            {
                throw new Exception("rspContent is Empty!");
            }

            var formatStr = JdPayUtil.FotmatXmlString(rspContent);

            return(DecryptResponseXmlContent <T>(formatStr));
        }
Exemple #7
0
        private void CheckNotifySign(JdPayDictionary content, object parameters)
        {
            if (content.Count == 0)
            {
                throw new Exception("sign check fail: Body is Empty!");
            }

            var sign = content["sign"];

            if (string.IsNullOrEmpty(sign))
            {
                throw new Exception("sign check fail: sign is Empty!");
            }

            if (!JdPaySignature.RSACheckContent(content, sign, RSAPublicParameters))
            {
                throw new Exception("sign check fail: check Sign and Data Fail");
            }
        }
Exemple #8
0
        private string GetEncryptXmlContent <T>(IJdPayRequest <T> request, JdPayDictionary dic) where T : JdPayResponse
        {
            var xmldoc = JdPayUtil.SortedDictionary2AllXml(dic);
            var smlStr = JdPayUtil.ConvertXmlToString(xmldoc);
            var sha256SourceSignString = SHA256.Compute(smlStr);
            var encyptBytes            = JdPaySignature.Encrypt(sha256SourceSignString, RSAPrivateParameters);
            var sign    = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks);
            var data    = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>");
            var key     = Convert.FromBase64String(Options.DesKey);
            var encrypt = DES3.EncryptECB(key, data);
            // 字典排序
            var reqdic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JdPayUtil.SortedDictionary2XmlStr(reqdic));
        }