1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: kaylv <[email protected]> |
5
|
|
|
* Date: 2019/10/16 |
6
|
|
|
* Time: 11:05 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Kaylyu\Alipay\F2fpay\Kernel; |
10
|
|
|
|
11
|
|
|
use Kaylyu\Alipay\F2fpay\Base\Aop\AopClient as BaseAopClient; |
12
|
|
|
use Kaylyu\Alipay\Kernel\Exceptions\Exception; |
13
|
|
|
|
14
|
|
|
class AopClient extends BaseAopClient |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* 准备请求参数 |
18
|
|
|
* @param $request |
19
|
|
|
* @param null $authToken |
20
|
|
|
* @param null $appInfoAuthtoken |
21
|
|
|
* @author kaylv <[email protected]> |
22
|
|
|
* @return array |
23
|
|
|
* @throws Exception |
24
|
|
|
*/ |
25
|
|
|
public function requestPrepare($request, $authToken = null, $appInfoAuthtoken = null) |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
$this->setupCharsets($request); |
29
|
|
|
|
30
|
|
|
// // 如果两者编码不一致,会出现签名验签或者乱码 |
31
|
|
View Code Duplication |
if (strcasecmp($this->fileCharset, $this->postCharset)) { |
|
|
|
|
32
|
|
|
|
33
|
|
|
// writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!"); |
34
|
|
|
throw new Exception("文件编码:[" . $this->fileCharset . "] 与表单提交编码:[" . $this->postCharset . "]两者不一致!"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$iv = null; |
|
|
|
|
38
|
|
|
|
39
|
|
|
if (!$this->checkEmpty($request->getApiVersion())) { |
40
|
|
|
$iv = $request->getApiVersion(); |
41
|
|
|
} else { |
42
|
|
|
$iv = $this->apiVersion; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
//组装系统参数 |
46
|
|
|
$sysParams["app_id"] = $this->appId; |
|
|
|
|
47
|
|
|
$sysParams["version"] = $iv; |
48
|
|
|
$sysParams["format"] = $this->format; |
49
|
|
|
$sysParams["sign_type"] = $this->signType; |
50
|
|
|
$sysParams["method"] = $request->getApiMethodName(); |
51
|
|
|
$sysParams["timestamp"] = date("Y-m-d H:i:s"); |
52
|
|
|
$sysParams["auth_token"] = $authToken; |
53
|
|
|
$sysParams["alipay_sdk"] = $this->alipaySdkVersion; |
54
|
|
|
$sysParams["terminal_type"] = $request->getTerminalType(); |
55
|
|
|
$sysParams["terminal_info"] = $request->getTerminalInfo(); |
56
|
|
|
$sysParams["prod_code"] = $request->getProdCode(); |
57
|
|
|
$sysParams["notify_url"] = $request->getNotifyUrl(); |
58
|
|
|
$sysParams["charset"] = $this->postCharset; |
59
|
|
|
$sysParams["app_auth_token"] = $appInfoAuthtoken; |
60
|
|
|
|
61
|
|
|
//获取业务参数 |
62
|
|
|
$apiParams = $request->getApiParas(); |
63
|
|
|
|
64
|
|
View Code Duplication |
if (method_exists($request, "getNeedEncrypt") && $request->getNeedEncrypt()) { |
|
|
|
|
65
|
|
|
|
66
|
|
|
$sysParams["encrypt_type"] = $this->encryptType; |
67
|
|
|
|
68
|
|
|
if ($this->checkEmpty($apiParams['biz_content'])) { |
69
|
|
|
|
70
|
|
|
throw new Exception(" api request Fail! The reason : encrypt request is not supperted!"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->checkEmpty($this->encryptKey) || $this->checkEmpty($this->encryptType)) { |
74
|
|
|
|
75
|
|
|
throw new Exception(" encryptType and encryptKey must not null! "); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ("AES" != $this->encryptType) { |
79
|
|
|
|
80
|
|
|
throw new Exception("加密类型只支持AES"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// 执行加密 |
84
|
|
|
$enCryptContent = \Kaylyu\Alipay\F2fpay\Base\Aop\encrypt($apiParams['biz_content'], $this->encryptKey); |
85
|
|
|
$apiParams['biz_content'] = $enCryptContent; |
86
|
|
|
} |
87
|
|
|
//签名 |
88
|
|
|
$sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams), $this->signType); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
//系统参数放入GET请求串 |
92
|
|
|
$requestUrl = $this->gatewayUrl . "?"; |
93
|
|
View Code Duplication |
foreach ($sysParams as $sysParamKey => $sysParamValue) { |
|
|
|
|
94
|
|
|
$requestUrl .= "$sysParamKey=" . urlencode($this->characet($sysParamValue, $this->postCharset)) . "&"; |
95
|
|
|
} |
96
|
|
|
$requestUrl = substr($requestUrl, 0, -1); |
97
|
|
|
|
98
|
|
|
return [$requestUrl, $apiParams]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 处理响应 |
103
|
|
|
* @param $request |
104
|
|
|
* @param $response |
105
|
|
|
* @author kaylv <[email protected]> |
106
|
|
|
* @return bool|mixed|\SimpleXMLElement |
107
|
|
|
*/ |
108
|
|
|
public function responseHandle($request, $response) |
109
|
|
|
{ |
110
|
|
|
//解析AOP返回结果 |
111
|
|
|
$respWellFormed = false; |
112
|
|
|
|
113
|
|
|
// 将返回结果转换本地文件编码 |
114
|
|
|
$r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $response); |
115
|
|
|
|
116
|
|
|
//存放数据 |
117
|
|
|
$signData = null; |
118
|
|
|
$respObject = null; |
119
|
|
|
|
120
|
|
View Code Duplication |
if ("json" == $this->format) { |
|
|
|
|
121
|
|
|
|
122
|
|
|
$respObject = json_decode($r); |
123
|
|
|
if (null !== $respObject) { |
124
|
|
|
$respWellFormed = true; |
125
|
|
|
$signData = $this->parserJSONSignData($request, $response, $respObject); |
126
|
|
|
} |
127
|
|
|
} else { |
128
|
|
|
if ("xml" == $this->format) { |
129
|
|
|
|
130
|
|
|
$respObject = @ simplexml_load_string($response); |
131
|
|
|
if (false !== $respObject) { |
132
|
|
|
$respWellFormed = true; |
133
|
|
|
|
134
|
|
|
$signData = $this->parserXMLSignData($request, $response); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
//返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
140
|
|
|
if (false === $respWellFormed) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// 验签 |
145
|
|
|
$this->checkResponseSign($request, $signData, $response, $respObject); |
146
|
|
|
|
147
|
|
|
// 解密 |
148
|
|
View Code Duplication |
if (method_exists($request, "getNeedEncrypt") && $request->getNeedEncrypt()) { |
|
|
|
|
149
|
|
|
if ("json" == $this->format) { |
150
|
|
|
$resp = $this->encryptJSONSignSource($request, $response); |
151
|
|
|
|
152
|
|
|
// 将返回结果转换本地文件编码 |
153
|
|
|
$r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $resp); |
154
|
|
|
$respObject = json_decode($r); |
155
|
|
|
} else { |
156
|
|
|
$resp = $this->encryptXMLSignSource($request, $response); |
157
|
|
|
|
158
|
|
|
$r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $resp); |
159
|
|
|
$respObject = @ simplexml_load_string($r); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $respObject; |
164
|
|
|
} |
165
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.