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

CallbackPlugin::assembly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 23
rs 9.8333
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\Direction\NoHttpRequestDirection;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidConfigException;
12
use Yansongda\Artful\Exception\InvalidParamsException;
13
use Yansongda\Artful\Exception\InvalidResponseException;
14
use Yansongda\Artful\Exception\ServiceNotFoundException;
15
use Yansongda\Artful\Logger;
16
use Yansongda\Artful\Rocket;
17
use Yansongda\Pay\Exception\Exception;
18
use Yansongda\Supports\Collection;
19
20
use function Yansongda\Pay\get_provider_config;
21
22
class CallbackPlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ServiceNotFoundException
26
     * @throws InvalidResponseException
27
     * @throws InvalidConfigException
28
     * @throws ContainerException
29
     * @throws InvalidParamsException
30
     */
31
    public function assembly(Rocket $rocket, Closure $next): Rocket
32
    {
33
        Logger::info('[Epay][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $this->formatRequestAndParams($rocket);
36
37
        $params = $rocket->getParams();
38
        $config = get_provider_config('epay', $params);
39
40
        $payload = $rocket->getPayload();
41
        $signature = $payload->get('sign');
42
43
        $payload->forget('sign');
44
        $payload->forget('signType');
45
46
        $this->verifySign($config, $payload, $signature);
47
48
        $rocket->setDirection(NoHttpRequestDirection::class)
49
            ->setDestination($rocket->getPayload());
50
51
        Logger::info('[Epay][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);
52
53
        return $next($rocket);
54
    }
55
56
    /**
57
     * @throws InvalidConfigException
58
     * @throws InvalidResponseException
59
     */
60
    protected function verifySign(array $config, Collection $payload, ?string $signature = null): void
61
    {
62
        if (!$signature) {
63
            throw new InvalidResponseException(Exception::SIGN_ERROR, 'Verify Epay payload Sign Failed: sign is empty', $payload);
64
        }
65
66
        $publicCert = $config['epay_public_cert_path'] ?? null;
67
68
        if (empty($publicCert)) {
69
            throw new InvalidConfigException(Exception::CONFIG_EPAY_INVALID, 'Missing Epay Config -- [epay_public_cert_path]');
70
        }
71
72
        $result = 1 === openssl_verify(
73
            $payload->sortKeys()->toString(),
74
            base64_decode($signature),
75
            file_get_contents($publicCert)
76
        );
77
        if (!$result) {
78
            throw new InvalidConfigException(Exception::SIGN_ERROR, 'Verify Epay Response Sign Failed', func_get_args());
79
        }
80
    }
81
82
    /**
83
     * @throws InvalidParamsException
84
     */
85
    protected function formatRequestAndParams(Rocket $rocket): void
86
    {
87
        $request = $rocket->getParams()['request'] ?? null;
88
        if (!$request instanceof Collection) {
89
            throw new InvalidParamsException(Exception::PARAMS_CALLBACK_REQUEST_INVALID);
90
        }
91
        $rocket->setPayload($request)->setParams($rocket->getParams()['params'] ?? []);
92
    }
93
}
94