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

VerifySignaturePlugin::verifySign()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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