Passed
Pull Request — master (#765)
by
unknown
01:42
created

OnlyContractPlugin::getConfigKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Yansongda\Pay\Plugin\Wechat\Papay;
4
5
use Closure;
6
7
use function Yansongda\Pay\get_wechat_config;
8
9
use Yansongda\Pay\Parser\NoHttpRequestParser;
10
use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin;
11
use Yansongda\Pay\Rocket;
12
13
/**
14
 * 返回只签约(委托代扣)参数.
15
 */
16
class OnlyContractPlugin extends RadarSignPlugin
17
{
18
    public function assembly(Rocket $rocket, Closure $next): Rocket
19
    {
20
        $config = get_wechat_config($rocket->getParams());
21
22
        $wechatId = $this->getWechatId($config, $rocket);
23
24
        if (!$rocket->getPayload()->has('notify_url')) {
25
            $wechatId['notify_url'] = $config['notify_url'] ?? null;
26
        }
27
28
        $rocket->mergePayload($wechatId);
29
30
        $rocket->mergePayload([
31
            'sign' => $this->v2GetSign($config['mch_secret_key_v2'] ?? '', $rocket->getPayload()->all()),
32
        ]);
33
34
        $rocket->setDestination($rocket->getPayload());
35
36
        $rocket->setDirection(NoHttpRequestParser::class);
37
38
        return $next($rocket);
39
    }
40
41
    protected function getWechatId(array $config, Rocket $rocket): array
42
    {
43
        $configKey = $this->getConfigKey($rocket->getParams());
44
45
        $result = [
46
            'appid' => $config[$configKey] ?? '',
47
            'mch_id' => $config['mch_id'] ?? '',
48
        ];
49
50
        return $result;
51
    }
52
53
    protected function getConfigKey(array $params): string
54
    {
55
        $key = ($params['_type'] ?? 'mp').'_app_id';
56
57
        if ('app_app_id' === $key) {
58
            $key = 'app_id';
59
        }
60
61
        return $key;
62
    }
63
}
64