|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Jsb; |
|
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\InvalidSignException; |
|
15
|
|
|
use Yansongda\Supports\Arr; |
|
16
|
|
|
use Yansongda\Supports\Collection; |
|
17
|
|
|
use Yansongda\Supports\Str; |
|
18
|
|
|
|
|
19
|
|
|
use function Yansongda\Artful\should_do_http_request; |
|
20
|
|
|
use function Yansongda\Pay\get_provider_config; |
|
21
|
|
|
use function Yansongda\Pay\verify_jsb_sign; |
|
22
|
|
|
|
|
23
|
|
|
class VerifySignaturePlugin implements PluginInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @throws ContainerException |
|
27
|
|
|
* @throws InvalidConfigException |
|
28
|
|
|
* @throws InvalidSignException |
|
29
|
|
|
* @throws ServiceNotFoundException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
32
|
|
|
{ |
|
33
|
|
|
/* @var Rocket $rocket */ |
|
34
|
|
|
$rocket = $next($rocket); |
|
35
|
|
|
|
|
36
|
|
|
Logger::info('[Jsb][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]); |
|
37
|
|
|
|
|
38
|
|
|
if (should_do_http_request($rocket->getDirection())) { |
|
39
|
|
|
$params = $rocket->getParams(); |
|
40
|
|
|
$config = get_provider_config('jsb', $params); |
|
41
|
|
|
|
|
42
|
|
|
$body = (string) $rocket->getDestinationOrigin()->getBody(); |
|
43
|
|
|
$signatureData = $this->getSignatureData($body); |
|
44
|
|
|
|
|
45
|
|
|
verify_jsb_sign($config, $signatureData['data'] ?? '', $signatureData['sign'] ?? ''); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
Logger::info('[Jsb][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
49
|
|
|
|
|
50
|
|
|
return $rocket; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getSignatureData(string $body): array |
|
54
|
|
|
{ |
|
55
|
|
|
if (Str::contains($body, '&-&')) { |
|
56
|
|
|
$beginIndex = strpos($body, '&signType='); |
|
57
|
|
|
$endIndex = strpos($body, '&-&'); |
|
58
|
|
|
$data = substr($body, 0, $beginIndex).substr($body, $endIndex); |
|
59
|
|
|
|
|
60
|
|
|
$signIndex = strpos($body, '&sign='); |
|
61
|
|
|
$signature = substr($body, $signIndex + strlen('&sign='), $endIndex - ($signIndex + strlen('&sign='))); |
|
62
|
|
|
} else { |
|
63
|
|
|
$result = Arr::wrapQuery($body, true); |
|
64
|
|
|
$result = Collection::wrap($result); |
|
65
|
|
|
$signature = $result->get('sign'); |
|
66
|
|
|
$result->forget('sign'); |
|
67
|
|
|
$result->forget('signType'); |
|
68
|
|
|
$data = $result->sortKeys()->toString(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return [ |
|
72
|
|
|
'sign' => $signature, |
|
73
|
|
|
'data' => $data, |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|