|
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\Exception\ContainerException; |
|
10
|
|
|
use Yansongda\Artful\Exception\InvalidConfigException; |
|
11
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
|
12
|
|
|
use Yansongda\Artful\Logger; |
|
13
|
|
|
use Yansongda\Artful\Rocket; |
|
14
|
|
|
use Yansongda\Pay\Exception\Exception; |
|
15
|
|
|
use Yansongda\Supports\Collection; |
|
16
|
|
|
|
|
17
|
|
|
use function Yansongda\Artful\filter_params; |
|
18
|
|
|
use function Yansongda\Pay\get_provider_config; |
|
19
|
|
|
|
|
20
|
|
|
class AddPayloadSignaturePlugin implements PluginInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @throws ContainerException |
|
24
|
|
|
* @throws InvalidConfigException |
|
25
|
|
|
* @throws ServiceNotFoundException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
28
|
|
|
{ |
|
29
|
|
|
Logger::debug('[Douyin][V1][Pay][AddPayloadSignaturePlugin] 插件开始装载', ['rocket' => $rocket]); |
|
30
|
|
|
|
|
31
|
|
|
$config = get_provider_config('douyin', $rocket->getParams()); |
|
32
|
|
|
$payload = $rocket->getPayload(); |
|
33
|
|
|
|
|
34
|
|
|
$rocket->mergePayload(['sign' => $this->getSign($config, filter_params($payload))]); |
|
35
|
|
|
|
|
36
|
|
|
Logger::info('[Douyin][V1][Pay][AddPayloadSignaturePlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
37
|
|
|
|
|
38
|
|
|
return $next($rocket); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws InvalidConfigException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function getSign(array $config, Collection $payload): string |
|
45
|
|
|
{ |
|
46
|
|
|
$salt = $config['mch_secret_salt'] ?? null; |
|
47
|
|
|
|
|
48
|
|
|
if (empty($salt)) { |
|
49
|
|
|
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_salt]'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach ($payload as $key => $value) { |
|
53
|
|
|
if (is_string($value)) { |
|
54
|
|
|
$value = trim($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (in_array($key, ['other_settle_params', 'app_id', 'sign', 'thirdparty_id']) || empty($value) || 'null' === $value) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (is_array($value)) { |
|
62
|
|
|
$value = $this->arrayToString($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$signData[] = $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$signData[] = $salt; |
|
69
|
|
|
|
|
70
|
|
|
sort($signData, SORT_STRING); |
|
71
|
|
|
|
|
72
|
|
|
return md5(implode('&', $signData)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function arrayToString(array $value): string |
|
76
|
|
|
{ |
|
77
|
|
|
$isJsonArray = isset($value[0]); |
|
78
|
|
|
$keys = array_keys($value); |
|
79
|
|
|
|
|
80
|
|
|
if ($isJsonArray) { |
|
81
|
|
|
sort($keys); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
foreach ($keys as $key) { |
|
85
|
|
|
$val = $value[$key]; |
|
86
|
|
|
|
|
87
|
|
|
$result[] = is_array($val) ? $this->arrayToString($val) : (($isJsonArray ? '' : $key.':').trim(strval($val))); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$result = '['.implode(' ', $result ?? []).']'; |
|
91
|
|
|
|
|
92
|
|
|
return ($isJsonArray ? '' : 'map').$result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|