Passed
Push — master ( ab7eb6...522f6c )
by Songda
03:48 queued 01:30
created

CreatePlugin::assembly()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 28
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\MchTransfer;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidConfigException;
11
use Yansongda\Artful\Exception\InvalidParamsException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\DecryptException;
16
use Yansongda\Pay\Exception\Exception;
17
use Yansongda\Pay\Pay;
18
use Yansongda\Supports\Collection;
19
20
use function Yansongda\Pay\encrypt_wechat_contents;
21
use function Yansongda\Pay\get_provider_config;
22
use function Yansongda\Pay\get_wechat_public_key;
23
use function Yansongda\Pay\get_wechat_serial_no;
24
use function Yansongda\Pay\get_wechat_type_key;
25
26
/**
27
 * @see https://pay.weixin.qq.com/doc/v3/merchant/4012716434
28
 */
29
class CreatePlugin implements PluginInterface
30
{
31
    /**
32
     * @throws ContainerException
33
     * @throws DecryptException
34
     * @throws InvalidConfigException
35
     * @throws InvalidParamsException
36
     * @throws ServiceNotFoundException
37
     */
38
    public function assembly(Rocket $rocket, Closure $next): Rocket
39
    {
40
        Logger::debug('[Wechat][Marketing][MchTransfer][CreatePlugin] 插件开始装载', ['rocket' => $rocket]);
41
42
        $params = $rocket->getParams();
43
        $payload = $rocket->getPayload();
44
        $config = get_provider_config('wechat', $params);
45
46
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
47
            throw new InvalidParamsException(Exception::PARAMS_PLUGIN_ONLY_SUPPORT_NORMAL_MODE, '参数异常: 发起商家转账,只支持普通商户模式,当前配置为服务商模式');
48
        }
49
50
        if (is_null($payload) || $payload->isEmpty()) {
51
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 发起商家转账参数,参数缺失');
52
        }
53
54
        $rocket->mergePayload(array_merge(
55
            [
56
                '_method' => 'POST',
57
                '_url' => 'v3/fund-app/mch-transfer/transfer-bills',
58
                'appid' => $payload->get('appid', $config[get_wechat_type_key($params)] ?? ''),
59
            ],
60
            $this->normal($params, $payload)
61
        ));
62
63
        Logger::info('[Wechat][Marketing][MchTransfer][CreatePlugin] 插件装载完毕', ['rocket' => $rocket]);
64
65
        return $next($rocket);
66
    }
67
68
    /**
69
     * @throws ContainerException
70
     * @throws InvalidParamsException
71
     * @throws ServiceNotFoundException
72
     * @throws DecryptException
73
     * @throws InvalidConfigException
74
     */
75
    protected function normal(array $params, Collection $payload): array
76
    {
77
        if (!$payload->has('user_name')) {
78
            return [];
79
        }
80
81
        return $this->encryptSensitiveData($params, $payload);
82
    }
83
84
    /**
85
     * @throws ContainerException
86
     * @throws DecryptException
87
     * @throws InvalidConfigException
88
     * @throws InvalidParamsException
89
     * @throws ServiceNotFoundException
90
     */
91
    protected function encryptSensitiveData(array $params, Collection $payload): array
92
    {
93
        $data['_serial_no'] = get_wechat_serial_no($params);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
94
95
        $config = get_provider_config('wechat', $params);
96
        $publicKey = get_wechat_public_key($config, $data['_serial_no']);
97
98
        $data['user_name'] = encrypt_wechat_contents($payload->get('user_name'), $publicKey);
99
100
        return $data;
101
    }
102
}
103