1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Wechat\V3\Extend\ProfitSharing; |
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/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_provider_config('wechat', $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_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
|
|
|
$wechatTypeKey = get_wechat_type_key($params); |
99
|
|
|
|
100
|
|
|
$data = [ |
101
|
|
|
'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''), |
102
|
|
|
'appid' => $config[$wechatTypeKey] ?? '', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
if ('PERSONAL_SUB_OPENID' === $payload->get('type')) { |
106
|
|
|
$data['sub_appid'] = $config['sub_'.$wechatTypeKey] ?? ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$payload->has('name')) { |
110
|
|
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return array_merge($data, $this->encryptSensitiveData($params, $config, $payload)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws ContainerException |
118
|
|
|
* @throws DecryptException |
119
|
|
|
* @throws InvalidConfigException |
120
|
|
|
* @throws InvalidParamsException |
121
|
|
|
* @throws ServiceNotFoundException |
122
|
|
|
*/ |
123
|
|
|
protected function encryptSensitiveData(array $params, array $config, Collection $payload): array |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$data['_serial_no'] = get_wechat_serial_no($params); |
|
|
|
|
126
|
|
|
|
127
|
|
|
$config = get_provider_config('wechat', $params); |
128
|
|
|
$publicKey = get_wechat_public_key($config, $data['_serial_no']); |
129
|
|
|
|
130
|
|
|
$data['name'] = encrypt_wechat_contents($payload->get('name'), $publicKey); |
131
|
|
|
|
132
|
|
|
return $data; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.