-
Notifications
You must be signed in to change notification settings - Fork 50
/
AopClient.php
executable file
·346 lines (303 loc) · 9.22 KB
/
AopClient.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
<?php
namespace Alipay;
use Alipay\Exception\AlipayBase64Exception;
use Alipay\Exception\AlipayInvalidSignException;
use Alipay\Exception\AlipayOpenSslException;
use Alipay\Key\AlipayKeyPair;
use Alipay\Request\AlipayRequest;
use Alipay\Signer\AlipayRSA2Signer;
use Alipay\Signer\AlipaySigner;
class AopClient
{
/**
* SDK 版本.
*/
public const SDK_VERSION = 'alipay-sdk-PHP-4.11.14.ALL';
/**
* API 版本.
*/
public const API_VERSION = '1.0';
/**
* 应用 ID.
*
* @var string
*/
protected $appId;
/**
* 签名器.
*
* @var AlipaySigner
*/
protected $signer;
/**
* 请求发送器.
*
* @var AlipayRequester
*/
protected $requester;
/**
* 响应解析器.
*
* @var AlipayResponseFactory
*/
protected $parser;
/**
* 密钥对.
*
* @var AlipayKeyPair
*/
protected $keyPair;
/**
* 创建客户端.
*
* @param string $appId 应用 ID,请在开放平台管理页面获取
* @param AlipayKeyPair $keyPair 密钥对,用于存储支付宝公钥和应用私钥
* @param AlipaySigner $signer 签名器,用于生成和验证签名
* @param AlipayRequester $requester 请求发送器,用于发送 HTTP 请求
* @param AlipayResponseFactory $parser 响应解析器,用于解析服务器原始响应
*/
public function __construct(
$appId,
AlipayKeyPair $keyPair,
AlipaySigner $signer = null,
AlipayRequester $requester = null,
AlipayResponseFactory $parser = null
) {
$this->appId = $appId;
$this->keyPair = $keyPair;
$this->signer = $signer === null ? new AlipayRSA2Signer() : $signer;
$this->requester = $requester === null ? new AlipayCurlRequester() : $requester;
$this->parser = $parser === null ? new AlipayResponseFactory() : $parser;
}
/**
* 解密被支付宝加密的敏感数据.
*
* @param string $encryptedData Base64 格式的已加密的数据,如手机号
* @param string $encodedKey Base64 编码后的密钥
* @param string $cipher 解密算法,保持默认值即可
*
* @throws AlipayOpenSslException
*
* @return string
*
* @see https://docs.alipay.com/mini/introduce/aes
* @see https://docs.alipay.com/mini/introduce/getphonenumber
*/
public static function decrypt($encryptedData, $encodedKey, $cipher = 'aes-128-cbc')
{
$key = base64_decode($encodedKey);
if ($key === false) {
throw new AlipayBase64Exception($encodedKey);
}
if (! in_array($cipher, openssl_get_cipher_methods(), true)) {
throw new AlipayOpenSslException("Cipher algorithm {$cipher} not available");
}
$result = openssl_decrypt($encryptedData, $cipher, $key);
if ($result === false) {
throw new AlipayOpenSslException();
}
return $result;
}
/**
* 一键执行请求.
*
* @param AlipayRequest $request
* @return AlipayResponse
* @throws AlipayBase64Exception
* @throws AlipayInvalidSignException
* @throws AlipayOpenSslException
* @throws Exception\AlipayInvalidResponseException
*/
public function execute(AlipayRequest $request)
{
$params = $this->build($request);
$response = $this->request($params);
return $response;
}
/**
* 拼接请求参数并签名.
*
* @param AlipayRequest $request
* @return array
* @throws AlipayBase64Exception
* @throws AlipayOpenSslException
*/
public function build(AlipayRequest $request)
{
// 组装系统参数
$sysParams = [];
$sysParams['app_id'] = $this->appId;
$sysParams['version'] = static::API_VERSION;
$sysParams['alipay_sdk'] = static::SDK_VERSION;
$sysParams['charset'] = $this->requester->getCharset();
$sysParams['format'] = $this->parser->getFormat();
$sysParams['sign_type'] = $this->signer->getSignType();
$sysParams['method'] = $request->getApiMethodName();
$sysParams['timestamp'] = $request->getTimestamp();
$sysParams['notify_url'] = $request->getNotifyUrl();
$sysParams['return_url'] = $request->getReturnUrl();
$sysParams['terminal_type'] = $request->getTerminalType();
$sysParams['terminal_info'] = $request->getTerminalInfo();
$sysParams['prod_code'] = $request->getProdCode();
$sysParams['auth_token'] = $request->getAuthToken();
$sysParams['app_auth_token'] = $request->getAppAuthToken();
$sysParams['biz_content'] = $request->getBizContent();
$sysParams = array_merge($sysParams, get_object_vars($request));
// 转换可能是数组的参数
foreach ($sysParams as $key => &$param) {
if (is_array($param) || is_object($param)) {
$param = json_encode($param, JSON_UNESCAPED_UNICODE);
}
if (is_null($param)) {
unset($sysParams[$key]);
}
}
// 签名
$sysParams['sign'] = $this->signer->generateByParams(
$sysParams,
$this->keyPair->getPrivateKey()->asResource()
);
return $sysParams;
}
/**
* 发起请求、解析响应、验证签名.
*
* @param $params
* @return AlipayResponse
* @throws AlipayBase64Exception
* @throws AlipayInvalidSignException
* @throws AlipayOpenSslException
* @throws Exception\AlipayInvalidResponseException
*/
public function request($params)
{
$raw = $this->requester->execute($params);
$response = $this->parser->parse($raw);
$this->signer->verify(
$response->getSign(),
$response->stripData(),
$this->keyPair->getPublicKey()->asResource()
);
return $response;
}
/**
* 仅拼接请求参数并签名,但不发起请求.
*
* @param AlipayRequest $request
* @return string
* @throws AlipayBase64Exception
* @throws AlipayOpenSslException
*/
public function sdkExecute(AlipayRequest $request)
{
$params = $this->build($request);
return http_build_query($params);
}
/**
* 仅拼接请求参数并签名,生成跳转 URL.
*
* @param AlipayRequest $request
* @return string
* @throws AlipayBase64Exception
* @throws AlipayOpenSslException
*/
public function pageExecuteUrl(AlipayRequest $request)
{
$queryParams = $this->build($request);
$url = $this->requester->getGateway().'?'.http_build_query($queryParams);
return $url;
}
/**
* 仅拼接请求参数并签名,生成表单 HTML.
*
* @param AlipayRequest $request
* @return string
* @throws AlipayBase64Exception
* @throws AlipayOpenSslException
*/
public function pageExecuteForm(AlipayRequest $request)
{
$fields = $this->build($request);
$html = "<form id='alipaysubmit' name='alipaysubmit' action='{$this->requester->getUrl()}' method='POST'>";
foreach ($fields as $key => $value) {
if (AlipayHelper::isEmpty($value)) {
continue;
}
$value = htmlentities($value, ENT_QUOTES | ENT_HTML5);
$html .= "<input type='hidden' name='{$key}' value='{$value}'/>";
}
$html .= "<input type='submit' value='ok' style='display:none;'></form>";
$html .= "<script>document.forms['alipaysubmit'].submit();</script>";
return $html;
}
/**
* 验证由支付宝服务器发来的回调通知请求,其签名数据是否未被篡改.
*
* @param null $params
* @return bool
* @throws AlipayBase64Exception
* @throws AlipayOpenSslException
*/
public function verify($params = null)
{
if ($params === null) {
$params = $_POST;
}
try {
$this->signer->verifyByParams(
$params,
$this->keyPair->getPublicKey()->asResource()
);
} catch (AlipayInvalidSignException $ex) {
return false;
} catch (\InvalidArgumentException $ex) {
return false;
}
return true;
}
/**
* 获取应用 ID.
*
* @return string
*/
public function getAppId()
{
return $this->appId;
}
/**
* 获取与本客户端关联的密钥对.
*
* @return AlipayKeyPair
*/
public function getKeyPair()
{
return $this->keyPair;
}
/**
* 获取与本客户端关联的响应解析器.
*
* @return AlipayResponseFactory
*/
public function getParser()
{
return $this->parser;
}
/**
* 获取与本客户端关联的请求发送器.
*
* @return AlipayRequester
*/
public function getRequester()
{
return $this->requester;
}
/**
* 获取与本客户端关联的签名器.
*
* @return AlipaySigner
*/
public function getSigner()
{
return $this->signer;
}
}