Ejemplo n.º 1
0
        public Task <T> ExecuteAsync <T>(HttpRequest request) where T : UnionPayNotifyResponse
        {
            var parameters = new UnionPayDictionary();

            if (request.Method == "POST")
            {
                foreach (var item in request.Form)
                {
                    parameters.Add(item.Key, item.Value);
                }
            }

            if (parameters == null || parameters.Count == 0)
            {
                throw new Exception("sign check fail: sign is Empty!");
            }

            if (!UnionPaySignature.Validate(parameters, Options.SecureKey))
            {
                throw new Exception("sign check fail: check Sign and Data Fail!");
            }

            var parser = new UnionPayDictionaryParser <T>();
            var rsp    = parser.Parse(parameters);

            return(Task.FromResult(rsp));
        }
Ejemplo n.º 2
0
        public Task <T> PageExecuteAsync <T>(IUnionPayRequest <T> request, string reqMethod) where T : UnionPayResponse
        {
            var version = string.Empty;

            if (!string.IsNullOrEmpty(request.GetApiVersion()))
            {
                version = request.GetApiVersion();
            }
            else
            {
                version = Options.Version;
            }

            var txtParams = new UnionPayDictionary(request.GetParameters())
            {
                { VERSION, version },
                { ENCODING, Options.Encoding },
                { SIGNMETHOD, Options.SignMethod },

                { TXNTYPE, request.GetTxnType() },
                { TXNSUBTYPE, request.GetTxnSubType() },
                { BIZTYPE, request.GetBizType() },
                { CHANNELTYPE, request.GetChannelType() },

                { ACCESSTYPE, Options.AccessType },
                { MERID, Options.MerId },
            };

            UnionPaySignature.Sign(txtParams, SignCertificate.certId, SignCertificate.key, Options.SecureKey);

            var rsp = Activator.CreateInstance <T>();

            var url = request.GetRequestUrl(Options.TestMode);

            if (reqMethod == "GET")
            {
                //拼接get请求的url
                var tmpUrl = url;
                if (txtParams != null && txtParams.Count > 0)
                {
                    if (tmpUrl.Contains("?"))
                    {
                        tmpUrl = tmpUrl + "&" + HttpClientEx.BuildQuery(txtParams);
                    }
                    else
                    {
                        tmpUrl = tmpUrl + "?" + HttpClientEx.BuildQuery(txtParams);
                    }
                }
                rsp.Body = tmpUrl;
            }
            else
            {
                //输出post表单
                rsp.Body = BuildHtmlRequest(url, txtParams, reqMethod);
            }

            return(Task.FromResult(rsp));
        }
Ejemplo n.º 3
0
        private string BuildHtmlRequest(string url, UnionPayDictionary 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());
        }
Ejemplo n.º 4
0
        public async Task <T> ExecuteAsync <T>(IUnionPayRequest <T> request) where T : UnionPayResponse
        {
            var version = string.Empty;

            if (!string.IsNullOrEmpty(request.GetApiVersion()))
            {
                version = request.GetApiVersion();
            }
            else
            {
                version = Options.Version;
            }

            var merId = Options.MerId;

            if (Options.TestMode && (request is UnionPayForm05_7_FileTransferRequest || request is UnionPayForm_6_6_FileTransferRequest))
            {
                merId = "700000000000001";
            }

            var txtParams = new UnionPayDictionary(request.GetParameters())
            {
                { VERSION, version },
                { ENCODING, Options.Encoding },
                { SIGNMETHOD, Options.SignMethod },

                { TXNTYPE, request.GetTxnType() },
                { TXNSUBTYPE, request.GetTxnSubType() },
                { BIZTYPE, request.GetBizType() },
                { CHANNELTYPE, request.GetChannelType() },

                { ACCESSTYPE, Options.AccessType },
                { MERID, merId },
            };

            UnionPaySignature.Sign(txtParams, SignCertificate.certId, SignCertificate.key, Options.SecureKey);

            var resContent = await Client.DoPostAsync(request.GetRequestUrl(Options.TestMode), txtParams);

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

            if (resContent.ContentType.Contains("text/plain"))
            {
                throw new Exception(resContent.Content);
            }

            var dic = ParseQueryString(resContent.Content);

            if (dic == null || dic.Count == 0)
            {
                throw new Exception("sign check fail: Body is Empty!");
            }

            if (!UnionPaySignature.Validate(dic, Options.SecureKey))
            {
                throw new Exception("sign check fail: check Sign and Data Fail!");
            }

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

            rsp.Body = resContent.Content;
            return(rsp);
        }