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

VerifySignaturePlugin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 11

4 Methods

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