Passed
Pull Request — master (#909)
by Songda
04:52 queued 02:52
created

AddReceiverPlugin::normal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Extend\ProfitSharing;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\DecryptException;
11
use Yansongda\Pay\Exception\Exception;
12
use Yansongda\Pay\Exception\InvalidConfigException;
13
use Yansongda\Pay\Exception\InvalidParamsException;
14
use Yansongda\Pay\Exception\ServiceNotFoundException;
15
use Yansongda\Pay\Logger;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Pay\Rocket;
18
use Yansongda\Supports\Collection;
19
20
use function Yansongda\Pay\encrypt_wechat_contents;
21
use function Yansongda\Pay\get_wechat_config;
22
use function Yansongda\Pay\get_wechat_config_type_key;
23
use function Yansongda\Pay\get_wechat_public_key;
24
use function Yansongda\Pay\get_wechat_serial_no;
25
26
/**
27
 * @see https://pay.weixin.qq.com/docs/merchant/apis/profit-sharing/receivers/add-receiver.html
28
 * @see https://pay.weixin.qq.com/docs/partner/apis/profit-sharing/receivers/add-receiver.html
29
 */
30
class AddReceiverPlugin implements PluginInterface
31
{
32
    /**
33
     * @throws ContainerException
34
     * @throws DecryptException
35
     * @throws InvalidConfigException
36
     * @throws InvalidParamsException
37
     * @throws ServiceNotFoundException
38
     */
39
    public function assembly(Rocket $rocket, Closure $next): Rocket
40
    {
41
        Logger::debug('[Wechat][Extend][ProfitSharing][AddReceiverPlugin] 插件开始装载', ['rocket' => $rocket]);
42
43
        $params = $rocket->getParams();
44
        $config = get_wechat_config($params);
45
        $payload = $rocket->getPayload();
46
47
        if (is_null($payload)) {
48
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 缺少分账参数');
49
        }
50
51
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
52
            $data = $this->service($payload, $params, $config);
53
        }
54
55
        $rocket->mergePayload(array_merge(
56
            [
57
                '_method' => 'POST',
58
                '_url' => 'v3/profitsharing/receivers/add',
59
                '_service_url' => 'v3/profitsharing/receivers/add',
60
            ],
61
            $data ?? $this->normal($payload, $params, $config),
62
        ));
63
64
        Logger::info('[Wechat][Extend][ProfitSharing][AddReceiverPlugin] 插件装载完毕', ['rocket' => $rocket]);
65
66
        return $next($rocket);
67
    }
68
69
    /**
70
     * @throws ContainerException
71
     * @throws DecryptException
72
     * @throws InvalidConfigException
73
     * @throws InvalidParamsException
74
     * @throws ServiceNotFoundException
75
     */
76
    protected function normal(Collection $payload, array $params, array $config): array
77
    {
78
        $data = [
79
            'appid' => $config[get_wechat_config_type_key($params)],
80
        ];
81
82
        if (!$payload->has('name')) {
83
            return $data;
84
        }
85
86
        return array_merge($data, $this->encryptSensitiveData($params, $config, $payload));
87
    }
88
89
    /**
90
     * @throws ContainerException
91
     * @throws DecryptException
92
     * @throws InvalidConfigException
93
     * @throws InvalidParamsException
94
     * @throws ServiceNotFoundException
95
     */
96
    protected function service(Collection $payload, array $params, array $config): array
97
    {
98
        $data = [
99
            'sub_mchid' => $config['sub_mch_id'],
100
            'appid' => $config[get_wechat_config_type_key($params)],
101
        ];
102
103
        if ('PERSONAL_SUB_OPENID' === $payload->get('receivers.0.type')) {
104
            $data['sub_appid'] = $config['sub_'.get_wechat_config_type_key($params)] ?? '';
105
        }
106
107
        if (!$payload->has('name')) {
108
            return $data;
109
        }
110
111
        return array_merge($data, $this->encryptSensitiveData($params, $config, $payload));
112
    }
113
114
    /**
115
     * @throws ContainerException
116
     * @throws DecryptException
117
     * @throws InvalidConfigException
118
     * @throws InvalidParamsException
119
     * @throws ServiceNotFoundException
120
     */
121
    protected function encryptSensitiveData(array $params, array $config, Collection $payload): array
122
    {
123
        $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...
124
        $publicKey = get_wechat_public_key($config, $data['_serial_no']);
125
126
        $data['name'] = encrypt_wechat_contents($payload->get('name'), $publicKey);
127
128
        return $data;
129
    }
130
}
131