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

CallbackPlugin::verifySign()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 19
rs 9.9
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\Logger;
10
use Yansongda\Artful\Rocket;
11
use Yansongda\Pay\Exception\Exception;
12
use Yansongda\Pay\Exception\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Exception\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yansongda\Pay\Exception\InvalidParamsException;
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Exception\InvalidParamsException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yansongda\Pay\Exception\InvalidResponseException;
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Exception\InvalidResponseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Pay\get_provider_config;
18
19
class CallbackPlugin implements PluginInterface
20
{
21
    public function assembly(Rocket $rocket, Closure $next): Rocket
22
    {
23
        Logger::info('[epay][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
24
25
        $this->formatRequestAndParams($rocket);
26
27
        $params = $rocket->getParams();
28
        $config = get_provider_config('alipay', $params);
29
30
        $payload = $rocket->getPayload();
31
        $signature = $payload->get('sign');
32
33
        $payload->forget('sign');
34
        $payload->forget('signType');
35
36
        $this->verifySign($config, $payload, $signature);
37
38
        $rocket->setDestination($rocket->getPayload());
39
40
        Logger::info('[epay][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);
41
42
        return $next($rocket);
43
    }
44
45
    protected function verifySign(array $config, Collection $payload, ?string $signature = null): void
46
    {
47
        if (!$signature) {
48
            throw new InvalidResponseException(Exception::SIGN_ERROR, 'Verify Epay payload Sign Failed: sign is empty', $payload);
49
        }
50
51
        $publicCert = $config['epay_public_cert_path'] ?? null;
52
53
        if (empty($publicCert)) {
54
            throw new InvalidConfigException(Exception::CONFIG_EPAY_INVALID, 'Missing Epay Config -- [epay_public_cert_path]');
55
        }
56
57
        $result = 1 === openssl_verify(
58
            $payload->sortKeys()->toString(),
59
            base64_decode($signature),
60
            file_get_contents($publicCert)
61
        );
62
        if (!$result) {
63
            throw new InvalidResponseException(Exception::SIGN_ERROR, 'Verify Epay Response Sign Failed', func_get_args());
64
        }
65
    }
66
67
    /**
68
     * @throws InvalidParamsException
69
     */
70
    protected function formatRequestAndParams(Rocket $rocket): void
71
    {
72
        $request = $rocket->getParams()['request'] ?? null;
73
        if (!$request instanceof Collection) {
74
            throw new InvalidParamsException(Exception::PARAMS_CALLBACK_REQUEST_INVALID);
75
        }
76
        $rocket->setPayload($request)->setParams($rocket->getParams()['params'] ?? []);
77
    }
78
}
79