1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay; |
6
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\Deprecated; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Yansongda\Artful\Contract\ConfigInterface; |
11
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
12
|
|
|
use Yansongda\Artful\Exception\InvalidConfigException; |
13
|
|
|
use Yansongda\Artful\Exception\InvalidParamsException; |
14
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
15
|
|
|
use Yansongda\Artful\Plugin\AddPayloadBodyPlugin; |
16
|
|
|
use Yansongda\Artful\Plugin\ParserPlugin; |
17
|
|
|
use Yansongda\Artful\Plugin\StartPlugin; |
18
|
|
|
use Yansongda\Pay\Exception\DecryptException; |
19
|
|
|
use Yansongda\Pay\Exception\Exception; |
20
|
|
|
use Yansongda\Pay\Exception\InvalidSignException; |
21
|
|
|
use Yansongda\Pay\Plugin\Wechat\AddRadarPlugin; |
22
|
|
|
use Yansongda\Pay\Plugin\Wechat\ResponsePlugin; |
23
|
|
|
use Yansongda\Pay\Plugin\Wechat\V3\AddPayloadSignaturePlugin; |
24
|
|
|
use Yansongda\Pay\Plugin\Wechat\V3\WechatPublicCertsPlugin; |
25
|
|
|
use Yansongda\Pay\Provider\Alipay; |
26
|
|
|
use Yansongda\Pay\Provider\Douyin; |
27
|
|
|
use Yansongda\Pay\Provider\Jsb; |
28
|
|
|
use Yansongda\Pay\Provider\Unipay; |
29
|
|
|
use Yansongda\Pay\Provider\Wechat; |
30
|
|
|
use Yansongda\Supports\Collection; |
31
|
|
|
|
32
|
|
|
use function Yansongda\Artful\get_radar_body; |
33
|
|
|
use function Yansongda\Artful\get_radar_method; |
34
|
|
|
|
35
|
|
|
function get_tenant(array $params = []): string |
36
|
|
|
{ |
37
|
|
|
return strval($params['_config'] ?? 'default'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function get_public_cert(string $key): string |
41
|
|
|
{ |
42
|
|
|
return is_file($key) ? file_get_contents($key) : $key; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function get_private_cert(string $key): string |
46
|
|
|
{ |
47
|
|
|
if (is_file($key)) { |
48
|
|
|
return file_get_contents($key); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return "-----BEGIN RSA PRIVATE KEY-----\n". |
52
|
|
|
wordwrap($key, 64, "\n", true). |
53
|
|
|
"\n-----END RSA PRIVATE KEY-----"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function get_radar_url(array $config, ?Collection $payload): ?string |
57
|
|
|
{ |
58
|
|
|
return match ($config['mode'] ?? Pay::MODE_NORMAL) { |
59
|
|
|
Pay::MODE_SERVICE => $payload?->get('_service_url') ?? $payload?->get('_url') ?? null, |
60
|
|
|
Pay::MODE_SANDBOX => $payload?->get('_sandbox_url') ?? $payload?->get('_url') ?? null, |
61
|
|
|
default => $payload?->get('_url') ?? null, |
62
|
|
|
}; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws ContainerException |
67
|
|
|
* @throws ServiceNotFoundException |
68
|
|
|
*/ |
69
|
|
|
function get_provider_config(string $provider, array $params = []): array |
70
|
|
|
{ |
71
|
|
|
/** @var ConfigInterface $config */ |
72
|
|
|
$config = Pay::get(ConfigInterface::class); |
73
|
|
|
|
74
|
|
|
return $config->get($provider, [])[get_tenant($params)] ?? []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws ContainerException |
79
|
|
|
* @throws ServiceNotFoundException |
80
|
|
|
*/ |
81
|
|
|
#[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')] |
82
|
|
|
function get_alipay_config(array $params = []): array |
83
|
|
|
{ |
84
|
|
|
$alipay = Pay::get(ConfigInterface::class)->get('alipay'); |
85
|
|
|
|
86
|
|
|
return $alipay[get_tenant($params)] ?? []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function get_alipay_url(array $config, ?Collection $payload): string |
90
|
|
|
{ |
91
|
|
|
$url = get_radar_url($config, $payload) ?? ''; |
92
|
|
|
|
93
|
|
|
if (str_starts_with($url, 'http')) { |
94
|
|
|
return $url; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return Alipay::URL[$config['mode'] ?? Pay::MODE_NORMAL]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws InvalidConfigException |
102
|
|
|
* @throws InvalidSignException |
103
|
|
|
*/ |
104
|
|
|
function verify_alipay_sign(array $config, string $contents, string $sign): void |
105
|
|
|
{ |
106
|
|
|
if (empty($sign)) { |
107
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证支付宝签名失败-支付宝签名为空', func_get_args()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$public = $config['alipay_public_cert_path'] ?? null; |
111
|
|
|
|
112
|
|
|
if (empty($public)) { |
113
|
|
|
throw new InvalidConfigException(Exception::CONFIG_ALIPAY_INVALID, '配置异常: 缺少支付宝配置 -- [alipay_public_cert_path]'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result = 1 === openssl_verify( |
117
|
|
|
$contents, |
118
|
|
|
base64_decode($sign), |
119
|
|
|
get_public_cert($public), |
120
|
|
|
OPENSSL_ALGO_SHA256 |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if (!$result) { |
124
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证支付宝签名失败', func_get_args()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws ContainerException |
130
|
|
|
* @throws ServiceNotFoundException |
131
|
|
|
*/ |
132
|
|
|
#[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')] |
133
|
|
|
function get_wechat_config(array $params = []): array |
134
|
|
|
{ |
135
|
|
|
$wechat = Pay::get(ConfigInterface::class)->get('wechat'); |
136
|
|
|
|
137
|
|
|
return $wechat[get_tenant($params)] ?? []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function get_wechat_method(?Collection $payload): string |
141
|
|
|
{ |
142
|
|
|
return get_radar_method($payload) ?? 'POST'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @throws InvalidParamsException |
147
|
|
|
*/ |
148
|
|
|
function get_wechat_url(array $config, ?Collection $payload): string |
149
|
|
|
{ |
150
|
|
|
$url = get_radar_url($config, $payload); |
151
|
|
|
|
152
|
|
|
if (empty($url)) { |
153
|
|
|
throw new InvalidParamsException(Exception::PARAMS_WECHAT_URL_MISSING, '参数异常: 微信 `_url` 或 `_service_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (str_starts_with($url, 'http')) { |
157
|
|
|
return $url; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return Wechat::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @throws InvalidParamsException |
165
|
|
|
*/ |
166
|
|
|
function get_wechat_body(?Collection $payload): mixed |
167
|
|
|
{ |
168
|
|
|
$body = get_radar_body($payload); |
169
|
|
|
|
170
|
|
|
if (is_null($body)) { |
171
|
|
|
throw new InvalidParamsException(Exception::PARAMS_WECHAT_BODY_MISSING, '参数异常: 微信 `_body` 参数缺失:你可能用错插件顺序,应该先使用 `AddPayloadBodyPlugin`'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $body; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function get_wechat_type_key(array $params): string |
178
|
|
|
{ |
179
|
|
|
$key = ($params['_type'] ?? 'mp').'_app_id'; |
180
|
|
|
|
181
|
|
|
if ('app_app_id' === $key) { |
182
|
|
|
$key = 'app_id'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $key; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @throws InvalidConfigException |
190
|
|
|
*/ |
191
|
|
|
function get_wechat_sign(array $config, string $contents): string |
192
|
|
|
{ |
193
|
|
|
$privateKey = $config['mch_secret_cert'] ?? null; |
194
|
|
|
|
195
|
|
|
if (empty($privateKey)) { |
196
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_cert]'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$privateKey = get_private_cert($privateKey); |
200
|
|
|
|
201
|
|
|
openssl_sign($contents, $sign, $privateKey, 'sha256WithRSAEncryption'); |
202
|
|
|
|
203
|
|
|
return base64_encode($sign); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @throws InvalidConfigException |
208
|
|
|
*/ |
209
|
|
|
function get_wechat_sign_v2(array $config, array $payload, bool $upper = true): string |
210
|
|
|
{ |
211
|
|
|
$key = $config['mch_secret_key_v2'] ?? null; |
212
|
|
|
|
213
|
|
|
if (empty($key)) { |
214
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key_v2]'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
ksort($payload); |
218
|
|
|
|
219
|
|
|
$buff = ''; |
220
|
|
|
|
221
|
|
|
foreach ($payload as $k => $v) { |
222
|
|
|
$buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : ''; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$sign = md5($buff.'key='.$key); |
226
|
|
|
|
227
|
|
|
return $upper ? strtoupper($sign) : $sign; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @throws ContainerException |
232
|
|
|
* @throws DecryptException |
233
|
|
|
* @throws InvalidConfigException |
234
|
|
|
* @throws InvalidParamsException |
235
|
|
|
* @throws InvalidSignException |
236
|
|
|
* @throws ServiceNotFoundException |
237
|
|
|
*/ |
238
|
|
|
function verify_wechat_sign(ResponseInterface|ServerRequestInterface $message, array $params): void |
239
|
|
|
{ |
240
|
|
|
if ($message instanceof ServerRequestInterface && 'localhost' === $message->getUri()->getHost()) { |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$wechatSerial = $message->getHeaderLine('Wechatpay-Serial'); |
245
|
|
|
$timestamp = $message->getHeaderLine('Wechatpay-Timestamp'); |
246
|
|
|
$random = $message->getHeaderLine('Wechatpay-Nonce'); |
247
|
|
|
$sign = $message->getHeaderLine('Wechatpay-Signature'); |
248
|
|
|
$body = (string) $message->getBody(); |
249
|
|
|
|
250
|
|
|
$content = $timestamp."\n".$random."\n".$body."\n"; |
251
|
|
|
$public = get_provider_config('wechat', $params)['wechat_public_cert_path'][$wechatSerial] ?? null; |
252
|
|
|
|
253
|
|
|
if (empty($sign)) { |
254
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 微信签名为空', ['headers' => $message->getHeaders(), 'body' => $body]); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$public = get_public_cert( |
258
|
|
|
empty($public) ? reload_wechat_public_certs($params, $wechatSerial) : $public |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
$result = 1 === openssl_verify( |
262
|
|
|
$content, |
263
|
|
|
base64_decode($sign), |
264
|
|
|
$public, |
265
|
|
|
'sha256WithRSAEncryption' |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
if (!$result) { |
269
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证微信签名失败', ['headers' => $message->getHeaders(), 'body' => $body]); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @throws InvalidConfigException |
275
|
|
|
* @throws InvalidSignException |
276
|
|
|
*/ |
277
|
|
|
function verify_wechat_sign_v2(array $config, array $destination): void |
278
|
|
|
{ |
279
|
|
|
$sign = $destination['sign'] ?? null; |
280
|
|
|
|
281
|
|
|
if (empty($sign)) { |
282
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 微信签名为空', $destination); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$key = $config['mch_secret_key_v2'] ?? null; |
286
|
|
|
|
287
|
|
|
if (empty($key)) { |
288
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key_v2]'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (get_wechat_sign_v2($config, $destination) !== $sign) { |
292
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证微信签名失败', $destination); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
function encrypt_wechat_contents(string $contents, string $publicKey): ?string |
297
|
|
|
{ |
298
|
|
|
if (openssl_public_encrypt($contents, $encrypted, get_public_cert($publicKey), OPENSSL_PKCS1_OAEP_PADDING)) { |
299
|
|
|
return base64_encode($encrypted); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return null; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
function decrypt_wechat_contents(string $encrypted, array $config): ?string |
306
|
|
|
{ |
307
|
|
|
if (openssl_private_decrypt(base64_decode($encrypted), $decrypted, get_private_cert($config['mch_secret_cert'] ?? ''), OPENSSL_PKCS1_OAEP_PADDING)) { |
308
|
|
|
return $decrypted; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return null; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @throws ContainerException |
316
|
|
|
* @throws DecryptException |
317
|
|
|
* @throws InvalidConfigException |
318
|
|
|
* @throws InvalidParamsException |
319
|
|
|
* @throws ServiceNotFoundException |
320
|
|
|
*/ |
321
|
|
|
function reload_wechat_public_certs(array $params, ?string $serialNo = null): string |
322
|
|
|
{ |
323
|
|
|
$data = Pay::wechat()->pay( |
324
|
|
|
[StartPlugin::class, WechatPublicCertsPlugin::class, AddPayloadBodyPlugin::class, AddPayloadSignaturePlugin::class, AddRadarPlugin::class, ResponsePlugin::class, ParserPlugin::class], |
325
|
|
|
$params |
326
|
|
|
)->get('data', []); |
|
|
|
|
327
|
|
|
|
328
|
|
|
$wechatConfig = get_provider_config('wechat', $params); |
329
|
|
|
|
330
|
|
|
foreach ($data as $item) { |
331
|
|
|
$certs[$item['serial_no']] = decrypt_wechat_resource($item['encrypt_certificate'], $wechatConfig)['ciphertext'] ?? ''; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
Pay::get(ConfigInterface::class)->set( |
335
|
|
|
'wechat.'.get_tenant($params).'.wechat_public_cert_path', |
336
|
|
|
((array) ($wechatConfig['wechat_public_cert_path'] ?? [])) + ($certs ?? []), |
337
|
|
|
); |
338
|
|
|
|
339
|
|
|
if (!is_null($serialNo) && empty($certs[$serialNo])) { |
340
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 获取微信 wechat_public_cert_path 配置失败'); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $certs[$serialNo] ?? ''; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @throws ContainerException |
348
|
|
|
* @throws DecryptException |
349
|
|
|
* @throws InvalidConfigException |
350
|
|
|
* @throws InvalidParamsException |
351
|
|
|
* @throws ServiceNotFoundException |
352
|
|
|
*/ |
353
|
|
|
function get_wechat_public_certs(array $params = [], ?string $path = null): void |
354
|
|
|
{ |
355
|
|
|
reload_wechat_public_certs($params); |
356
|
|
|
|
357
|
|
|
$config = get_provider_config('wechat', $params); |
358
|
|
|
|
359
|
|
|
if (empty($path)) { |
360
|
|
|
var_dump($config['wechat_public_cert_path']); |
|
|
|
|
361
|
|
|
|
362
|
|
|
return; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
foreach ($config['wechat_public_cert_path'] as $serialNo => $cert) { |
366
|
|
|
file_put_contents($path.'/'.$serialNo.'.crt', $cert); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @throws InvalidConfigException |
372
|
|
|
* @throws DecryptException |
373
|
|
|
*/ |
374
|
|
|
function decrypt_wechat_resource(array $resource, array $config): array |
375
|
|
|
{ |
376
|
|
|
$ciphertext = base64_decode($resource['ciphertext'] ?? ''); |
377
|
|
|
$secret = $config['mch_secret_key'] ?? null; |
378
|
|
|
|
379
|
|
|
if (strlen($ciphertext) <= Wechat::AUTH_TAG_LENGTH_BYTE) { |
380
|
|
|
throw new DecryptException(Exception::DECRYPT_WECHAT_CIPHERTEXT_PARAMS_INVALID, '加解密异常: ciphertext 位数过短'); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if (is_null($secret) || Wechat::MCH_SECRET_KEY_LENGTH_BYTE != strlen($secret)) { |
384
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key]'); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$resource['ciphertext'] = match ($resource['algorithm'] ?? '') { |
388
|
|
|
'AEAD_AES_256_GCM' => decrypt_wechat_resource_aes_256_gcm($ciphertext, $secret, $resource['nonce'] ?? '', $resource['associated_data'] ?? ''), |
389
|
|
|
default => throw new DecryptException(Exception::DECRYPT_WECHAT_DECRYPTED_METHOD_INVALID, '加解密异常: algorithm 不支持'), |
390
|
|
|
}; |
391
|
|
|
|
392
|
|
|
return $resource; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @throws DecryptException |
397
|
|
|
*/ |
398
|
|
|
function decrypt_wechat_resource_aes_256_gcm(string $ciphertext, string $secret, string $nonce, string $associatedData): array|string |
399
|
|
|
{ |
400
|
|
|
$decrypted = openssl_decrypt( |
401
|
|
|
substr($ciphertext, 0, -Wechat::AUTH_TAG_LENGTH_BYTE), |
402
|
|
|
'aes-256-gcm', |
403
|
|
|
$secret, |
404
|
|
|
OPENSSL_RAW_DATA, |
405
|
|
|
$nonce, |
406
|
|
|
substr($ciphertext, -Wechat::AUTH_TAG_LENGTH_BYTE), |
407
|
|
|
$associatedData |
408
|
|
|
); |
409
|
|
|
|
410
|
|
|
if (false === $decrypted) { |
411
|
|
|
throw new DecryptException(Exception::DECRYPT_WECHAT_ENCRYPTED_DATA_INVALID, '加解密异常: 解密失败,请检查微信 mch_secret_key 是否正确'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
if ('certificate' !== $associatedData) { |
415
|
|
|
$decrypted = json_decode($decrypted, true); |
416
|
|
|
|
417
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
418
|
|
|
throw new DecryptException(Exception::DECRYPT_WECHAT_ENCRYPTED_DATA_INVALID, '加解密异常: 待解密数据非正常数据'); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return $decrypted; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @throws ContainerException |
427
|
|
|
* @throws DecryptException |
428
|
|
|
* @throws InvalidConfigException |
429
|
|
|
* @throws InvalidParamsException |
430
|
|
|
* @throws ServiceNotFoundException |
431
|
|
|
*/ |
432
|
|
|
function get_wechat_serial_no(array $params): string |
433
|
|
|
{ |
434
|
|
|
if (!empty($params['_serial_no'])) { |
435
|
|
|
return $params['_serial_no']; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$config = get_provider_config('wechat', $params); |
439
|
|
|
|
440
|
|
|
if (empty($config['wechat_public_cert_path'])) { |
441
|
|
|
reload_wechat_public_certs($params); |
442
|
|
|
|
443
|
|
|
$config = get_provider_config('wechat', $params); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
mt_srand(); |
447
|
|
|
|
448
|
|
|
return strval(array_rand($config['wechat_public_cert_path'])); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @throws InvalidParamsException |
453
|
|
|
*/ |
454
|
|
|
function get_wechat_public_key(array $config, string $serialNo): string |
455
|
|
|
{ |
456
|
|
|
$publicKey = $config['wechat_public_cert_path'][$serialNo] ?? null; |
457
|
|
|
|
458
|
|
|
if (empty($publicKey)) { |
459
|
|
|
throw new InvalidParamsException(Exception::PARAMS_WECHAT_SERIAL_NOT_FOUND, '参数异常: 微信公钥序列号为找到 -'.$serialNo); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
return $publicKey; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @throws InvalidConfigException |
467
|
|
|
*/ |
468
|
|
|
function get_wechat_miniprogram_pay_sign(array $config, string $url, string $payload): string |
469
|
|
|
{ |
470
|
|
|
if (empty($config['mini_app_key_virtual_pay'])) { |
471
|
|
|
throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mini_app_key_virtual_pay]'); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
return hash_hmac('sha256', $url.'&'.$payload, $config['mini_app_key_virtual_pay']); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
function get_wechat_miniprogram_user_sign(string $sessionKey, string $payload): string |
478
|
|
|
{ |
479
|
|
|
return hash_hmac('sha256', $payload, $sessionKey); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @throws ContainerException |
484
|
|
|
* @throws ServiceNotFoundException |
485
|
|
|
*/ |
486
|
|
|
#[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')] |
487
|
|
|
function get_unipay_config(array $params = []): array |
488
|
|
|
{ |
489
|
|
|
$unipay = Pay::get(ConfigInterface::class)->get('unipay'); |
490
|
|
|
|
491
|
|
|
return $unipay[get_tenant($params)] ?? []; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @throws InvalidConfigException |
496
|
|
|
* @throws InvalidSignException |
497
|
|
|
*/ |
498
|
|
|
function verify_unipay_sign(array $config, string $contents, string $sign, ?string $signPublicKeyCert = null): void |
499
|
|
|
{ |
500
|
|
|
if (empty($sign)) { |
501
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 银联签名为空', func_get_args()); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
if (empty($signPublicKeyCert) && empty($public = $config['unipay_public_cert_path'] ?? null)) { |
505
|
|
|
throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [unipay_public_cert_path]'); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$result = 1 === openssl_verify( |
509
|
|
|
hash('sha256', $contents), |
510
|
|
|
base64_decode($sign), |
511
|
|
|
get_public_cert($signPublicKeyCert ?? $public ?? ''), |
512
|
|
|
'sha256' |
513
|
|
|
); |
514
|
|
|
|
515
|
|
|
if (!$result) { |
516
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证银联签名失败', func_get_args()); |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @throws InvalidParamsException |
522
|
|
|
*/ |
523
|
|
|
function get_unipay_url(array $config, ?Collection $payload): string |
524
|
|
|
{ |
525
|
|
|
$url = get_radar_url($config, $payload); |
526
|
|
|
|
527
|
|
|
if (empty($url)) { |
528
|
|
|
throw new InvalidParamsException(Exception::PARAMS_UNIPAY_URL_MISSING, '参数异常: 银联 `_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`'); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
if (str_starts_with($url, 'http')) { |
532
|
|
|
return $url; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
return Unipay::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* @throws InvalidParamsException |
540
|
|
|
*/ |
541
|
|
|
function get_unipay_body(?Collection $payload): string |
542
|
|
|
{ |
543
|
|
|
$body = get_radar_body($payload); |
544
|
|
|
|
545
|
|
|
if (is_null($body)) { |
546
|
|
|
throw new InvalidParamsException(Exception::PARAMS_UNIPAY_BODY_MISSING, '参数异常: 银联 `_body` 参数缺失:你可能用错插件顺序,应该先使用 `AddPayloadBodyPlugin`'); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
return $body; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @throws InvalidConfigException |
554
|
|
|
*/ |
555
|
|
|
function get_unipay_sign_qra(array $config, array $payload): string |
556
|
|
|
{ |
557
|
|
|
$key = $config['mch_secret_key'] ?? null; |
558
|
|
|
|
559
|
|
|
if (empty($key)) { |
560
|
|
|
throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [mch_secret_key]'); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
ksort($payload); |
564
|
|
|
|
565
|
|
|
$buff = ''; |
566
|
|
|
|
567
|
|
|
foreach ($payload as $k => $v) { |
568
|
|
|
$buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : ''; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
return strtoupper(md5($buff.'key='.$key)); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* @throws InvalidConfigException |
576
|
|
|
* @throws InvalidSignException |
577
|
|
|
*/ |
578
|
|
|
function verify_unipay_sign_qra(array $config, array $destination): void |
579
|
|
|
{ |
580
|
|
|
$sign = $destination['sign'] ?? null; |
581
|
|
|
|
582
|
|
|
if (empty($sign)) { |
583
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 银联签名为空', $destination); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
$key = $config['mch_secret_key'] ?? null; |
587
|
|
|
|
588
|
|
|
if (empty($key)) { |
589
|
|
|
throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [mch_secret_key]'); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
if (get_unipay_sign_qra($config, $destination) !== $sign) { |
593
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证银联签名失败', $destination); |
594
|
|
|
} |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
function get_jsb_url(array $config, ?Collection $payload): string |
598
|
|
|
{ |
599
|
|
|
$url = get_radar_url($config, $payload) ?? ''; |
600
|
|
|
|
601
|
|
|
if (str_starts_with($url, 'http')) { |
602
|
|
|
return $url; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
return Jsb::URL[$config['mode'] ?? Pay::MODE_NORMAL]; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* @throws InvalidConfigException |
610
|
|
|
* @throws InvalidSignException |
611
|
|
|
*/ |
612
|
|
|
function verify_jsb_sign(array $config, string $content, string $sign): void |
613
|
|
|
{ |
614
|
|
|
if (empty($sign)) { |
615
|
|
|
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 江苏银行签名为空', func_get_args()); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$publicCert = $config['jsb_public_cert_path'] ?? null; |
619
|
|
|
|
620
|
|
|
if (empty($publicCert)) { |
621
|
|
|
throw new InvalidConfigException(Exception::CONFIG_JSB_INVALID, '配置异常: 缺少配置参数 -- [jsb_public_cert_path]'); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
$result = 1 === openssl_verify( |
625
|
|
|
$content, |
626
|
|
|
base64_decode($sign), |
627
|
|
|
get_public_cert($publicCert) |
628
|
|
|
); |
629
|
|
|
|
630
|
|
|
if (!$result) { |
631
|
|
|
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证江苏银行签名失败', func_get_args()); |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @throws InvalidParamsException |
637
|
|
|
*/ |
638
|
|
|
function get_douyin_url(array $config, ?Collection $payload): string |
639
|
|
|
{ |
640
|
|
|
$url = get_radar_url($config, $payload); |
641
|
|
|
|
642
|
|
|
if (empty($url)) { |
643
|
|
|
throw new InvalidParamsException(Exception::PARAMS_DOUYIN_URL_MISSING, '参数异常: 抖音 `_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`'); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
if (str_starts_with($url, 'http')) { |
647
|
|
|
return $url; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
return Douyin::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url; |
651
|
|
|
} |
652
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.