HasWechatEncryption.php
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Traits;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use function Yansongda\Pay\get_wechat_config;
use function Yansongda\Pay\reload_wechat_public_certs;
trait HasWechatEncryption
{
/**
* @throws ContainerException
* @throws InvalidConfigException
* @throws InvalidParamsException
* @throws InvalidResponseException
* @throws ServiceNotFoundException
*/
public function loadSerialNo(array $params): array
{
$config = get_wechat_config($params);
if (empty($config['wechat_public_cert_path'])) {
reload_wechat_public_certs($params);
$config = get_wechat_config($params);
}
if (empty($params['_serial_no'])) {
mt_srand();
$params['_serial_no'] = strval(array_rand($config['wechat_public_cert_path']));
}
return $params;
}
/**
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function getPublicKey(array $params, string $serialNo): string
{
$config = get_wechat_config($params);
$publicKey = $config['wechat_public_cert_path'][$serialNo] ?? null;
if (empty($publicKey)) {
throw new InvalidParamsException(Exception::WECHAT_SERIAL_NO_NOT_FOUND, 'Wechat serial no not found: '.$serialNo);
}
return $publicKey;
}
}