1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Web;
 
namespace JumbotCms.API.Tenpay
{
    /// <summary>
    /// ResponseHandler µÄժҪ˵Ã÷¡£
    /// </summary>
    public class ResponseHandler
    {
        /** ÃÜÔ¿ */
        private string key;
        
        /** Ó¦´ðµÄ²ÎÊý */
        protected Hashtable parameters;
        
        /** debugÐÅÏ¢ */
        private string debugInfo;
 
        protected HttpContext httpContext;
 
        //»ñÈ¡·þÎñÆ÷֪ͨÊý¾Ý·½Ê½£¬½øÐвÎÊý»ñÈ¡
        public ResponseHandler(HttpContext httpContext)
        {
            parameters = new Hashtable();
 
            this.httpContext = httpContext;
            NameValueCollection collection;
            if(this.httpContext.Request.HttpMethod == "POST")
            {
                collection = this.httpContext.Request.Form;
            }
            else
            {
                collection = this.httpContext.Request.QueryString;
            }
 
            foreach(string k in collection)
            {
                string v = (string)collection[k];
                this.setParameter(k, v);
            }
        }    
 
        /** »ñÈ¡ÃÜÔ¿ */
        public string getKey() 
        { return key;}
 
        /** ÉèÖÃÃÜÔ¿ */
        public void setKey(string key) 
        { this.key = key;}
 
        /** »ñÈ¡²ÎÊýÖµ */
        public string getParameter(string parameter) 
        {
            string s = (string)parameters[parameter];
            return (null == s) ? "" : s;
        }
 
        /** ÉèÖòÎÊýÖµ */
        public void setParameter(string parameter,string parameterValue) 
        {
            if(parameter != null && parameter != "")
            {
                if(parameters.Contains(parameter))
                {
                    parameters.Remove(parameter);
                }
    
                parameters.Add(parameter,parameterValue);        
            }
        }
 
        /** ÊÇ·ñ²Æ¸¶Í¨Ç©Ãû,¹æÔòÊÇ:°´²ÎÊýÃû³Æa-zÅÅÐò,Óöµ½¿ÕÖµµÄ²ÎÊý²»²Î¼ÓÇ©Ãû¡£ 
         * @return boolean */
        public virtual Boolean isTenpaySign() 
        {
            StringBuilder sb = new StringBuilder();
 
            ArrayList akeys=new ArrayList(parameters.Keys); 
            akeys.Sort();
 
            foreach(string k in akeys)
            {
                string v = (string)parameters[k];
                if(null != v && "".CompareTo(v) != 0
                    && "sign".CompareTo(k) != 0 && "key".CompareTo(k) != 0) 
                {
                    sb.Append(k + "=" + v + "&");
                }
            }
 
            sb.Append("key=" + this.getKey());
            string sign = MD5Util.GetMD5(sb.ToString(),getCharset()).ToLower();
            
            //debugÐÅÏ¢
            this.setDebugInfo(sb.ToString() + " => sign:" + sign);
            return getParameter("sign").ToLower().Equals(sign); 
        }
 
        /**
        * ÏÔʾ´¦Àí½á¹û¡£
        * @param show_url ÏÔʾ´¦Àí½á¹ûµÄurlµØÖ·,¾ø¶ÔurlµØÖ·µÄÐÎʽ(http://www.xxx.com/xxx.aspx)¡£
        * @throws IOException 
        */
        public void doShow(string show_url) 
        {
            string strHtml = "<html><head>\r\n" +
                "<meta name=\"TENCENT_ONLINE_PAYMENT\" content=\"China TENCENT\">\r\n" +
                "<script language=\"javascript\">\r\n" +
                "window.location.href='" + show_url + "';\r\n" +
                "</script>\r\n" +
                "</head><body></body></html>";
 
            this.httpContext.Response.Write(strHtml);
 
            this.httpContext.Response.End();        
        }
 
        /** »ñÈ¡debugÐÅÏ¢ */
        public string getDebugInfo() 
        { return debugInfo;}
                
        /** ÉèÖÃdebugÐÅÏ¢ */
        protected void setDebugInfo(String debugInfo)
        { this.debugInfo = debugInfo;}
 
        protected virtual string getCharset()
        {
            return this.httpContext.Request.ContentEncoding.BodyName;
            
        }
 
        /** ÊÇ·ñ²Æ¸¶Í¨Ç©Ãû,¹æÔòÊÇ:°´²ÎÊýÃû³Æa-zÅÅÐò,Óöµ½¿ÕÖµµÄ²ÎÊý²»²Î¼ÓÇ©Ãû¡£ 
         * @return boolean */
        public virtual Boolean _isTenpaySign(ArrayList akeys) 
        {
            StringBuilder sb = new StringBuilder();
 
            foreach(string k in akeys)
            {
                string v = (string)parameters[k];
                if(null != v && "".CompareTo(v) != 0
                    && "sign".CompareTo(k) != 0 && "key".CompareTo(k) != 0) 
                {
                    sb.Append(k + "=" + v + "&");
                }
            }
 
            sb.Append("key=" + this.getKey());
            string sign = MD5Util.GetMD5(sb.ToString(),getCharset()).ToLower();
            
            //debugÐÅÏ¢
            this.setDebugInfo(sb.ToString() + " => sign:" + sign);
            return getParameter("sign").ToLower().Equals(sign); 
        }
    }
}