Passed
Push — master ( 69b498...406a4e )
by Songda
11:57 queued 10:04
created

PayPlugin::assembly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 18
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
15
use function Yansongda\Pay\get_wechat_config;
16
use function Yansongda\Pay\get_wechat_type_key;
17
18
/**
19
 * @see https://pay.weixin.qq.com/docs/merchant/apis/code-payment-v3/direct/code-pay.html
20
 */
21
class PayPlugin implements PluginInterface
22
{
23
    /**
24
     * @throws ContainerException
25
     * @throws ServiceNotFoundException
26
     * @throws Throwable                随机字符串生成失败
27
     */
28
    public function assembly(Rocket $rocket, Closure $next): Rocket
29
    {
30
        Logger::debug('[Wechat][V3][Pay][Pos][PayPlugin] 插件开始装载', ['rocket' => $rocket]);
31
32
        $params = $rocket->getParams();
33
        $config = get_wechat_config($params);
34
35
        $rocket->mergePayload(array_merge(
36
            [
37
                '_method' => 'POST',
38
                '_url' => 'v3/pay/transactions/codepay',
39
            ],
40
            $this->normal($params, $config)
41
        ));
42
43
        Logger::info('[Wechat][V3][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]);
44
45
        return $next($rocket);
46
    }
47
48
    protected function normal(array $params, array $config): array
49
    {
50
        return [
51
            'appid' => $config[get_wechat_type_key($params)] ?? '',
52
            'mchid' => $config['mch_id'] ?? '',
53
        ];
54
    }
55
}
56