Пример #1
0
        public static string BuildRequest(SortedDictionary <string, string> sParaTemp, string strMethod, string strButtonValue, Config config)
        {
            Dictionary <string, string> strs = new Dictionary <string, string>();

            strs = Submit.BuildRequestPara(sParaTemp, config);
            StringBuilder stringBuilder = new StringBuilder();

            string[] gATEWAYNEW = new string[] { "<form id='alipaysubmit' name='alipaysubmit' action='", Submit.GATEWAY_NEW, "_input_charset=", Submit._input_charset, "' method='", strMethod.ToLower().Trim(), "'>" };
            stringBuilder.Append(string.Concat(gATEWAYNEW));
            foreach (KeyValuePair <string, string> str in strs)
            {
                gATEWAYNEW = new string[] { "<input type='hidden' name='", str.Key, "' value='", str.Value, "'/>" };
                stringBuilder.Append(string.Concat(gATEWAYNEW));
            }
            stringBuilder.Append(string.Concat("<input type='submit' value='", strButtonValue, "' style='display:none;'></form>"));
            stringBuilder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
            return(stringBuilder.ToString());
        }
Пример #2
0
        public static string BuildRequest(string GATEWAY_NEW, Dictionary <string, string> sParaTemp, Config config)
        {
            Submit._sign_type     = config.Sign_type;
            Submit._input_charset = config.Input_charset;
            Submit._key           = config.key;
            Encoding encoding = Encoding.GetEncoding(config.Input_charset);
            string   str      = Submit.BuildRequestParaToString(new SortedDictionary <string, string>(sParaTemp), encoding, config);

            byte[] bytes = encoding.GetBytes(str);
            string str1  = string.Concat(GATEWAY_NEW, "?_input_charset=", Submit._input_charset);
            string str2  = "";

            try
            {
                HttpWebRequest length = (HttpWebRequest)WebRequest.Create(str1);
                length.Method        = "post";
                length.ContentType   = "application/x-www-form-urlencoded";
                length.ContentLength = (int)bytes.Length;
                Stream requestStream = length.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                Stream        responseStream = ((HttpWebResponse)length.GetResponse()).GetResponseStream();
                StreamReader  streamReader   = new StreamReader(responseStream, encoding);
                StringBuilder stringBuilder  = new StringBuilder();
                while (true)
                {
                    string str3 = streamReader.ReadLine();
                    string str4 = str3;
                    if (str3 == null)
                    {
                        break;
                    }
                    stringBuilder.Append(str4);
                }
                responseStream.Close();
                str2 = stringBuilder.ToString();
            }
            catch (Exception exception)
            {
                str2 = string.Concat("报错:", exception.Message);
            }
            return(str2);
        }
