chinapay扫码支付

/// 
        /// 提交订单获取支付二维码
        /// 
        /// 
        /// 
        /// 
        public string GetChinaPay(decimal amount, string order_no)
        {
            string resultStr = "0";

            Hashtable myMap = new Hashtable();
            myMap.Add("MerId", " ");
            myMap.Add("MerOrderNo", order_no);
            myMap.Add("TranDate", DateTime.Now.ToString("yyyyMMdd"));
            myMap.Add("TranTime", DateTime.Now.ToString("HHmmss"));
            myMap.Add("OrderAmt", (amount * 100).ToString("0"));
            myMap.Add("TranType", "0001");
            myMap.Add("BusiType", "0001");
            myMap.Add("AccessType", "0");
            myMap.Add("MerPageUrl", frontUrl);
            myMap.Add("MerBgUrl", backUrl);
            myMap.Add("CurryNo", "CNY");
            myMap.Add("CommodityMsg", "支付订单");
            myMap.Add("PayTimeOut", "145");
            myMap.Add("Version", "20140728");
            myMap.Add("OrderReserved", "{\"OrderType\":\"0001\"}");

            chinapaysecure.SecssUtil obj = new chinapaysecure.SecssUtil();

            obj.init(chinaPayFile);
            NetLog.WriteTextLog("log", chinaPayFile, DateTime.Now, "");

            obj.sign(myMap);
            String signature = obj.getSign();
            if ("00" == obj.getErrCode())
            {
                myMap.Add("Signature", signature);

                string parmas = "";
                int i = 0;
                foreach (DictionaryEntry de in myMap)
                {
                    if (i > 0)
                    {
                        parmas += "&";
                    }
                    parmas += de.Key + "=" + HttpUtility.UrlEncode(de.Value.ToString(), Encoding.UTF8);
                    //parmas += de.Key + "=" + de.Value;
                    i++;
                }
                string result = Post("http://www.chinapay.com/cofcoko/bgTransGet", parmas);
                string code = ""; string PayReserved = "";
                string[] results = result.Split('&');
                for (int s = 0; s < results.Length; s++)
                {
                    if (results[s].Split('=')[0] == "respCode")
                    {
                        code = results[s].Split('=')[1];
                    }
                    else if (results[s].Split('=')[0] == "PayReserved")
                    {
                        PayReserved = results[s].Replace("PayReserved=", "");
                    }
                }
                if (code == "0000")
                {
                    Dictionary res = JsonHelper.DataRowFromJSON(PayReserved);
                    foreach (var item in res)
                    {
                        if (item.Key == "QrCode")
                        {
                            resultStr = item.Value.ToString();
                            break;
                        }
                    }
                }
            }
            return resultStr;
        }

        /// 
        /// chinapay应答验签
        /// 
        /// 
        /// 
        public string CheckChinaPay(string dataStr)
        {
            string result = "";
            try
            {
                if (!string.IsNullOrEmpty(dataStr))
                {
                    Hashtable myMap = new Hashtable();
                    string order_no = "", amount = "";
                    //dataStr = HttpUtility.UrlDecode(dataStr);
                    string[] infos = dataStr.Split('&');
                    for (int i = 0; i < infos.Length; i++)
                    {
                        string[] item = infos[i].Split('=');
                        if (item.Length == 2 && item[0] == "MerOrderNo")
                        {
                            order_no = item[1];
                        }
                        else if (item.Length == 2 && item[0] == "OrderAmt")
                        {
                            amount = item[1];
                        }
                        myMap.Add(item[0], HttpUtility.UrlDecode(item[1], Encoding.UTF8));
                    }
                    SecssUtil obj = new SecssUtil();
                    obj.init(chinaPayFile); //初始化安全控件:
                    obj.verify(myMap);
                    if (obj.getErrCode() != "00")
                    {
                        result = obj.getErrMsg();
                    }
                    else
                    {
                        result = order_no + "/" + amount;
                    }
                }
            }
            catch (Exception e)
            {
                result = e.Message.ToString();
            }
            return result;

        }


/// 
        /// HTTP POST方式请求数据
        /// 
        /// URL.
        /// POST的数据
        /// 
        public static string Post(string url, string param)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(param);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentLength = byteArray.Length;

            Stream requestStream = null;
            WebResponse response = null;
            string responseStr = null;

            try
            {
                requestStream = request.GetRequestStream();
                requestStream.Write(byteArray, 0, byteArray.Length);
                requestStream.Close();

                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }

            return responseStr;
        }


你可能感兴趣的:(C#基础)