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