Example #1
0
        private JdPayDictionary GetEncryptDicContent <T>(IJdPayRequest <T> request, IDictionary <string, string> dic) where T : JdPayResponse
        {
            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(DesKey, item.Value));
                }
            }
            return(encyptDic);
        }
Example #2
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);
            Logger.LogInformation(0, "Request Html:{body}", rsp.Body);
            return(Task.FromResult(rsp));
        }
Example #3
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());
        }
Example #4
0
        public async Task <T> ExecuteAsync <T>(IJdPayRequest <T> request) where T : JdPayResponse
        {
            // 字典排序
            var sortedTxtParams = new JdPayDictionary(request.GetParameters());

            var content = GetEncryptXmlContent(request, sortedTxtParams);

            Logger.LogInformation(0, "Request Content:{content}", content);

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

            Logger.LogInformation(1, "Response Content:{content}", rspContent);

            var formatStr = JdPayUtil.FotmatXmlString(rspContent);

            return(DecryptResponseXmlContent <T>(formatStr));
        }
Example #5
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 encrypt = DES3.EncryptECB(DesKey, data);
            // 字典排序
            var reqdic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JdPayUtil.SortedDictionary2XmlStr(reqdic));
        }
Example #6
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");
            }
        }
Example #7
0
        public async Task <T> ExecuteAsync <T>(HttpRequest request) where T : JdPayNotifyResponse
        {
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                var dic = new JdPayDictionary();
                foreach (var item in request.Form)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                    {
                        if (item.Key == "sign")
                        {
                            dic.Add(item.Key, item.Value);
                        }
                        else
                        {
                            dic.Add(item.Key, DES3.DecryptECB(DesKey, item.Value));
                        }
                    }
                }

                var query = HttpClientEx.BuildQuery(dic);
                Logger.LogInformation(1, "Request Content:{query}", query);

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

                CheckNotifySign(dic, RSAPrivateParameters);
                return(rsp);
            }
            else
            {
                var body = await new StreamReader(request.Body).ReadToEndAsync();
                Logger.LogInformation(1, "Request Content:{body}", body);

                var rsp = DecryptResponseXml <T>(JdPayUtil.FotmatXmlString(body));
                return(rsp);
            }
        }