CreatePlugin   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 78
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 28 4
A encryptSensitiveData() 0 15 3
A normal() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Transfer;
6
7
use Closure;
8
use JetBrains\PhpStorm\Deprecated;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidConfigException;
12
use Yansongda\Artful\Exception\InvalidParamsException;
13
use Yansongda\Artful\Exception\ServiceNotFoundException;
14
use Yansongda\Artful\Logger;
15
use Yansongda\Artful\Rocket;
16
use Yansongda\Pay\Exception\DecryptException;
17
use Yansongda\Pay\Exception\Exception;
18
use Yansongda\Pay\Pay;
19
use Yansongda\Supports\Collection;
20
21
use function Yansongda\Pay\encrypt_wechat_contents;
22
use function Yansongda\Pay\get_provider_config;
23
use function Yansongda\Pay\get_wechat_public_key;
24
use function Yansongda\Pay\get_wechat_serial_no;
25
use function Yansongda\Pay\get_wechat_type_key;
26
27
/**
28
 * @see https://pay.weixin.qq.com/docs/merchant/apis/batch-transfer-to-balance/transfer-batch/initiate-batch-transfer.html
29
 */
30
#[Deprecated(reason: '由于微信支付变更,自 v3.7.12 开始废弃, 并将在 v3.8.0 移除')]
31
class CreatePlugin implements PluginInterface
32
{
33
    /**
34
     * @throws ContainerException
35
     * @throws DecryptException
36
     * @throws InvalidConfigException
37
     * @throws InvalidParamsException
38
     * @throws ServiceNotFoundException
39
     */
40
    public function assembly(Rocket $rocket, Closure $next): Rocket
41
    {
42
        Logger::debug('[Wechat][Marketing][Transfer][CreatePlugin] 插件开始装载', ['rocket' => $rocket]);
43
44
        $params = $rocket->getParams();
45
        $payload = $rocket->getPayload();
46
        $config = get_provider_config('wechat', $params);
47
48
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
49
            throw new InvalidParamsException(Exception::PARAMS_PLUGIN_ONLY_SUPPORT_NORMAL_MODE, '参数异常: 发起商家转账,只支持普通商户模式,当前配置为服务商模式');
50
        }
51
52
        if (is_null($payload) || $payload->isEmpty()) {
53
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 发起商家转账参数,参数缺失');
54
        }
55
56
        $rocket->mergePayload(array_merge(
57
            [
58
                '_method' => 'POST',
59
                '_url' => 'v3/transfer/batches',
60
                'appid' => $payload->get('appid', $config[get_wechat_type_key($params)] ?? ''),
61
            ],
62
            $this->normal($params, $payload)
63
        ));
64
65
        Logger::info('[Wechat][Marketing][Transfer][CreatePlugin] 插件装载完毕', ['rocket' => $rocket]);
66
67
        return $next($rocket);
68
    }
69
70
    /**
71
     * @throws ContainerException
72
     * @throws InvalidParamsException
73
     * @throws ServiceNotFoundException
74
     * @throws DecryptException
75
     * @throws InvalidConfigException
76
     */
77
    protected function normal(array $params, Collection $payload): array
78
    {
79
        if (!$payload->has('transfer_detail_list.0.user_name')) {
80
            return [];
81
        }
82
83
        return $this->encryptSensitiveData($params, $payload);
84
    }
85
86
    /**
87
     * @throws ContainerException
88
     * @throws DecryptException
89
     * @throws InvalidConfigException
90
     * @throws InvalidParamsException
91
     * @throws ServiceNotFoundException
92
     */
93
    protected function encryptSensitiveData(array $params, Collection $payload): array
94
    {
95
        $data['transfer_detail_list'] = $payload->get('transfer_detail_list', []);
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...
96
        $data['_serial_no'] = get_wechat_serial_no($params);
97
98
        $config = get_provider_config('wechat', $params);
99
        $publicKey = get_wechat_public_key($config, $data['_serial_no']);
100
101
        foreach ($data['transfer_detail_list'] as $key => $list) {
102
            if (!empty($list['user_name'])) {
103
                $data['transfer_detail_list'][$key]['user_name'] = encrypt_wechat_contents($list['user_name'], $publicKey);
104
            }
105
        }
106
107
        return $data;
108
    }
109
}
110