Passed
Pull Request — master (#662)
by Songda
02:09 queued 23s
created

PreparePlugin::getPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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