Пример #3
0
        public string GetRequestUrl(string returnUrl, string notifyUrl, string orderId, decimal totalFee, string productInfo, string openId = null)
        {
            if (string.IsNullOrEmpty(productInfo))
            {
                throw new PluginConfigException("商品信息不能为空!");
            }
            if (string.IsNullOrEmpty(orderId))
            {
                throw new PluginConfigException("订单号不能为空!");
            }
            if (string.IsNullOrWhiteSpace(returnUrl) && string.IsNullOrWhiteSpace(notifyUrl))
            {
                throw new PluginConfigException("返回URL不能为空!");
            }

            if (_config == null)
            {
                _config = Utility <Config> .GetConfig(WorkDirectory);
            }

            string strResult = string.Empty;
            Dictionary <string, string> dicPara = new Dictionary <string, string>();

            dicPara.Add("service", _config.GetCodeService); //取二维码接口
            dicPara.Add("partner", _config.Partner);        //合作者ID,支付宝提供
            dicPara.Add("timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            dicPara.Add("sign_type", _config.Sign_type);    //签名方式,暂时使用MD5
            dicPara.Add("_input_charset", _config.Input_charset);
            dicPara.Add("method", "add");
            dicPara.Add("biz_type", "10");//业务类型:商品码

            //**************************
            #region 整理请求数据
            StringBuilder reqdata = new StringBuilder();
            reqdata.Append("{");
            reqdata.Append("\"trade_type\":\"" + _config.Trade_Type + "\"");
            reqdata.Append(",\"need_address\":\"F\"");

            reqdata.Append(",\"goods_info\":{");
            reqdata.Append("\"id\":\"" + orderId);
            reqdata.Append("\",\"name\":\"" + productInfo.Substring(0, productInfo.Length > 32 ? 32 : productInfo.Length));
            reqdata.Append("\",\"desc\":\"" + productInfo);
            reqdata.Append("\",\"price\":\"" + totalFee.ToString("F2"));
            reqdata.Append("\"}");

            reqdata.Append(",\"notify_url\":\"" + notifyUrl);
            reqdata.Append(",\"return_url\":\"" + notifyUrl);
            reqdata.Append("\"}");

            #endregion
            //**************************
            dicPara.Add("biz_data", reqdata.ToString());

            string strToken = Submit.BuildRequest(dicPara, _config);//调用接口取令牌
            if (!string.IsNullOrWhiteSpace(strToken))
            {
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.LoadXml(strToken);
                    string is_success = xmlDoc.SelectSingleNode("/alipay/is_success").InnerText;
                    if (is_success == "F")
                    {
                        string error = xmlDoc.SelectSingleNode("/alipay/error").InnerText;
                        throw new PluginConfigException("生成二维码出现异常:" + error);
                    }
                    else if (is_success == "T")
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/alipay/response/alipay/result_code");
                        if (node != null && node.InnerText == "SUCCESS")
                        {
                            strResult = xmlDoc.SelectSingleNode("/alipay/response/alipay/qrcode").InnerText;
                        }
                        else
                        {
                            string error = xmlDoc.SelectSingleNode("/alipay/response/alipay/error_message").InnerText;
                            throw new PluginConfigException("生成二维码出现异常:" + error);
                        }
                    }
                }
                catch (Exception exp)
                {
                    throw new PluginConfigException("生成二维码出现异常:" + exp.Message);
                }
            }
            return(strResult);
        }
