RequestHandler.class.php
2.79 KB
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
162
163
164
165
166
<?php
/**
* 请求类
* ============================================================================
* api说明:
* init(),初始化函数,默认给一些参数赋值,如cmdno,date等。
* getGateURL()/setGateURL(),获取/设置入口地址,不包含参数值
* getKey()/setKey(),获取/设置密钥
* getParameter()/setParameter(),获取/设置参数值
* getAllParameters(),获取所有参数
* getRequestURL(),获取带参数的请求URL
* doSend(),重定向到财付通支付
* getDebugInfo(),获取debug信息
*
* ============================================================================
*
*/
class RequestHandler {
/** 网关url地址 */
var $gateUrl;
/** 密钥 */
var $key;
/** 请求的参数 */
var $parameters;
/** debug信息 */
var $debugInfo;
function __construct() {
$this->RequestHandler();
}
function RequestHandler() {
$this->gateUrl = "https://www.tenpay.com/cgi-bin/v1.0/service_gate.cgi";
$this->key = "";
$this->parameters = array();
$this->debugInfo = "";
}
/**
*初始化函数。
*/
function init() {
//nothing to do
}
/**
*获取入口地址,不包含参数值
*/
function getGateURL() {
return $this->gateUrl;
}
/**
*设置入口地址,不包含参数值
*/
function setGateURL($gateUrl) {
$this->gateUrl = $gateUrl;
}
/**
*获取密钥
*/
function getKey() {
return $this->key;
}
/**
*设置密钥
*/
function setKey($key) {
$this->key = $key;
}
/**
*获取参数值
*/
function getParameter($parameter) {
return $this->parameters[$parameter];
}
/**
*设置参数值
*/
function setParameter($parameter, $parameterValue) {
$this->parameters[$parameter] = $parameterValue;
}
/**
*获取所有请求的参数
*@return array
*/
function getAllParameters() {
return $this->parameters;
}
/**
*获取带参数的请求URL
*/
function getRequestURL() {
$this->createSign();
$reqPar = "";
ksort($this->parameters);
foreach($this->parameters as $k => $v) {
$reqPar .= $k . "=" . urlencode($v) . "&";
}
//去掉最后一个&
$reqPar = substr($reqPar, 0, strlen($reqPar)-1);
$requestURL = $this->getGateURL() . "?" . $reqPar;
return $requestURL;
}
/**
*获取debug信息
*/
function getDebugInfo() {
return $this->debugInfo;
}
/**
*重定向到财付通支付
*/
function doSend() {
header("Location:" . $this->getRequestURL());
exit;
}
/**
*创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
*/
function createSign() {
$signPars = "";
ksort($this->parameters);
foreach($this->parameters as $k => $v) {
if("" != $v && "sign" != $k) {
$signPars .= $k . "=" . $v . "&";
}
}
$signPars .= "key=" . $this->getKey();
$sign = strtolower(md5($signPars));
$this->setParameter("sign", $sign);
//debug信息
$this->_setDebugInfo($signPars . " => sign:" . $sign);
}
/**
*设置debug信息
*/
function _setDebugInfo($debugInfo) {
$this->debugInfo = $debugInfo;
}
}
?>