1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Douyin\V1\Pay; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
9
|
|
|
use Yansongda\Artful\Direction\NoHttpRequestDirection; |
10
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
11
|
|
|
use Yansongda\Artful\Exception\InvalidConfigException; |
12
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
13
|
|
|
use Yansongda\Artful\Logger; |
14
|
|
|
use Yansongda\Artful\Rocket; |
15
|
|
|
use Yansongda\Pay\Exception\Exception; |
16
|
|
|
use Yansongda\Pay\Exception\InvalidSignException; |
17
|
|
|
|
18
|
|
|
use function Yansongda\Artful\filter_params; |
19
|
|
|
use function Yansongda\Pay\get_provider_config; |
20
|
|
|
|
21
|
|
|
class CallbackPlugin implements PluginInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws ContainerException |
25
|
|
|
* @throws InvalidConfigException |
26
|
|
|
* @throws InvalidSignException |
27
|
|
|
* @throws ServiceNotFoundException |
28
|
|
|
*/ |
29
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
30
|
|
|
{ |
31
|
|
|
Logger::debug('[Douyin][V1][Pay][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]); |
32
|
|
|
|
33
|
|
|
$params = $rocket->getParams(); |
34
|
|
|
$config = get_provider_config('douyin', $params); |
35
|
|
|
|
36
|
|
|
$value = filter_params($params, fn ($k, $v) => '' !== $v && 'msg_signature' != $k && 'type' != $k); |
37
|
|
|
|
38
|
|
|
$this->verifySign($config, $value->all(), $params['msg_signature'] ?? ''); |
39
|
|
|
|
40
|
|
|
$rocket->setPayload($params) |
41
|
|
|
->setDirection(NoHttpRequestDirection::class) |
42
|
|
|
->setDestination($rocket->getPayload()); |
43
|
|
|
|
44
|
|
|
Logger::info('[Douyin][V1][Pay][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]); |
45
|
|
|
|
46
|
|
|
return $next($rocket); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws InvalidConfigException |
51
|
|
|
* @throws InvalidSignException |
52
|
|
|
*/ |
53
|
|
|
protected function verifySign(array $config, array $contents, string $sign): void |
54
|
|
|
{ |
55
|
|
|
if (empty($sign)) { |
56
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证抖音签名失败-抖音签名为空', func_get_args()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$contents['token'] = $config['mch_secret_token'] ?? null; |
60
|
|
|
|
61
|
|
|
if (empty($contents['token'])) { |
62
|
|
|
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_token]'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
sort($contents, SORT_STRING); |
66
|
|
|
$data = trim(implode('', $contents)); |
67
|
|
|
|
68
|
|
|
$result = $sign === sha1($data); |
69
|
|
|
|
70
|
|
|
if (!$result) { |
71
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证抖音签名失败', func_get_args()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|