-
Notifications
You must be signed in to change notification settings - Fork 4
/
quickpay_service.php
374 lines (331 loc) · 12.8 KB
/
quickpay_service.php
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/*
* @file quickpay_service.inc.php
* @author fengmin([email protected])
* @date 2011-08-22
* @version $Revision$
*
*/
//require_once(dirname(__FILE__) . '/quickpay_conf.php');
include_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'unionpay') . '/quickpay_conf.php');
if (function_exists("date_default_timezone_set")) {
date_default_timezone_set(quickpay_conf::$timezone);
}
class quickpay_service
{
const RESP_SUCCESS = "00"; //返回成功
const QUERY_SUCCESS = "0"; //查询成功
const QUERY_FAIL = "1";
const QUERY_WAIT = "2";
const QUERY_INVALID = "3";
var $args;
var $api_url;
var $signature;
var $signMethod;
function quickpay_service($args, $service)
{
$param_check = array();
switch($service)
{
case quickpay_conf::FRONT_PAY:
$trans_type = $args['transType'];
if ($trans_type != quickpay_conf::CONSUME && $trans_type != quickpay_conf::PRE_AUTH) {
//前台交易仅支持 消费 和 预授权
throw new Exception("Bad trans_type for front_pay. Use back_pay instead");
}
$this->api_url = quickpay_conf::$urls[variable_get('unionpay_site_status', 'test')]['front'];
$this->args = array_merge(quickpay_conf::$pay_params_empty,
quickpay_conf::$pay_params, $args);
$param_check = quickpay_conf::$pay_params_check;
break;
case quickpay_conf::BACK_PAY:
$this->api_url = quickpay_conf::$urls[variable_get('unionpay_site_status', 'test')]['back'];
$this->args = array_merge(quickpay_conf::$pay_params_empty,
quickpay_conf::$pay_params, $args);
$param_check = quickpay_conf::$pay_params_check;
$trans_type = $this->args['transType'];
if ($trans_type == quickpay_conf::CONSUME || $trans_type == quickpay_conf::PRE_AUTH) {
if (!isset($this->args['cardNumber']) && !isset($this->args['pan'])) {
throw new Exception('consume OR pre_auth transactions need cardNumber!');
}
}
else {
if (empty($this->args['origQid'])) {
throw new Exception('origQid is not provided');
}
}
break;
case quickpay_conf::QUERY:
$this->api_url = quickpay_conf::$urls[variable_get('unionpay_site_status', 'test')]['query'];
$args['version'] = quickpay_conf::$pay_params['version'];
$args['charset'] = quickpay_conf::$pay_params['charset'];
if (empty($args['merId']) &&
empty($args['acqCode']))
{
throw new Exception('merId and acqCode can\'t be both empty');
}
//acqCode在QUERY请求中作为保留域存在
if (!empty($args['acqCode'])) {
$acqCode = $args['acqCode'];
$args['merReserved'] = "{acqCode=$acqCode}";
}
else {
$args['merReserved'] = '';
}
unset($args['acqCode']);
$this->args = $args;
$param_check = quickpay_conf::$query_params_check;
break;
case quickpay_conf::RESPONSE:
$arr_args = array();
$arr_reserved = array();
if (is_array($args)) {
$arr_args = $args;
$cupReserved = isset($arr_args['cupReserved']) ? $arr_args['cupReserved'] : '';
parse_str(substr($cupReserved, 1, -1), $arr_reserved); //去掉前后的{}
}
else {
$cupReserved = '';
$pattern = '/cupReserved=(\{.*?\})/';
if (preg_match($pattern, $args, $match)) { //先提取cupReserved
$cupReserved = $match[1];
}
//将cupReserved的value清除(因为含有&, parse_str没法正常处理)
$args_r = preg_replace($pattern, 'cupReserved=', $args);
parse_str($args_r, $arr_args);
$arr_args['cupReserved'] = $cupReserved;
parse_str(substr($cupReserved, 1, -1), $arr_reserved); //去掉前后的{}
}
//提取服务器端的签名
if (!isset($arr_args['signature']) || !isset($arr_args['signMethod'])) {
throw new Exception('No signature Or signMethod set in notify data!');
}
$this->signature = $arr_args['signature'];
$this->signMethod= $arr_args['signMethod'];
unset($arr_args['signature']);
unset($arr_args['signMethod']);
//验证签名
$signature = self::sign($arr_args, $this->signMethod);
if ($signature != $this->signature) {
throw new Exception('Bad signature returned!');
}
$this->args = array_merge($arr_args, $arr_reserved);
unset($this->args['cupReserved']);
return; //RESPONSE参数不需要作后续处理了
default:
throw new Exception("Unknown service provided.");
}
if (isset($this->args['commodityUrl'])) {
$this->args['commodityUrl'] = self::encodeURI($this->args['commodityUrl']);
}
//merReserved: 前后台支付、查询
$has_reserved = false;
$arr_reserved = array();
foreach (quickpay_conf::$mer_params_reserved as $key) {
if (isset($this->args[$key])) {
$value = $this->args[$key];
unset($this->args[$key]);
$arr_reserved[] = "$key=$value";
$has_reserved = true;
}
}
if ($has_reserved) {
$this->args['merReserved'] = sprintf("{%s}", join("&", $arr_reserved));
}
else {
//请求一定有merReserved字段
if (!isset($this->args['merReserved'])) {
$this->args['merReserved'] = '';
}
}
//param check
foreach ($param_check as $key) {
if (!isset($this->args[$key])) {
throw new Exception("KEY [$key] not set in params given");
}
}
//signature
$this->args['signature'] = self::sign($this->args, quickpay_conf::$sign_method);
$this->args['signMethod'] = quickpay_conf::$sign_method;
} //end of constructor
function get($key, $default = null)
{
if (isset($this->args[$key])) {
return $this->args[$key];
}
return $default;
}
function get_args()
{
return $this->args;
}
static function encode($value, $output_charset, $input_charset)
{
if (strtolower($input_charset) == strtolower($output_charset) || empty($value)) {
return $value;
}
if (function_exists("iconv")) {
return iconv($input_charset, $output_charset, $value);
}
if (function_exists("mb_convert_encoding")) {
return mb_convert_encoding($value, $output_charset, $input_charset);
}
throw new Exception("sorry, mbstring or iconv is needed for charset conversion.");
}
static function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
static function encodeURI($url)
{
if (preg_match("/^(.*?)\?(.*)/", $url, $match)) {
$prefix = preg_replace("/\?.*/", "", $url);
$query_string = $match[2];
$arr_keqv = explode('&', $query_string);
$arr_encoded = array();
foreach ($arr_keqv as $keqv) {
list($key, $value) = explode('=', $keqv);
$arr_encoded[] = sprintf("%s=%s", $key, urlencode($value));
}
$query_string = join('&', $arr_encoded);
return $prefix . '?' . $query_string;
}
else {
return $url;
}
}
static function decodeURI($url)
{
if (preg_match("/^(.*?)\?(.*)/", $url, $match)) {
$prefix = preg_replace("/\?.*/", "", $url);
$query_string = $match[2];
$arr_keqv = explode('&', $query_string);
$arr_decoded = array();
foreach ($arr_keqv as $keqv) {
list($key, $value) = explode('=', $keqv);
$arr_decoded[] = sprintf("%s=%s", $key, urldecode($value));
}
$query_string = join('&', $arr_decoded);
return $prefix . '?' . $query_string;
}
else {
return $url;
}
}
static function sign($params, $sign_method)
{
if (strtolower($sign_method) == "md5") {
ksort($params);
$sign_str = "";
foreach ($params as $key => $val) {
if (in_array($key, quickpay_conf::$sign_ignore_params)) {
continue;
}
$sign_str .= sprintf("%s=%s&", $key, $val);
}
return md5($sign_str . md5(variable_get('unionpay_security_key', quickpay_conf::$security_key)));
}
/* TODO: elseif (strtolower($sign_method) == "rsa") */
else {
throw new Exception("Unknown sign_method set in quickpay_conf");
}
}
function create_html()
{
$html = <<<eot
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset={$this->args['charset']}" />
</head>
<body onload="javascript:document.pay_form.submit();">
<form id="pay_form" name="pay_form" action="{$this->api_url}" method="post">
eot;
foreach ($this->args as $key => $value) {
$html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
}
$html .= <<<eot
</form>
</body>
</html>
eot;
return $html;
}
function create_query_string()
{
$query_string = '';
foreach ($this->args as $key => $value) {
$query_string .= sprintf("%s=%s&", $key, urlencode($value));
}
return $query_string;
}
function post()
{
return self::curl_call($this->api_url, $this->args, array(
CURLOPT_SSL_VERIFYPEER => quickpay_conf::VERIFY_HTTPS_CERT,
CURLOPT_SSL_VERIFYHOST => quickpay_conf::VERIFY_HTTPS_CERT,
));
}
/*
* curl_call
*
* @url: string, curl url to call, may have query string like ?a=b
* @data: array(key => value), data for get/post
* @is_post, boolean, true=post, false=get
* @options, array(curl_option => option_value), extra curl options
*
* return param:
* mixed:
* false: error happened
* string: curl return data
*
*/
static function curl_call($url, $data = null, $is_post = true, $options = null)
{
if (function_exists("curl_init")) {
$curl = curl_init();
if (is_array($data)) {
$data = http_build_query($data);
}
if ($is_post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
else { //GET
if (!empty($data)) {
$sep = '?';
if (strpos($url, '?') !== false) {
$sep = '&';
}
$url .= $sep . $data;
}
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 60); //seconds
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, quickpay_conf::VERIFY_HTTPS_CERT);
if (is_array($options)) {
foreach($options as $key => $value) {
curl_setopt($curl, $key, $value);
}
}
$ret_data = curl_exec($curl);
if (curl_errno($curl)) {
printf("curl call error(%s): %s\n", curl_errno($curl), curl_error($curl));
curl_close($curl);
return false;
}
else {
//printf("curl call ok, ret_data: %s\n", $ret_data);
curl_close($curl);
return $ret_data;
}
}
/* TODO: elseif (function_exists('fsockopen')) { } */
else {
throw new Exception("[PHP] curl module is required");
}
}
}
?>