Passed
Pull Request — master (#662)
by Songda
01:51
created

PreparePlugin::getPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 20
rs 9.7666
cc 1
nc 1
nop 1
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
10
use function Yansongda\Pay\get_tenant;
11
use function Yansongda\Pay\get_unipay_config;
12
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Pay\Traits\GetUnipayCerts;
16
use Yansongda\Supports\Str;
17
18
class PreparePlugin implements PluginInterface
19
{
20
    use GetUnipayCerts;
21
22
    /**
23
     * @throws \Yansongda\Pay\Exception\ContainerException
24
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
25
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        Logger::info('[unipay][PreparePlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        $rocket->mergePayload($this->getPayload($rocket->getParams()));
32
33
        Logger::info('[unipay][PreparePlugin] 插件装载完毕', ['rocket' => $rocket]);
34
35
        return $next($rocket);
36
    }
37
38
    /**
39
     * @throws \Yansongda\Pay\Exception\ContainerException
40
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
41
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
42
     */
43
    protected function getPayload(array $params): array
44
    {
45
        $tenant = get_tenant($params);
46
        $config = get_unipay_config($params);
47
48
        $init = [
49
            'version' => '5.1.0',
50
            'encoding' => 'utf-8',
51
            'backUrl' => $config['notify_url'] ?? '',
52
            'accessType' => '0',
53
            'signature' => '',
54
            'signMethod' => '01',
55
            'merId' => $config['mch_id'] ?? '',
56
            'frontUrl' => $config['return_url'] ?? '',
57
            'certId' => $this->getCertId($tenant, $config),
58
        ];
59
60
        return array_merge(
61
            $init,
62
            array_filter($params, fn ($v, $k) => !Str::startsWith(strval($k), '_'), ARRAY_FILTER_USE_BOTH),
63
        );
64
    }
65
}
66