Пример #4
0
        public static string BuildRequest(SortedDictionary <string, string> sParaTemp, string strMethod, string fileName, byte[] data, string contentType, int lengthFile, Config config)
        {
            Stream responseStream;
            string str;
            Dictionary <string, string> strs = new Dictionary <string, string>();

            strs = Submit.BuildRequestPara(sParaTemp, config);
            string         str1           = string.Concat(Submit.GATEWAY_NEW, "_input_charset=", Submit._input_charset);
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(str1);

            httpWebRequest.Method = strMethod;
            string str2 = DateTime.Now.Ticks.ToString("x");
            string str3 = string.Concat("--", str2);

            httpWebRequest.ContentType = string.Concat("\r\nmultipart/form-data; boundary=", str2);
            httpWebRequest.KeepAlive   = true;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (KeyValuePair <string, string> keyValuePair in strs)
            {
                string[] key = new string[] { str3, "\r\nContent-Disposition: form-data; name=\"", keyValuePair.Key, "\"\r\n\r\n", keyValuePair.Value, "\r\n" };
                stringBuilder.Append(string.Concat(key));
            }
            stringBuilder.Append(string.Concat(str3, "\r\nContent-Disposition: form-data; name=\"withhold_file\"; filename=\""));
            stringBuilder.Append(fileName);
            stringBuilder.Append(string.Concat("\"\r\nContent-Type: ", contentType, "\r\n\r\n"));
            string   str4     = stringBuilder.ToString();
            Encoding encoding = Encoding.GetEncoding(Submit._input_charset);

            byte[] bytes    = encoding.GetBytes(str4);
            byte[] numArray = Encoding.ASCII.GetBytes(string.Concat("\r\n", str3, "--\r\n"));
            long   length   = (int)bytes.Length + lengthFile + (int)numArray.Length;

            httpWebRequest.ContentLength = length;
            Stream requestStream = httpWebRequest.GetRequestStream();

            try
            {
                try
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Write(data, 0, lengthFile);
                    requestStream.Write(numArray, 0, numArray.Length);
                    responseStream = ((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream();
                }
                catch (WebException webException)
                {
                    str = webException.ToString();
                    return(str);
                }
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }
            StreamReader  streamReader   = new StreamReader(responseStream, encoding);
            StringBuilder stringBuilder1 = new StringBuilder();

            while (true)
            {
                string str5 = streamReader.ReadLine();
                string str6 = str5;
                if (str5 == null)
                {
                    break;
                }
                stringBuilder1.Append(str6);
            }
            responseStream.Close();
            str = stringBuilder1.ToString();
            return(str);
        }
Пример #5
0
        public string GetRequestUrl(string returnUrl, string notifyUrl, string orderId, decimal totalFee, string productInfo, string openId = null)
        {
            string innerText;

            if (string.IsNullOrEmpty(productInfo))
            {
                throw new PluginConfigException("商品信息不能为空!");
            }
            if (string.IsNullOrEmpty(orderId))
            {
                throw new PluginConfigException("订单号不能为空!");
            }
            if ((!string.IsNullOrWhiteSpace(returnUrl) ? false : string.IsNullOrWhiteSpace(notifyUrl)))
            {
                throw new PluginConfigException("返回URL不能为空!");
            }
            if (Service._config == null)
            {
                Service._config = Utility <Config> .GetConfig(base.WorkDirectory);
            }
            string empty = string.Empty;
            Dictionary <string, string> strs = new Dictionary <string, string>()
            {
                { "service", Service._config.getCodeService },
                { "partner", Service._config.Partner },
                { "timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                { "sign_type", Service._config.Sign_type },
                { "_input_charset", Service._config.Input_charset },
                { "method", "add" },
                { "biz_type", "10" }
            };
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("{");
            stringBuilder.Append(string.Concat("\"trade_type\":\"", Service._config.trade_type, "\""));
            stringBuilder.Append(",\"need_address\":\"F\"");
            stringBuilder.Append(",\"goods_info\":{");
            stringBuilder.Append(string.Concat("\"id\":\"", orderId));
            stringBuilder.Append(string.Concat("\",\"name\":\"", productInfo.Substring(0, (productInfo.Length > 32 ? 32 : productInfo.Length))));
            stringBuilder.Append(string.Concat("\",\"desc\":\"", productInfo));
            stringBuilder.Append(string.Concat("\",\"price\":\"", totalFee.ToString("F2")));
            stringBuilder.Append("\"}");
            stringBuilder.Append(string.Concat(",\"notify_url\":\"", notifyUrl));
            stringBuilder.Append(string.Concat(",\"return_url\":\"", notifyUrl));
            stringBuilder.Append("\"}");
            strs.Add("biz_data", stringBuilder.ToString());
            string str = Submit.BuildRequest(Service._config.gateWay, strs, Service._config);

            if (!string.IsNullOrWhiteSpace(str))
            {
                XmlDocument xmlDocument = new XmlDocument();
                try
                {
                    xmlDocument.LoadXml(str);
                    string innerText1 = xmlDocument.SelectSingleNode("/alipay/is_success").InnerText;
                    if (innerText1 == "F")
                    {
                        innerText = xmlDocument.SelectSingleNode("/alipay/error").InnerText;
                        throw new PluginConfigException(string.Concat("生成二维码出现异常:", innerText));
                    }
                    if (innerText1 == "T")
                    {
                        XmlNode xmlNodes = xmlDocument.SelectSingleNode("/alipay/response/alipay/result_code");
                        if ((xmlNodes == null ? true : !(xmlNodes.InnerText == "SUCCESS")))
                        {
                            innerText = xmlDocument.SelectSingleNode("/alipay/response/alipay/error_message").InnerText;
                            throw new PluginConfigException(string.Concat("生成二维码出现异常:", innerText));
                        }
                        empty = xmlDocument.SelectSingleNode("/alipay/response/alipay/qrcode").InnerText;
                    }
                }
                catch (Exception exception)
                {
                    throw new PluginConfigException(string.Concat("生成二维码出现异常:", exception.Message));
                }
            }
            return(empty);
        }