|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay\V2; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
|
9
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
|
10
|
|
|
use Yansongda\Artful\Exception\Exception; |
|
11
|
|
|
use Yansongda\Artful\Exception\InvalidConfigException; |
|
12
|
|
|
use Yansongda\Artful\Exception\InvalidParamsException; |
|
13
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
|
14
|
|
|
use Yansongda\Artful\Logger; |
|
15
|
|
|
use Yansongda\Artful\Rocket; |
|
16
|
|
|
use Yansongda\Pay\Exception\InvalidSignException; |
|
17
|
|
|
use Yansongda\Supports\Collection; |
|
18
|
|
|
|
|
19
|
|
|
use function Yansongda\Artful\should_do_http_request; |
|
20
|
|
|
use function Yansongda\Pay\get_provider_config; |
|
21
|
|
|
use function Yansongda\Pay\verify_alipay_sign; |
|
22
|
|
|
|
|
23
|
|
|
class VerifySignaturePlugin implements PluginInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @throws ContainerException |
|
27
|
|
|
* @throws InvalidConfigException |
|
28
|
|
|
* @throws ServiceNotFoundException |
|
29
|
|
|
* @throws InvalidSignException |
|
30
|
|
|
* @throws InvalidParamsException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
33
|
|
|
{ |
|
34
|
|
|
/* @var Rocket $rocket */ |
|
35
|
|
|
$rocket = $next($rocket); |
|
36
|
|
|
|
|
37
|
|
|
Logger::debug('[Alipay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]); |
|
38
|
|
|
|
|
39
|
|
|
if (!should_do_http_request($rocket->getDirection())) { |
|
40
|
|
|
return $rocket; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$destination = $rocket->getDestination(); |
|
44
|
|
|
|
|
45
|
|
|
if ((!$destination instanceof Collection) || empty($result = $destination->except('_sign')->all())) { |
|
46
|
|
|
throw new InvalidParamsException(Exception::RESPONSE_EMPTY, '参数异常: 支付宝验证签名时待验签参数不正确', $destination); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$config = get_provider_config('alipay', $rocket->getParams()); |
|
50
|
|
|
|
|
51
|
|
|
verify_alipay_sign($config, json_encode($result, JSON_UNESCAPED_UNICODE), $destination->get('_sign', '')); |
|
52
|
|
|
|
|
53
|
|
|
Logger::info('[Alipay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
54
|
|
|
|
|
55
|
|
|
return $rocket; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|