Passed
Pull Request — master (#1002)
by
unknown
02:35
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\InvalidResponseException;
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\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 InvalidConfigException
28
     * @throws InvalidResponseException
29
     * @throws ContainerException
30
     */
31
    public function assembly(Rocket $rocket, Closure $next): Rocket
32
    {
33
        /* @var Rocket $rocket */
34
        $rocket = $next($rocket);
35
36
        Logger::info('[Epay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
37
38
        if (should_do_http_request($rocket->getDirection())) {
39
            $params = $rocket->getParams();
40
            $config = get_provider_config('epay', $params);
41
            $body = (string) $rocket->getDestinationOrigin()->getBody();
42
            $this->verifySign($config, $body);
43
        }
44
45
        Logger::info('[Epay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
46
47
        return $rocket;
48
    }
49
50
    /**
51
     * @throws InvalidResponseException
52
     * @throws InvalidConfigException
53
     */
54
    protected function verifySign(array $config, string $body): void
55
    {
56
        // 解析签名
57
        $signatureData = $this->getSignatureData($body);
58
59
        if (!$signatureData['sign']) {
60
            throw new InvalidResponseException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, 'Verify Epay Response Sign Failed: sign is empty', $body);
61
        }
62
63
        $publicCert = $config['epay_public_cert_path'] ?? null;
64
        if (empty($publicCert)) {
65
            throw new InvalidConfigException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, 'Missing Epay Config -- [epay_public_cert_path]');
66
        }
67
        $result = 1 === openssl_verify(
68
            $signatureData['data'],
69
            base64_decode($signatureData['sign']),
70
            file_get_contents($publicCert)
71
        );
72
        if (!$result) {
73
            throw new InvalidResponseException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, 'Verify Epay Response Sign Failed', func_get_args());
74
        }
75
    }
76
77
    protected function query(string $body): array
78
    {
79
        $result = [];
80
        foreach (explode('&', $body) as $item) {
81
            $pos = strpos($item, '=');
82
            if (!$pos) {
83
                continue;
84
            }
85
            $result[substr($item, 0, $pos)] = substr($item, $pos + 1);
86
        }
87
88
        return $result;
89
    }
90
91
    private function getSignatureData(string $body): array
92
    {
93
        if (Str::contains($body, '&-&')) {
94
            $beginIndex = strpos($body, '&signType=');
95
            $endIndex = strpos($body, '&-&');
96
            $data = substr($body, 0, $beginIndex).substr($body, $endIndex);
97
98
            $signIndex = strpos($body, '&sign=');
99
            $signature = substr($body, $signIndex + strlen('&sign='), $endIndex - ($signIndex + strlen('&sign=')));
100
        } else {
101
            $result = Arr::wrapQuery($body, true);
102
            $result = Collection::wrap($result);
103
            $signature = $result->get('sign');
104
            $result->forget('sign');
105
            $result->forget('signType');
106
            $data = $result->sortKeys()->toString();
107
        }
108
109
        return [
110
            'sign' => $signature,
111
            'data' => $data,
112
        ];
113
    }
114
}
115