Passed
Pull Request — master (#913)
by Songda
01:55
created

AddPayloadSignaturePlugin::getSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidParamsException;
12
use Yansongda\Pay\Exception\ServiceNotFoundException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Pay\filter_params;
18
use function Yansongda\Pay\get_unipay_config;
19
20
class AddPayloadSignaturePlugin implements PluginInterface
21
{
22
    /**
23
     * @throws ContainerException
24
     * @throws InvalidParamsException
25
     * @throws ServiceNotFoundException
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        Logger::debug('[Unipay][AddPayloadSignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        $params = $rocket->getParams();
32
        $config = get_unipay_config($params);
33
        $payload = $rocket->getPayload();
34
35
        if (empty($payload) || $payload->isEmpty()) {
36
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 银联支付必要参数缺失。可能插件用错顺序,应该先使用 `业务插件`');
37
        }
38
39
        $rocket->mergePayload([
40
            'signature' => $this->getSignature($config['certs']['pkey'] ?? '', filter_params($rocket->getPayload()->all(), fn ($k, $v) => 'signature' != $k)),
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
            'signature' => $this->getSignature($config['certs']['pkey'] ?? '', filter_params($rocket->getPayload()->all(), fn ($k, /** @scrutinizer ignore-unused */ $v) => 'signature' != $k)),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
        ]);
42
43
        Logger::info('[Unipay][AddPayloadSignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
44
45
        return $next($rocket);
46
    }
47
48
    /**
49
     * @throws InvalidParamsException
50
     */
51
    protected function getSignature(string $pkey, array $payload): string
52
    {
53
        if (empty($pkey)) {
54
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 银联支付配置文件中未找到 `certs.pkey` 配置项。可能插件用错顺序,应该先使用 `StartPlugin`');
55
        }
56
57
        $content = Collection::wrap($payload)->sortKeys()->toString();
58
59
        openssl_sign(hash('sha256', $content), $sign, $pkey, 'sha256');
60
61
        return base64_encode($sign);
62
    }
63
}
64