Passed
Pull Request — master (#1002)
by
unknown
02:31
created

VerifySignaturePlugin::getSignatureData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Epay;
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\Pay\Exception\InvalidSignException;
16
use Yansongda\Supports\Arr;
17
use Yansongda\Supports\Collection;
18
use Yansongda\Supports\Str;
19
20
use function Yansongda\Artful\should_do_http_request;
21
use function Yansongda\Pay\get_provider_config;
22
23
class VerifySignaturePlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ServiceNotFoundException
27
     * @throws ContainerException|InvalidConfigException|InvalidSignException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        /* @var Rocket $rocket */
32
        $rocket = $next($rocket);
33
34
        Logger::info('[Epay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
35
36
        if (should_do_http_request($rocket->getDirection())) {
37
            $params = $rocket->getParams();
38
            $config = get_provider_config('epay', $params);
39
            $body = (string) $rocket->getDestinationOrigin()->getBody();
40
            $this->verifySign($config, $body);
41
        }
42
43
        Logger::info('[Epay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
44
45
        return $rocket;
46
    }
47
48
    /**
49
     * @throws InvalidConfigException|InvalidSignException
50
     */
51
    protected function verifySign(array $config, string $body): void
52
    {
53
        // 解析签名
54
        $signatureData = $this->getSignatureData($body);
55
56
        if (!$signatureData['sign']) {
57
            throw new InvalidSignException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, 'Verify Epay Response Sign Failed: sign is empty', $body);
58
        }
59
60
        $publicCert = $config['epay_public_cert_path'] ?? null;
61
        if (empty($publicCert)) {
62
            throw new InvalidConfigException(Exception::CONFIG_EPAY_INVALID, 'Missing Epay Config -- [epay_public_cert_path]');
63
        }
64
        $result = 1 === openssl_verify(
65
            $signatureData['data'],
66
            base64_decode($signatureData['sign']),
67
            file_get_contents($publicCert)
68
        );
69
        if (!$result) {
70
            throw new InvalidSignException(Exception::SIGN_ERROR, 'Verify Epay Response Sign Failed', func_get_args());
71
        }
72
    }
73
74
    private function getSignatureData(string $body): array
75
    {
76
        if (Str::contains($body, '&-&')) {
77
            $beginIndex = strpos($body, '&signType=');
78
            $endIndex = strpos($body, '&-&');
79
            $data = substr($body, 0, $beginIndex).substr($body, $endIndex);
80
81
            $signIndex = strpos($body, '&sign=');
82
            $signature = substr($body, $signIndex + strlen('&sign='), $endIndex - ($signIndex + strlen('&sign=')));
83
        } else {
84
            $result = Arr::wrapQuery($body, true);
85
            $result = Collection::wrap($result);
86
            $signature = $result->get('sign');
87
            $result->forget('sign');
88
            $result->forget('signType');
89
            $data = $result->sortKeys()->toString();
90
        }
91
92
        return [
93
            'sign' => $signature,
94
            'data' => $data,
95
        ];
96
    }
97
}
98